redis 分布式锁

= 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;
    }
}

使用:

你可能感兴趣的:(redis 分布式锁)