Skip to content

Commit

Permalink
模型支持动态指定表名和连接池名 (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft authored Oct 27, 2021
1 parent cece6c1 commit 407fb30
Showing 1 changed file with 46 additions and 48 deletions.
94 changes: 46 additions & 48 deletions src/WorkerLimiterLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,41 @@ public static function lock(string $name, int $max, ?float $timeout, ?string $po
return PoolManager::use($poolName ?? RedisManager::getDefaultPoolName(), function ($resource, RedisHandler $redis) use ($args, $numKeys) {
$redis->clearLastError();
$result = $redis->evalEx(<<<'SCRIPT'
local name = KEYS[1]
local max = ARGV[1]
local now = ARGV[2]
local timeout = ARGV[3]
local timeoutKey = name .. ':timeout'
-- 处理超时
local kv = redis.call('ZRANGEBYSCORE', timeoutKey, '-inf', now)
for key, value in pairs(kv) do
redis.call('decr', name)
redis.call('zrem', timeoutKey, value)
end
local num = redis.call('get', name)
if(num ~= false and num < '0')
then
redis.call('del', name)
end
-- 判断值是否超过最大允许数量
local num = redis.call('get', name)
if(num ~= false and num >= max)
then
return false
end
-- 写入超时列表
local id = redis.call('incr', name .. ':id_incr')
if(timeout ~= nil and false == redis.call('zadd', timeoutKey, timeout, id))
then
return false
end
-- 累加
if(false == redis.call('incr', name))
then
return false
end
return id
SCRIPT
, $args, $numKeys);
local name = KEYS[1]
local max = ARGV[1]
local now = ARGV[2]
local timeout = ARGV[3]
local timeoutKey = name .. ':timeout'
-- 处理超时
local kv = redis.call('ZRANGEBYSCORE', timeoutKey, '-inf', now)
for key, value in pairs(kv) do
redis.call('decr', name)
redis.call('zrem', timeoutKey, value)
end
local num = redis.call('get', name)
if(num ~= false and num < '0')
then
redis.call('del', name)
end
-- 判断值是否超过最大允许数量
local num = redis.call('get', name)
if(num ~= false and num >= max)
then
return false
end
-- 写入超时列表
local id = redis.call('incr', name .. ':id_incr')
if(timeout ~= nil and false == redis.call('zadd', timeoutKey, timeout, id))
then
return false
end
-- 累加
if(false == redis.call('incr', name))
then
return false
end
return id
SCRIPT, $args, $numKeys);
if (!$result && '' !== ($error = $redis->getLastError()))
{
throw new \RuntimeException($error);
Expand All @@ -86,18 +85,17 @@ public static function unlock(string $name, int $workerId, ?string $poolName = n
return PoolManager::use($poolName ?? RedisManager::getDefaultPoolName(), function ($resource, RedisHandler $redis) use ($args, $numKeys) {
$redis->clearLastError();
$result = $redis->evalEx(<<<'SCRIPT'
local name = KEYS[1]
local id = ARGV[1]
local timeoutKey = name .. ':timeout'
local num = redis.call('get', name)
redis.call('zrem', timeoutKey, id)
if(num ~= false and num > '0')
then
return redis.call('decr', name) >= 0
end
return false
SCRIPT
, $args, $numKeys);
local name = KEYS[1]
local id = ARGV[1]
local timeoutKey = name .. ':timeout'
local num = redis.call('get', name)
redis.call('zrem', timeoutKey, id)
if(num ~= false and num > '0')
then
return redis.call('decr', name) >= 0
end
return false
SCRIPT, $args, $numKeys);
if (!$result && '' !== ($error = $redis->getLastError()))
{
throw new \RuntimeException($error);
Expand Down

0 comments on commit 407fb30

Please sign in to comment.