diff --git a/.travis.yml b/.travis.yml index 7362847..7dd92f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,14 @@ language: php +services: + - redis-server + php: - 7.0 - 7.1 before_script: + - echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - composer self-update - composer install --prefer-source --dev diff --git a/src/Cache/ThrottlerCache.php b/src/Cache/ThrottlerCache.php index 1170de2..c08d38e 100644 --- a/src/Cache/ThrottlerCache.php +++ b/src/Cache/ThrottlerCache.php @@ -88,10 +88,6 @@ public function setItem(string $key, ThrottlerItemInterface $item): bool { try { $cacheItem = $this->cacheItemPool->getItem($key); - - if ($cacheItem->isHit()) { - $this->cacheItemPool->deleteItem($key); - } } catch (PsrCacheException $e) { throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious()); } diff --git a/tests/Functional/AbstractThrottlerTestCase.php b/tests/Functional/AbstractThrottlerTestCase.php index 47850de..85f5ef6 100644 --- a/tests/Functional/AbstractThrottlerTestCase.php +++ b/tests/Functional/AbstractThrottlerTestCase.php @@ -3,6 +3,7 @@ namespace Sunspikes\Tests\Ratelimit\Functional; use Cache\Adapter\PHPArray\ArrayCachePool; +use Cache\Adapter\Redis\RedisCachePool; use PHPUnit\Framework\TestCase; use Sunspikes\Ratelimit\Cache\ThrottlerCache; use Sunspikes\Ratelimit\Cache\ThrottlerCacheInterface; @@ -15,25 +16,38 @@ abstract class AbstractThrottlerTestCase extends TestCase */ protected $ratelimiter; - /** - * @var array - */ - protected $cache = []; - /** * @inheritdoc */ protected function setUp() { - $pool = new ArrayCachePool(null, $this->cache); + $pool = $this->getCachePool(); $cache = new ThrottlerCache($pool); $this->ratelimiter = $this->createRatelimiter($cache); } + /** + * Get the cache pool adapter to use + * + * @return ArrayCachePool|RedisCachePool + */ + private function getCachePool() + { + if (class_exists(\Redis::class)) { + $redis = new \Redis(); + if (true === $redis->connect('localhost')) { + return new RedisCachePool($redis); + } + } + + return new ArrayCachePool(); + } + public function testThrottlePreLimit() { - $throttle = $this->ratelimiter->get('pre-limit-test'); + $key = $this->getRateLimiterKey('pre-limit-test'); + $throttle = $this->ratelimiter->get($key); for ($i = 0; ++$i < $this->getMaxAttempts();) { $throttle->hit(); @@ -44,7 +58,8 @@ public function testThrottlePreLimit() public function testThrottlePostLimit() { - $throttle = $this->ratelimiter->get('post-limit-test'); + $key = $this->getRateLimiterKey('post-limit-test'); + $throttle = $this->ratelimiter->get($key); for ($i = 0; $i < $this->getMaxAttempts(); $i++) { $throttle->hit(); @@ -55,7 +70,8 @@ public function testThrottlePostLimit() public function testThrottleAccess() { - $throttle = $this->ratelimiter->get('access-test'); + $key = $this->getRateLimiterKey('access-test'); + $throttle = $this->ratelimiter->get($key); for ($i = 0; $i < $this->getMaxAttempts(); $i++) { $throttle->access(); @@ -66,7 +82,8 @@ public function testThrottleAccess() public function testThrottleCount() { - $throttle = $this->ratelimiter->get('count-test'); + $key = $this->getRateLimiterKey('count-test'); + $throttle = $this->ratelimiter->get($key); for ($i = 0; $i < $this->getMaxAttempts(); $i++) { $throttle->access(); @@ -77,13 +94,25 @@ public function testThrottleCount() public function testClear() { - $throttle = $this->ratelimiter->get('clear-test'); + $key = $this->getRateLimiterKey('clear-test'); + $throttle = $this->ratelimiter->get($key); $throttle->hit(); $throttle->clear(); self::assertEquals(0, $throttle->count()); } + /** + * Get an unique key based on throttling mode + * + * @param string $key + * @return string + */ + private function getRateLimiterKey(string $key): string + { + return $key .'-'. strtolower(static::class); + } + /** * @return int */