public static function lock($key, $value, $expireSeconds)
{
$response = Redis::set($key, $value, 'ex', $expireSeconds, 'nx');
if($response=='OK'){
return true;
}
return false;
}
public static function unlock($key)
{
$response = Redis::del($key);
if($response=='1'){
return true;
}
return false;
}
private static function evalScript($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null){
$response = Redis::eval($script, $numkeys,$keyOrArg1, $keyOrArgN);
if($response instanceof \Predis\Response\Status){
if($response=='OK'){
return true;
}
return false;
}elseif($response instanceof \Predis\Response\Error){
return false;
}else{
return $response;
}
}
public static function getKeyValue($key){
return self::evalScript(self::getKeyValueLuaScript(),1,$key);
}
private static function getKeyValueLuaScript()
{
return <<<'LUA'
return redis.call('GET', KEYS[1])
LUA;
}
public function backup(){
/*$database = app('redis');
if ($database instanceof \Illuminate\Redis\Database){
$client = $database->connection('default');
if ($client instanceof \Predis\Client){
// 执行命令
// $client->set($key, $value, 'ex', $expireSeconds, 'nx');
{
$connection = $client->getConnection();
if ($connection instanceof \Predis\Connection\StreamConnection) {
// 创建命令
$command = null;
$profile = $client->getProfile();
if($profile instanceof \Predis\Profile\RedisVersion300){
$command = $profile->createCommand('set', [$value, 'ex', $expireSeconds, 'nx']);
}
if($command instanceof \Predis\Command\StringSet){
// 执行命令
$response = $connection->executeCommand($command);
if ($response instanceof ResponseInterface) {
// ...
}else{
$response = $command->parseResponse($response);
}
}
}
}
}
}*/
}
while (!$ok = \App\Logic\RedisLogic::lock($lockKey,1,10)){ // 获取锁
if (time() - $starting >= 2) {
return []; // 获取锁超时
}
usleep(750 * 1000);
}