diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f7e2b8..348a376 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,3 +20,5 @@ - [2026-01-09] DamImpr: Instance configuration [#33](https://github.com/DamImpr/cache-multi-layer/pull/33) - [2026-01-12] DamImpr: redis cache with phpredis or predis [#34](https://github.com/DamImpr/cache-multi-layer/pull/34) + +- [2026-01-16] DamImpr: prefix key [#35](https://github.com/DamImpr/cache-multi-layer/pull/35) diff --git a/commands b/commands index 3de710f..06cf703 100644 --- a/commands +++ b/commands @@ -5,23 +5,29 @@ case "$1" in for i in 8.2 8.3 8.4 do PHP_VERSION=$i docker compose build - docker compose run --rm --remove-orphans app test-sw && docker compose down + docker compose run --rm --remove-orphans app test-sw + docker compose down done ;; 'update-vendor') - docker compose run --rm --remove-orphans app update-vendor && docker compose down + docker compose run --rm --remove-orphans app update-vendor + docker compose down ;; 'php-cs-fixer') - docker compose run --rm --remove-orphans app php-cs-fixer && docker compose down + docker compose run --rm --remove-orphans app php-cs-fixer + docker compose down ;; 'rector') - docker compose run --rm --remove-orphans app rector && docker compose down + docker compose run --rm --remove-orphans app rector + docker compose down ;; 'phpstan') - docker compose run --rm --remove-orphans app phpstan && docker compose down + docker compose run --rm --remove-orphans app phpstan + docker compose down ;; 'sh') - docker compose run --rm --remove-orphans app sh && docker compose down + docker compose run --rm --remove-orphans app sh + docker compose down ;; *) echo "comands allowed" diff --git a/src/Service/MemcacheCache.php b/src/Service/MemcacheCache.php index 2952e89..6cc6ade 100644 --- a/src/Service/MemcacheCache.php +++ b/src/Service/MemcacheCache.php @@ -32,7 +32,7 @@ protected function getMandatoryConfig(): array #[Override] public function clear(string $key): bool { - return $this->memcache->delete($key); + return $this->memcache->delete($this->getEffectiveKey($key)); } /** @@ -52,7 +52,7 @@ public function clearAllCache(): bool #[Override] public function decrement(string $key, ?int $ttl = null): int|false { - $pair = $this->memcache->get($key); + $pair = $this->memcache->get($this->getEffectiveKey($key)); if (empty($pair)) { $this->set($key, -1, $ttl); return -1; @@ -65,7 +65,7 @@ public function decrement(string $key, ?int $ttl = null): int|false --$value; $pair['data'] = $value; - $this->memcache->set($key, $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key)); + $this->memcache->set($this->getEffectiveKey($key), $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key)); return $value; } @@ -76,7 +76,7 @@ public function decrement(string $key, ?int $ttl = null): int|false #[Override] public function get(string $key): int|float|string|Cacheable|array|null { - $val = $this->memcache->get($key); + $val = $this->memcache->get($this->getEffectiveKey($key)); if (empty($val)) { return null; } @@ -102,7 +102,7 @@ public function getEnum(): CacheEnum #[Override] public function getRemainingTTL(string $key): ?int { - $val = $this->memcache->get($key); + $val = $this->memcache->get($this->getEffectiveKey($key)); if (empty($val)) { return null; } @@ -117,7 +117,7 @@ public function getRemainingTTL(string $key): ?int #[Override] public function increment(string $key, ?int $ttl = null): int|false { - $pair = $this->memcache->get($key); + $pair = $this->memcache->get($this->getEffectiveKey($key)); if (empty($pair)) { $this->set($key, 1, $ttl); return 1; @@ -130,7 +130,7 @@ public function increment(string $key, ?int $ttl = null): int|false ++$value; $pair['data'] = $value; - $this->memcache->set($key, $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key)); + $this->memcache->set($this->getEffectiveKey($key), $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key)); return $value; } @@ -157,7 +157,7 @@ public function set(string $key, int|float|string|Cacheable|array $val, ?int $tt 'data' => json_encode($values) , 'exipres_at' => time() + $ttlToUse ]; - return $this->memcache->set($key, $dataToStore, $this->compress ? MEMCACHE_COMPRESSED : 0, $ttlToUse); + return $this->memcache->set($this->getEffectiveKey($key), $dataToStore, $this->compress ? MEMCACHE_COMPRESSED : 0, $ttlToUse); } /** @@ -182,7 +182,7 @@ protected function __construct(int $ttl, array $configuration = []) throw new Exception("Connection not found"); } } - + $this->prefixKey = $configuration['key_prefix'] ?? ''; $this->compress = array_key_exists('compress', $configuration) && $configuration['compress']; } @@ -196,10 +196,18 @@ protected function assertConfig(array $configuration): void } } + /** + * manages keys by adding the prefix set during configuration + * @param string $key cache key + * @return string key to be used + */ + private function getEffectiveKey(string $key): string + { + return $this->prefixKey . $key; + } private readonly Memcache $memcache; - + private readonly string $prefixKey; private readonly bool $compress; - private array $mandatoryKeys = [ 'server_address' ]; diff --git a/src/Service/PRedisCache.php b/src/Service/PRedisCache.php index f42a663..7b998f3 100644 --- a/src/Service/PRedisCache.php +++ b/src/Service/PRedisCache.php @@ -22,9 +22,9 @@ class PRedisCache extends Cache #[\Override] public function decrement(string $key, ?int $ttl = null): int|false { - $value = $this->predisClient->decr($key); + $value = $this->predisClient->decr($this->getEffectiveKey($key)); if (empty($this->getRemainingTTL($key))) { - $this->predisClient->expire($key, $this->getTtlToUse($ttl)); + $this->predisClient->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl)); } return $value; @@ -37,7 +37,7 @@ public function decrement(string $key, ?int $ttl = null): int|false #[\Override] public function get(string $key): int|float|string|Cacheable|array|null { - $val = $this->predisClient->get($key); + $val = $this->predisClient->get($this->getEffectiveKey($key)); if ($val === null) { return null; } @@ -54,7 +54,7 @@ public function get(string $key): int|float|string|Cacheable|array|null public function set(string $key, int|float|string|Cacheable|array $val, ?int $ttl = null): bool { $data = is_array($val) ? $this->serializeValArray($val) : $this->serializeVal($val); - return $this->predisClient->setex($key, $this->getTtlToUse($ttl), json_encode($data)) !== null; + return $this->predisClient->setex($this->getEffectiveKey($key), $this->getTtlToUse($ttl), json_encode($data)) !== null; } /** @@ -64,9 +64,9 @@ 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($key); + $value = $this->predisClient->incr($this->getEffectiveKey($key)); if (empty($this->getRemainingTTL($key))) { - $this->predisClient->expire($key, $this->getTtlToUse($ttl)); + $this->predisClient->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl)); } return $value; @@ -79,7 +79,7 @@ public function increment(string $key, ?int $ttl = null): int|false #[Override] public function clear(string $key): bool { - return (bool) $this->predisClient->del($key); + return (bool) $this->predisClient->del($this->getEffectiveKey($key)); } /** @@ -123,6 +123,7 @@ protected function __construct(int $ttl, array $configuration = []) 'conn_uid' => $configuration['connection_id'] ?? '' ]); } + $this->prefixKey = $configuration['key_prefix'] ?? ''; } /** @@ -165,8 +166,18 @@ protected function assertConfig(array $configuration): void } } - private readonly PredisClient $predisClient; + /** + * manages keys by adding the prefix set during configuration + * @param string $key cache key + * @return string key to be used + */ + private function getEffectiveKey(string $key): string + { + return $this->prefixKey . $key; + } + private readonly PredisClient $predisClient; + private readonly string $prefixKey; private array $mandatoryKeys = [ 'server_address' ]; diff --git a/src/Service/RedisCache.php b/src/Service/RedisCache.php index 6b0065d..902c543 100644 --- a/src/Service/RedisCache.php +++ b/src/Service/RedisCache.php @@ -21,9 +21,9 @@ class RedisCache extends Cache #[\Override] public function decrement(string $key, ?int $ttl = null): int|false { - $value = $this->redis->decr($key); + $value = $this->redis->decr($this->getEffectiveKey($key)); if (empty($this->getRemainingTTL($key))) { - $this->redis->expire($key, $this->getTtlToUse($ttl)); + $this->redis->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl)); } return $value; @@ -36,7 +36,7 @@ public function decrement(string $key, ?int $ttl = null): int|false #[\Override] public function get(string $key): int|float|string|Cacheable|array|null { - $val = $this->redis->get($key); + $val = $this->redis->get($this->getEffectiveKey($key)); if ($val === null) { return null; } @@ -53,7 +53,7 @@ public function get(string $key): int|float|string|Cacheable|array|null public function set(string $key, int|float|string|Cacheable|array $val, ?int $ttl = null): bool { $data = is_array($val) ? $this->serializeValArray($val) : $this->serializeVal($val); - return $this->redis->setex($key, $this->getTtlToUse($ttl), json_encode($data)) !== null; + return $this->redis->setex($this->getEffectiveKey($key), $this->getTtlToUse($ttl), json_encode($data)) !== null; } /** @@ -63,9 +63,9 @@ 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->redis->incr($key); + $value = $this->redis->incr($this->getEffectiveKey($key)); if (empty($this->getRemainingTTL($key))) { - $this->redis->expire($key, $this->getTtlToUse($ttl)); + $this->redis->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl)); } return $value; @@ -78,7 +78,7 @@ public function increment(string $key, ?int $ttl = null): int|false #[Override] public function clear(string $key): bool { - return (bool) $this->redis->del($key); + return (bool) $this->redis->del($this->getEffectiveKey($key)); } /** @@ -98,7 +98,7 @@ public function clearAllCache(): bool #[Override] public function getRemainingTTL(string $key): ?int { - $ttl = $this->redis->ttl($key); + $ttl = $this->redis->ttl($this->getEffectiveKey($key)); return $ttl !== false ? $ttl : null; } @@ -119,6 +119,7 @@ protected function __construct(int $ttl, array $configuration = []) $this->redis->connect($configuration['server_address'], $configuration['port'] ?? 6379, $configuration['timeout'] ?? 3); } } + $this->prefixKey = $configuration['key_prefix'] ?? ''; } /** @@ -161,8 +162,17 @@ protected function assertConfig(array $configuration): void } } + /** + * manages keys by adding the prefix set during configuration + * @param string $key cache key + * @return string key to be used + */ + private function getEffectiveKey(string $key): string + { + return $this->prefixKey . $key; + } private readonly \Redis $redis; - + private readonly string $prefixKey; private array $mandatoryKeys = [ 'server_address' ]; diff --git a/tests/MemcacheCacheTest.php b/tests/MemcacheCacheTest.php index 395e8cd..8a146c3 100644 --- a/tests/MemcacheCacheTest.php +++ b/tests/MemcacheCacheTest.php @@ -152,6 +152,17 @@ public function testEnum(): void $this->doTestRealEnum(CacheEnum::MEMCACHE); } + public function testPrefix(): 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->assertEquals($cacheSamePrefix->get($key), $val); + $this->assertNull($cacheOtherPrefix->get($key)); + } + #[\Override] public static function tearDownAfterClass(): void { diff --git a/tests/PRedisCacheTest.php b/tests/PRedisCacheTest.php index 81f584c..ebfe98c 100644 --- a/tests/PRedisCacheTest.php +++ b/tests/PRedisCacheTest.php @@ -155,6 +155,17 @@ public function testMissingInstance(): void $this->expectException(CacheMissingConfigurationException::class); Cache::factory(CacheEnum::PREDIS, 60, ['instance' => 5]); } + + public function testPrefix(): 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->assertEquals($cacheSamePrefix->get($key), $val); + $this->assertNull($cacheOtherPrefix->get($key)); + } #[\Override] public static function tearDownAfterClass(): void diff --git a/tests/RedisCacheTest.php b/tests/RedisCacheTest.php index 2d79ca2..074fe40 100644 --- a/tests/RedisCacheTest.php +++ b/tests/RedisCacheTest.php @@ -153,6 +153,17 @@ public function testMissingInstance(): void Cache::factory(CacheEnum::REDIS, 60, ['instance' => 5]); } + public function testPrefix(): 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->assertEquals($cacheSamePrefix->get($key), $val); + $this->assertNull($cacheOtherPrefix->get($key)); + } + #[\Override] public static function tearDownAfterClass(): void {