diff --git a/app/Library/Cache/Backend/Redis.php b/app/Library/Cache/Backend/Redis.php index 57422183..fba3a91a 100644 --- a/app/Library/Cache/Backend/Redis.php +++ b/app/Library/Cache/Backend/Redis.php @@ -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} * diff --git a/app/Services/Throttle.php b/app/Services/Throttle.php index 7a5f4140..341ffd37 100644 --- a/app/Services/Throttle.php +++ b/app/Services/Throttle.php @@ -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; }