美文网首页
redis 分布式锁

redis 分布式锁

作者: JJNile | 来源:发表于2021-07-12 15:12 被阅读0次
    <?php
    
    use Illuminate\Support\Facades\Redis;
    
    class RedisLock
    {
        const NOT_EXISTS              = 'NX';
        const MILLISECOND_EXPIRE_TIME = 'PX';
        // key 持有时间(millisecond)
        const EXPIRE_TIME = 500;
        // 等待超时时间
        const TIME_OUT = 1000;
    
        private static $token;
    
        // 解锁 LUA 脚本
        const LUA_SCRIPT_RELEASE_LOCK = '
            if (ARGV[1] == redis.call("GET", KEYS[1])) then
                return redis.call("DEL", KEYS[1]);
            end;
            return 0;
        ';
    
        /**
         * @param     $key
         * @param int $expire
         *
         * @return bool
         * @throws \Exception
         */
        public static function lock($key, $expire = self::EXPIRE_TIME)
        {
            $start       = self::msectime();
            self::$token = self::createToken();
    
            // 获取锁失败,再一定时间内循环尝试获取
            while (1) {
                $result = Redis::set($key, self::$token, self::MILLISECOND_EXPIRE_TIME, $expire, self::NOT_EXISTS);
                if ('OK' === (string)$result) {
                    return true;
                }
    
                $diff = self::msectime() - $start;
                if ($diff >= self::TIME_OUT) {
                    return false;
                }
    
                usleep(100);
    
            }
        }
    
    
        /**
         * @param $key
         *
         * @return bool
         */
        public static function unlock($key)
        {
            return (bool)Redis::eval(self::LUA_SCRIPT_RELEASE_LOCK, 1, $key, self::$token);
        }
    
    
        /**
         * @return string
         * @throws \Exception
         */
        public static function createToken()
        {
            if (PHP_VERSION_ID >= 70000 && function_exists('random_bytes')) {
                return getmypid() . '_' . bin2hex(random_bytes(16));
            }
            return getmypid() . '_' . uniqid() . rand(10000, 99999);
        }
    
        /**
         * 返回当前的毫秒时间戳
         *
         * @return float
         */
        public static function  msectime() {
            list($msec, $sec) = explode(' ', microtime());
            $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
            return $msectime;
        }
    }
    
    

    使用:

    <?php
    
    $key = 'redis_lock:'.$response['order_no'];
    $lock = RedisLock::lock($key);
    if(! $lock) {
        Log::error("order lock: ".$key);
        return;
    }
    
    try {
    
        sleep(10);
       
    } catch (\Throwable $throwable) {
        Log::error(CommonService::getExceptionMsg($throwable));
    } finally {
        // 处理完任务释放锁
        RedisLock::unlock($key);
    }
    

    相关文章

      网友评论

          本文标题:redis 分布式锁

          本文链接:https://www.haomeiwen.com/subject/knnrpltx.html