Last active
March 4, 2020 10:11
-
-
Save GithubMrxia/89765b42d07d07f5f3f272b403f1f19b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Libs; | |
use Illuminate\Support\Facades\Redis; | |
class RedisLib | |
{ | |
/** | |
* 加锁 | |
* | |
* @param string $lockName | |
* @param integer $timeout | |
* @param integer $waitTime | |
* @return mixed | |
*/ | |
public static function addLock($lockName, $timeout = 3600, $waitTime = 10) | |
{ | |
$lockName = self::getLockName($lockName); | |
$lockValue = 'lock:'.uniqid(date('YmdHis')); | |
$timeout = max(intval($timeout), 1); | |
$waitTime = max(intval($waitTime), 1); | |
$endTime = time() + $waitTime; | |
while(time() < $endTime) { | |
if (Redis::setnx($lockName, $lockValue)) { | |
Redis::expire($lockName, $timeout); | |
return $lockValue; | |
} else { | |
if (Redis::ttl($lockName) == -1) { | |
Redis::expire($lockName, $timeout); | |
} | |
} | |
sleep(0.01); | |
} | |
return false; | |
} | |
/** | |
* 解锁 | |
* | |
* @param string $lockName | |
* @param string $lockValue | |
* @return void | |
*/ | |
public static function delLock($lockName, $lockValue) | |
{ | |
$lockName = self::getLockName($lockName); | |
$res = false; | |
Redis::watch($lockName); | |
if (Redis::get($lockName) == $lockValue) { | |
Redis::multi(); | |
Redis::del($lockName); | |
$res = Redis::exec(); | |
} | |
return $res; | |
} | |
/** | |
* 锁名 | |
* | |
* @param string $lockName | |
* @return void | |
*/ | |
public static function getLockName($lockName) | |
{ | |
return 'lock:'.$lockName; | |
} | |
/** | |
* Redis对象 | |
* | |
* @return obj | |
*/ | |
public static function redis() | |
{ | |
return Redis::client(); | |
} | |
/** | |
* set | |
* | |
* @param string $key | |
* @param mixed $value | |
* @param integer $expire | |
* @return mixed | |
*/ | |
public static function set($key, $value, $expire = 0) | |
{ | |
Redis::set($key, $value); | |
if ($expire > 0) { | |
Redis::expire($key, $expire); | |
} | |
return $value; | |
} | |
/** | |
* 加入队列 | |
* | |
* @param string $queueName | |
* @param string $val | |
* @param mixed $score | |
* @return boll | |
*/ | |
public static function addQueueMember($queueName, $val, $score) | |
{ | |
$key = self::getQueueKey($queueName); | |
if (! is_numeric($score)) { | |
$score = strtotime($score); | |
} | |
return (bool)Redis::zAdd($key, $score, $val); | |
} | |
/** | |
* 获取队列 | |
* | |
* @param string $queueName | |
* @param integer $end | |
* @param boolean $withscores | |
* @return array | |
*/ | |
public static function getQueue($queueName, $end = -1, $withscores = false) | |
{ | |
$key = self::getQueueKey($queueName); | |
if (Redis::exists($key)) { | |
return $withscores ? Redis::zRange($key, 0, $end, 'withscores') : Redis::zRange($key, 0, $end); | |
} | |
return []; | |
} | |
/** | |
* 删除队列成员 | |
* | |
* @param string $queueName | |
* @param string $key | |
* @return bool | |
*/ | |
public static function remQueueMember($queueName, $key) | |
{ | |
$queueKey = self::getQueueKey($queueName); | |
return (bool)RedisLib::zrem($queueKey, $key); | |
} | |
/** | |
* 删除队列 | |
* | |
* @param string $key | |
* @return bool | |
*/ | |
public static function delQueue($key) | |
{ | |
return (bool)self::del(self::getQueueKey($key)); | |
} | |
/** | |
* 队列名key值 | |
* | |
* @param [type] $queueName | |
* @return void | |
*/ | |
public static function getQueueKey($queueName) | |
{ | |
return 'queue:'.$queueName; | |
} | |
/** | |
* 处理本类中未有的静态方法,调用Redis方法 | |
* | |
* @param string $name | |
* @param array $arguments | |
* @return mixed | |
*/ | |
public static function __callStatic($name, $arguments) | |
{ | |
return forward_static_call_array(["Illuminate\Support\Facades\Redis", $name], $arguments); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment