diff --git a/CHANGELOG.md b/CHANGELOG.md index b9e9640..ff1d44a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,3 +30,5 @@ - [2026-01-16] DamImpr: test method with depth array greater than 1 [#38](https://github.com/DamImpr/cache-multi-layer/pull/38) - [2026-03-25] DamImpr: Fix code review [#40](https://github.com/DamImpr/cache-multi-layer/pull/40) + +- [2026-05-31] DamImpr: Code review [#41](https://github.com/DamImpr/cache-multi-layer/pull/41) diff --git a/Dockerfile b/Dockerfile index 01d12b4..5da5115 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,12 +11,12 @@ RUN apk add --no-cache \ autoconf \ libc-dev \ && pecl install apcu redis memcache \ + && rm -rf /tmp/pear \ && docker-php-ext-enable apcu redis memcache \ && apk del .build-deps COPY --from=composer:2.8 /usr/bin/composer /usr/bin/composer -RUN echo "apc.enable_cli=1" >> /usr/local/etc/php/php.ini -RUN echo "apc.enable=1" >> /usr/local/etc/php/php.ini +RUN printf "apc.enable_cli=1\napc.enable=1\n" >> /usr/local/etc/php/php.ini WORKDIR /app COPY ./src ./src diff --git a/commands b/commands index 06cf703..ae45a64 100644 --- a/commands +++ b/commands @@ -30,7 +30,7 @@ case "$1" in docker compose down ;; *) - echo "comands allowed" + echo "commands allowed" echo " - test-sw" echo " - update-vendor" echo " - php-cs-fixer" diff --git a/docker-compose.yml b/docker-compose.yml index d4f4728..b4fbe72 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,15 +17,25 @@ services: - cache-multi-layer-network depends_on: redis-server: - condition: service_started + condition: service_healthy memcache-server: - condition: service_started + condition: service_healthy redis-server: image: redis + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 5 networks: - cache-multi-layer-network memcache-server: image: memcached + healthcheck: + test: ["CMD-SHELL", "bash -c 'exec 3<>/dev/tcp/localhost/11211'"] + interval: 5s + timeout: 3s + retries: 5 networks: - cache-multi-layer-network networks: diff --git a/entrypoint.sh b/entrypoint.sh index db3da10..ae826df 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -21,7 +21,7 @@ case "$1" in exec sh ;; *) - echo "comands allowed" + echo "commands allowed" echo " - test-sw" echo " - update-vendor" echo " - php-cs-fix" diff --git a/src/Exception/CacheConnectionException.php b/src/Exception/CacheConnectionException.php new file mode 100644 index 0000000..dcc5e8c --- /dev/null +++ b/src/Exception/CacheConnectionException.php @@ -0,0 +1,14 @@ + aka Drizella + */ +class CacheConnectionException extends \Exception +{ +} diff --git a/src/Service/ApcuCache.php b/src/Service/ApcuCache.php index 85fe71e..1759457 100644 --- a/src/Service/ApcuCache.php +++ b/src/Service/ApcuCache.php @@ -63,8 +63,15 @@ public function increment(string $key, ?int $ttl = null): int|false public function getRemainingTTL(string $key): ?int { $keyInfo = apcu_key_info($this->getEffectiveKey($key)); - - return null !== $keyInfo ? $keyInfo['ttl'] : null; + if (null === $keyInfo) { + return null; + } + $result = $keyInfo['creation_time'] + $keyInfo['ttl'] - time(); + if ($result < 0) { + return null; + } + + return $result; } #[\Override] diff --git a/src/Service/CacheManagerImpl.php b/src/Service/CacheManagerImpl.php index 45c1c76..cfb8b16 100644 --- a/src/Service/CacheManagerImpl.php +++ b/src/Service/CacheManagerImpl.php @@ -53,8 +53,11 @@ public function get(string $key): int|float|string|Cacheable|array|null return null; } $ttlRemaining = $this->caches[$i - 1]->getRemainingTTL($key); + if ($ttlRemaining <= 0) { + $ttlRemaining = null; + } for ($j = $i - 2; $j >= 0; --$j) { - $ttl = min($this->caches[$j]->getTtl(), $ttlRemaining); + $ttl = null !== $ttlRemaining ? min($this->caches[$j]->getTtl(), $ttlRemaining) : null; $this->caches[$j]->set($key, $data, $ttl); } diff --git a/src/Service/MemcacheCache.php b/src/Service/MemcacheCache.php index 5b0608a..0ac0f1c 100644 --- a/src/Service/MemcacheCache.php +++ b/src/Service/MemcacheCache.php @@ -3,8 +3,8 @@ namespace CacheMultiLayer\Service; use CacheMultiLayer\Enum\CacheEnum; +use CacheMultiLayer\Exception\CacheConnectionException; use CacheMultiLayer\Interface\Cacheable; -use Memcache; /** * MEMCACHE cache implementation. @@ -79,8 +79,9 @@ public function getRemainingTTL(string $key): ?int if (empty($val)) { return null; } + $res = $val['expires_at'] - time(); - return $val['expires_at'] - time(); + return $res >= 0 ? $res : null; } #[\Override] @@ -144,7 +145,7 @@ protected function __construct(int $ttl, array $configuration = []) } if (!$resultConnection) { - throw new \Exception('Connection not found'); + throw new CacheConnectionException('Connection not found'); } } diff --git a/src/Service/PRedisCache.php b/src/Service/PRedisCache.php index 27093ff..bad0cea 100644 --- a/src/Service/PRedisCache.php +++ b/src/Service/PRedisCache.php @@ -5,6 +5,7 @@ use CacheMultiLayer\Enum\CacheEnum; use CacheMultiLayer\Interface\Cacheable; use Predis\Client as PredisClient; +use Predis\Response\ServerException; /** * PREDIS cache implementation. @@ -16,7 +17,12 @@ class PRedisCache extends Cache #[\Override] public function decrement(string $key, ?int $ttl = null): int|false { - $value = $this->predisClient->decr($this->getEffectiveKey($key)); + try { + $value = $this->predisClient->decr($this->getEffectiveKey($key)); + } catch (ServerException) { + // value has not numeric value + return false; + } if (empty($this->getRemainingTTL($key))) { $this->predisClient->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl)); } @@ -48,7 +54,12 @@ public function set(string $key, int|float|string|Cacheable|array $val, ?int $tt #[\Override] public function increment(string $key, ?int $ttl = null): int|false { - $value = $this->predisClient->incr($this->getEffectiveKey($key)); + try { + $value = $this->predisClient->incr($this->getEffectiveKey($key)); + } catch (ServerException) { + // value has not numeric value + return false; + } if (empty($this->getRemainingTTL($key))) { $this->predisClient->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl)); } @@ -65,13 +76,13 @@ public function clear(string $key): bool #[\Override] public function clearAllCache(): bool { - return null !== $this->predisClient->flushall(); + return 'OK' === (string) $this->predisClient->flushall(); } #[\Override] public function getRemainingTTL(string $key): ?int { - $ttl = $this->predisClient->ttl($key); + $ttl = $this->predisClient->ttl($this->getEffectiveKey($key)); return $ttl >= 0 ? $ttl : null; } diff --git a/src/Service/RedisCache.php b/src/Service/RedisCache.php index 22a5e0e..24e3182 100644 --- a/src/Service/RedisCache.php +++ b/src/Service/RedisCache.php @@ -64,7 +64,7 @@ public function clear(string $key): bool #[\Override] public function clearAllCache(): bool { - return null !== $this->redis->flushall(); + return (bool) $this->redis->flushall(); } #[\Override] @@ -78,7 +78,7 @@ public function getRemainingTTL(string $key): ?int #[\Override] public function isConnected(): bool { - return null !== $this->redis->ping(); + return (bool) $this->redis->ping(); } #[\Override] diff --git a/tests/AbstractCache.php b/tests/AbstractCache.php index 8390fbf..4907b12 100644 --- a/tests/AbstractCache.php +++ b/tests/AbstractCache.php @@ -176,16 +176,35 @@ public function testEmptyDecrement(): void $this->assertEquals($expected, $actual); } + public function testFailStringIncrement(): void + { + $key = 'test_fail_increment'; + $value = 'foo'; + $this->cache->set($key, $value); + $actual = $this->cache->increment($key); + $this->assertFalse($actual); + } + + public function testFailStringDecrement(): void + { + $key = 'test_fail_decrement'; + $value = 'foo'; + $this->cache->set($key, $value); + $actual = $this->cache->decrement($key); + $this->assertFalse($actual); + } + public function testRemainingTTL(): void { - $key = 'test_clear'; - $x = 1; - $res = $this->cache->set($key, $x); + $key = 'test_remaining_ttl'; + $val = 1; + $ttl = 10; + $res = $this->cache->set($key, $val, $ttl); $this->assertTrue($res); sleep(2); - $ttl = $this->cache->getRemainingTTL($key); - $this->assertNotNull($ttl); - $this->assertLessThanOrEqual(60, $ttl); + $ttlActual = $this->cache->getRemainingTTL($key); + $this->assertNotNull($ttlActual); + $this->assertLessThan(60, $ttlActual); } public function testIsConnected(): void diff --git a/tests/AbstractCacheManager.php b/tests/AbstractCacheManager.php index 26affc3..8ba452e 100644 --- a/tests/AbstractCacheManager.php +++ b/tests/AbstractCacheManager.php @@ -2,6 +2,9 @@ namespace CacheMultiLayer\Tests; +use CacheMultiLayer\Enum\CacheEnum; +use CacheMultiLayer\Service\Cache; +use CacheMultiLayer\Service\CacheConfiguration; use CacheMultiLayer\Service\CacheManager; use CacheMultiLayer\Tests\Entity\Foo; use PHPUnit\Framework\TestCase; @@ -15,7 +18,6 @@ class AbstractCacheManager extends TestCase { private ?CacheManager $cacheManager = null; - private ?Foo $foo = null; final public function setCacheManager(?CacheManager $cacheManager): void @@ -156,4 +158,114 @@ public function testClearAllCache(): void $val2 = $this->cacheManager->get($key2); $this->assertNull($val2); } + + public function testEmptyCache(): void + { + $key = 'foo'; + $val = 'bar'; + $res = CacheManager::factory()->set($key, $val); + $this->assertFalse($res); + } + + public function testDuplicateCache(): void + { + $cc = CacheManager::factory(); + $appendTrue = $cc->appendCache(Cache::factory(CacheEnum::APCU, 10)); + $appendFalse = $cc->appendCache(Cache::factory(CacheEnum::APCU, 10)); + $this->assertTrue($appendTrue); + $this->assertFalse($appendFalse); + } + + public function testDuplicateConfig(): void + { + $cc = new CacheConfiguration(); + $appendTrue = $cc->appendCacheLevel(CacheEnum::APCU, 10); + $appendFalse = $cc->appendCacheLevel(CacheEnum::APCU, 10); + $this->assertTrue($appendTrue); + $this->assertFalse($appendFalse); + } + + public function testArrayDepth(): void + { + $x = [1, 2, 3, null, [ + 1, 2, 3, null, [ + 1, 2, 3, null, + ], + ], + ]; + $key = 'test_array_depth_manager'; + $res = $this->cacheManager->set($key, $x); + $this->assertTrue($res); + $val = $this->cacheManager->get($key); + $this->testRecursiveArray($x, $val); + } + + public function testEmptyIncrement(): void + { + $key = 'test_empty_increment_manager'; + $expected = 1; + $resultSet = $this->cacheManager->increment($key); + foreach ($resultSet as $cacheKey => $actual) { + $this->assertEquals($expected, $actual, 'cache current '.$cacheKey); + } + } + + public function testEmptyDecrement(): void + { + $key = 'test_empty_decrement_manager'; + $expected = -1; + $resultSet = $this->cacheManager->decrement($key); + foreach ($resultSet as $cacheKey => $actual) { + $this->assertEquals($expected, $actual, 'cache current '.$cacheKey); + } + } + + public function testFailStringIncrement(): void + { + $key = 'test_fail_increment_manager'; + $value = 'foo'; + $this->cacheManager->set($key, $value); + $resultSet = $this->cacheManager->increment($key); + foreach ($resultSet as $cacheKey => $actual) { + $this->assertFalse($actual, 'cache current '.$cacheKey); + } + } + + public function testFailStringDecrement(): void + { + $key = 'test_fail_decrement_manager'; + $value = 'foo'; + $this->cacheManager->set($key, $value); + $resultSet = $this->cacheManager->decrement($key); + foreach ($resultSet as $cacheKey => $actual) { + $this->assertFalse($actual, 'cache current '.$cacheKey); + } + } + + public function testRemainingTTL(): void + { + $key = 'test_remaining_ttl_manager'; + $val = 1; + $ttl = 10; + $res = $this->cacheManager->set($key, $val, $ttl); + $this->assertTrue($res); + sleep(2); + $resultSet = $this->cacheManager->getRemainingTTL($key); + foreach ($resultSet as $cacheKey => $actual) { + $this->assertNotNull($actual, 'cache current '.$cacheKey); + $this->assertLessThan(60, $actual, 'cache current '.$cacheKey); + } + } + + private function testRecursiveArray(array $actual, array $expected): void + { + foreach ($expected as $key => $value) { + $this->assertArrayHasKey($key, $actual); + if (is_array($value)) { + $this->testRecursiveArray($value, $actual[$key]); + } else { + $this->assertEquals($value, $actual[$key]); + } + } + } } diff --git a/tests/ApcuCacheTest.php b/tests/ApcuCacheTest.php index c0ef1de..39d239e 100644 --- a/tests/ApcuCacheTest.php +++ b/tests/ApcuCacheTest.php @@ -118,6 +118,18 @@ public function testEmptyIncrement(): void parent::testEmptyIncrement(); } + #[\Override] + public function testFailStringDecrement(): void + { + parent::testFailStringDecrement(); + } + + #[\Override] + public function testFailStringIncrement(): void + { + parent::testFailStringIncrement(); + } + public function testEnum(): void { $this->doTestRealEnum(CacheEnum::APCU); @@ -134,6 +146,23 @@ public function testPrefix(): void $this->assertNull($cacheOtherPrefix->get($key)); } + public function testPrefixTTL(): void + { + $val = 10; // maradona + $key = 'test_prefix'; + $cacheSamePrefix = Cache::factory(CacheEnum::APCU, 60, ['key_prefix' => 'pre_']); + $cacheOtherPrefix = Cache::factory(CacheEnum::APCU, 10, ['key_prefix' => 'other_']); + $this->getCache()->set($key, $val); + $this->assertIsInt($cacheSamePrefix->getRemainingTTL($key)); + $this->assertNull($cacheOtherPrefix->getRemainingTTL($key)); + } + + public function testNegativeTTL(): void + { + $this->expectException(\InvalidArgumentException::class); + Cache::factory(CacheEnum::APCU, -1, ['key_prefix' => 'pre_']); + } + #[\Override] public static function tearDownAfterClass(): void { diff --git a/tests/CacheManagerDryRunTest.php b/tests/CacheManagerDryRunTest.php index 7bae934..3e3837c 100644 --- a/tests/CacheManagerDryRunTest.php +++ b/tests/CacheManagerDryRunTest.php @@ -125,6 +125,88 @@ public function testClearAllCache(): void $this->assertNull($val2); } + #[\Override] + public function testArrayDepth(): void + { + $x = [1, 2, 3, null, [ + 1, 2, 3, null, [ + 1, 2, 3, null, + ], + ], + ]; + $key = 'test_array_depth'; + $res = $this->getCacheManager()->set($key, $x); + $this->assertTrue($res); + $val = $this->getCacheManager()->get($key); + $this->assertEmpty($val); + } + + #[\Override] + public function testDuplicateCache(): void + { + parent::testDuplicateCache(); + } + + #[\Override] + public function testDuplicateConfig(): void + { + parent::testDuplicateConfig(); + } + + #[\Override] + public function testEmptyCache(): void + { + parent::testEmptyCache(); + } + + #[\Override] + public function testEmptyIncrement(): void + { + $key = 'test_empty_increment'; + $resultSet = $this->getCacheManager()->increment($key); + $this->assertEmpty($resultSet); + } + + #[\Override] + public function testEmptyDecrement(): void + { + $key = 'test_empty_decrement'; + $resultSet = $this->getCacheManager()->decrement($key); + $this->assertEmpty($resultSet); + } + + #[\Override] + public function testFailStringIncrement(): void + { + $key = 'test_fail_increment'; + $value = 'foo'; + $this->getCacheManager()->set($key, $value); + $resultSet = $this->getCacheManager()->increment($key); + $this->assertEmpty($resultSet); + } + + #[\Override] + public function testFailStringDecrement(): void + { + $key = 'test_fail_decrement'; + $value = 'foo'; + $this->getCacheManager()->set($key, $value); + $resultSet = $this->getCacheManager()->decrement($key); + $this->assertEmpty($resultSet); + } + + #[\Override] + public function testRemainingTTL(): void + { + $key = 'test_remaining_ttl'; + $x = 1; + $res = $this->getCacheManager()->set($key, $x); + $this->assertTrue($res); + sleep(2); + $resultSet = $this->getCacheManager()->getRemainingTTL($key); + $this->assertEmpty($resultSet); + } + #[\Override] public static function tearDownAfterClass(): void { diff --git a/tests/CacheManagerMultiLevelTest.php b/tests/CacheManagerMultiLevelTest.php index 2bd12bd..dabaa06 100644 --- a/tests/CacheManagerMultiLevelTest.php +++ b/tests/CacheManagerMultiLevelTest.php @@ -75,6 +75,60 @@ public function testString(): void parent::testString(); } + #[\Override] + public function testEmptyCache(): void + { + parent::testEmptyCache(); + } + + #[\Override] + public function testDuplicateCache(): void + { + parent::testDuplicateCache(); + } + + #[\Override] + public function testDuplicateConfig(): void + { + parent::testDuplicateConfig(); + } + + #[\Override] + public function testArrayDepth(): void + { + parent::testArrayDepth(); + } + + #[\Override] + public function testEmptyDecrement(): void + { + parent::testEmptyDecrement(); + } + + #[\Override] + public function testEmptyIncrement(): void + { + parent::testEmptyIncrement(); + } + + #[\Override] + public function testFailStringDecrement(): void + { + parent::testFailStringDecrement(); + } + + #[\Override] + public function testFailStringIncrement(): void + { + parent::testFailStringIncrement(); + } + + #[\Override] + public function testRemainingTTL(): void + { + parent::testRemainingTTL(); + } + public function testUpdateHighestLevels(): void { $key = 'test_update_highest'; diff --git a/tests/CacheManagerOneLevelTest.php b/tests/CacheManagerOneLevelTest.php index 32d777d..fb43f74 100644 --- a/tests/CacheManagerOneLevelTest.php +++ b/tests/CacheManagerOneLevelTest.php @@ -74,6 +74,60 @@ public function testString(): void parent::testString(); } + #[\Override] + public function testEmptyCache(): void + { + parent::testEmptyCache(); + } + + #[\Override] + public function testDuplicateCache(): void + { + parent::testDuplicateCache(); + } + + #[\Override] + public function testDuplicateConfig(): void + { + parent::testDuplicateConfig(); + } + + #[\Override] + public function testArrayDepth(): void + { + parent::testArrayDepth(); + } + + #[\Override] + public function testEmptyDecrement(): void + { + parent::testEmptyDecrement(); + } + + #[\Override] + public function testEmptyIncrement(): void + { + parent::testEmptyIncrement(); + } + + #[\Override] + public function testFailStringDecrement(): void + { + parent::testFailStringDecrement(); + } + + #[\Override] + public function testFailStringIncrement(): void + { + parent::testFailStringIncrement(); + } + + #[\Override] + public function testRemainingTTL(): void + { + parent::testRemainingTTL(); + } + private static function getConfig(): CacheConfiguration { $cacheConfiguration = new CacheConfiguration(); diff --git a/tests/MemcacheCacheTest.php b/tests/MemcacheCacheTest.php index bec0142..cf11d9d 100644 --- a/tests/MemcacheCacheTest.php +++ b/tests/MemcacheCacheTest.php @@ -119,12 +119,40 @@ public function testRemainingTTL(): void parent::testRemainingTTL(); } + #[\Override] + public function testFailStringDecrement(): void + { + parent::testFailStringDecrement(); + } + + #[\Override] + public function testFailStringIncrement(): void + { + parent::testFailStringIncrement(); + } + public function testMissingServer(): void { $this->expectException(CacheMissingConfigurationException::class); Cache::factory(CacheEnum::MEMCACHE, 60, ['port' => 11211]); } + public function testNegativeTTL(): void + { + $this->expectException(\InvalidArgumentException::class); + Cache::factory(CacheEnum::MEMCACHE, -1, ['server_address' => 'memcache-server']); + } + + public function testConnectionPersistent(): void + { + $this->assertTrue( + Cache::factory(CacheEnum::MEMCACHE, 60, [ + 'server_address' => 'memcache-server', + 'persistent' => true, + ])->isConnected() + ); + } + public function testConnectionNotFound(): void { $this->expectException(\Exception::class); @@ -161,6 +189,28 @@ public function testPrefix(): void $this->assertNull($cacheOtherPrefix->get($key)); } + public function testCompression(): void + { + $cache = Cache::factory(CacheEnum::MEMCACHE, 60, ['key_prefix' => 'compressed_', 'server_address' => 'memcache-server', 'compress' => 1]); + $key = 'test_compression'; + $val = 1; + $resSet = $cache->set($key, $val); + $this->assertTrue($resSet); + $actual = $cache->get($key); + $this->assertEquals($val, $actual); + } + + public function testPrefixTTL(): void + { + $val = 10; // maradona + $key = 'test_prefix'; + $cacheSamePrefix = Cache::factory(CacheEnum::MEMCACHE, 60, ['key_prefix' => '', 'server_address' => 'memcache-server']); + $cacheOtherPrefix = Cache::factory(CacheEnum::MEMCACHE, 10, ['key_prefix' => 'other_', 'server_address' => 'memcache-server']); + $this->getCache()->set($key, $val); + $this->assertIsInt($cacheSamePrefix->getRemainingTTL($key)); + $this->assertNull($cacheOtherPrefix->getRemainingTTL($key)); + } + #[\Override] public function testArrayDepth(): void { diff --git a/tests/PRedisCacheTest.php b/tests/PRedisCacheTest.php index ce3bf5c..78753cb 100644 --- a/tests/PRedisCacheTest.php +++ b/tests/PRedisCacheTest.php @@ -5,7 +5,6 @@ use CacheMultiLayer\Enum\CacheEnum; use CacheMultiLayer\Exception\CacheMissingConfigurationException; use CacheMultiLayer\Service\Cache; -use Exception; use Predis\Client; /** @@ -117,6 +116,18 @@ public function testEmptyIncrement(): void parent::testEmptyIncrement(); } + #[\Override] + public function testFailStringDecrement(): void + { + parent::testFailStringDecrement(); + } + + #[\Override] + public function testFailStringIncrement(): void + { + parent::testFailStringIncrement(); + } + public function testMissingServer(): void { $this->expectException(CacheMissingConfigurationException::class); @@ -149,6 +160,12 @@ public function testInstance(): void $this->assertTrue(true); // no exception throwns } + public function testNegativeTTL(): void + { + $this->expectException(\InvalidArgumentException::class); + Cache::factory(CacheEnum::PREDIS, -1, ['server_address' => 'redis-server']); + } + public function testMissingInstance(): void { $this->expectException(CacheMissingConfigurationException::class); @@ -166,6 +183,17 @@ public function testPrefix(): void $this->assertNull($cacheOtherPrefix->get($key)); } + public function testPrefixTTL(): void + { + $val = 10; // maradona + $key = 'test_prefix'; + $cacheSamePrefix = Cache::factory(CacheEnum::PREDIS, 60, ['key_prefix' => '', 'server_address' => 'redis-server']); + $cacheOtherPrefix = Cache::factory(CacheEnum::PREDIS, 10, ['key_prefix' => 'other_', 'server_address' => 'redis-server']); + $this->getCache()->set($key, $val); + $this->assertIsInt($cacheSamePrefix->getRemainingTTL($key)); + $this->assertNull($cacheOtherPrefix->getRemainingTTL($key)); + } + #[\Override] public function testArrayDepth(): void { diff --git a/tests/RedisCacheTest.php b/tests/RedisCacheTest.php index ecebe07..f7b0e1f 100644 --- a/tests/RedisCacheTest.php +++ b/tests/RedisCacheTest.php @@ -5,7 +5,6 @@ use CacheMultiLayer\Enum\CacheEnum; use CacheMultiLayer\Exception\CacheMissingConfigurationException; use CacheMultiLayer\Service\Cache; -use Exception; /** * REDIS unit test class implementation. @@ -116,6 +115,18 @@ public function testEmptyIncrement(): void parent::testEmptyIncrement(); } + #[\Override] + public function testFailStringDecrement(): void + { + parent::testFailStringDecrement(); + } + + #[\Override] + public function testFailStringIncrement(): void + { + parent::testFailStringIncrement(); + } + public function testMissingServer(): void { $this->expectException(CacheMissingConfigurationException::class); @@ -163,6 +174,23 @@ public function testPrefix(): void $this->assertNull($cacheOtherPrefix->get($key)); } + public function testPrefixTTL(): void + { + $val = 10; // maradona + $key = 'test_prefix'; + $cacheSamePrefix = Cache::factory(CacheEnum::REDIS, 60, ['key_prefix' => '', 'server_address' => 'redis-server']); + $cacheOtherPrefix = Cache::factory(CacheEnum::REDIS, 10, ['key_prefix' => 'other_', 'server_address' => 'redis-server']); + $this->getCache()->set($key, $val); + $this->assertIsInt($cacheSamePrefix->getRemainingTTL($key)); + $this->assertNull($cacheOtherPrefix->getRemainingTTL($key)); + } + + public function testNegativeTTL(): void + { + $this->expectException(\InvalidArgumentException::class); + Cache::factory(CacheEnum::REDIS, -1, ['server_address' => 'redis-server']); + } + #[\Override] public function testArrayDepth(): void {