1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-23 20:00:27 +08:00

使用ttl方法来取代exist判断键值是否存在

This commit is contained in:
winzer 2021-02-01 20:34:36 +08:00
parent 5d96cd0f8c
commit a5cca0e899
2 changed files with 25 additions and 8 deletions

View File

@ -186,6 +186,23 @@ class Redis extends \Phalcon\Cache\Backend\Redis
return (bool)$redis->exists($lastKey);
}
/**
* @param string $keyName
* @return int|bool
*/
public function ttl($keyName = null)
{
$redis = $this->getRedis();
if ($keyName === null) {
$lastKey = $this->_lastKey;
} else {
$lastKey = $this->getKeyName($keyName);
}
return $redis->ttl($lastKey);
}
/**
* {@inheritdoc}
*

View File

@ -19,18 +19,18 @@ class Throttle extends Service
$cacheKey = $this->getCacheKey($sign);
if ($cache->ttl($cacheKey) < 1) {
$cache->save($cacheKey, 0, $config->path('throttle.lifetime'));
}
$rateLimit = $cache->get($cacheKey);
if ($rateLimit) {
if ($rateLimit >= $config->path('throttle.rate_limit')) {
return false;
} else {
$cache->increment($cacheKey, 1);
}
} else {
$cache->save($cacheKey, 1, $config->path('throttle.lifetime'));
if ($rateLimit >= $config->path('throttle.rate_limit')) {
return false;
}
$cache->increment($cacheKey, 1);
return true;
}