From 53e195a70cf4ecea711022259c4adadc42fb18d2 Mon Sep 17 00:00:00 2001 From: Damiano Improta Date: Fri, 16 Jan 2026 09:59:25 +0100 Subject: [PATCH 1/3] prefix key --- commands | 18 ++++++++++++------ src/Service/MemcacheCache.php | 33 +++++++++++++++++++++------------ src/Service/PRedisCache.php | 27 +++++++++++++++++++-------- src/Service/RedisCache.php | 31 +++++++++++++++++++++---------- tests/MemcacheCacheTest.php | 11 +++++++++++ tests/PRedisCacheTest.php | 11 +++++++++++ tests/RedisCacheTest.php | 11 +++++++++++ 7 files changed, 106 insertions(+), 36 deletions(-) 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..705c16f 100644 --- a/src/Service/MemcacheCache.php +++ b/src/Service/MemcacheCache.php @@ -15,6 +15,7 @@ */ class MemcacheCache extends Cache { + /** * * {@inheritDoc} @@ -32,7 +33,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 +53,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 +66,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 +77,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 +103,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 +118,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 +131,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 +158,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 +183,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,11 +197,19 @@ 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' ]; -} +} \ No newline at end of file diff --git a/src/Service/PRedisCache.php b/src/Service/PRedisCache.php index f42a663..bb67638 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 } } + + /** + * 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..0ecacaf 100644 --- a/src/Service/RedisCache.php +++ b/src/Service/RedisCache.php @@ -14,6 +14,7 @@ */ class RedisCache extends Cache { + /** * * {@InheritDoc} @@ -21,9 +22,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 +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->redis->get($key); + $val = $this->redis->get($this->getEffectiveKey($key)); if ($val === null) { return null; } @@ -53,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->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 +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->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 +79,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 +99,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 +120,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,9 +163,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 \Redis $redis; - + private readonly string $prefixKey; private array $mandatoryKeys = [ 'server_address' ]; -} +} \ No newline at end of file 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 { From c3446427eb14400dd35322051b07dbcca64206da Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 16 Jan 2026 09:01:22 +0000 Subject: [PATCH 2/3] chore: update CHANGELOG for PR #35 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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) From 0aea1ba2be287519c034d68f0e774114d95d07a9 Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 16 Jan 2026 09:02:33 +0000 Subject: [PATCH 3/3] style: Fix PHP code style issues --- src/Service/MemcacheCache.php | 3 +-- src/Service/PRedisCache.php | 2 +- src/Service/RedisCache.php | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Service/MemcacheCache.php b/src/Service/MemcacheCache.php index 705c16f..6cc6ade 100644 --- a/src/Service/MemcacheCache.php +++ b/src/Service/MemcacheCache.php @@ -15,7 +15,6 @@ */ class MemcacheCache extends Cache { - /** * * {@inheritDoc} @@ -212,4 +211,4 @@ private function getEffectiveKey(string $key): string private array $mandatoryKeys = [ 'server_address' ]; -} \ No newline at end of file +} diff --git a/src/Service/PRedisCache.php b/src/Service/PRedisCache.php index bb67638..7b998f3 100644 --- a/src/Service/PRedisCache.php +++ b/src/Service/PRedisCache.php @@ -166,7 +166,7 @@ protected function assertConfig(array $configuration): void } } - + /** * manages keys by adding the prefix set during configuration * @param string $key cache key diff --git a/src/Service/RedisCache.php b/src/Service/RedisCache.php index 0ecacaf..902c543 100644 --- a/src/Service/RedisCache.php +++ b/src/Service/RedisCache.php @@ -14,7 +14,6 @@ */ class RedisCache extends Cache { - /** * * {@InheritDoc} @@ -177,4 +176,4 @@ private function getEffectiveKey(string $key): string private array $mandatoryKeys = [ 'server_address' ]; -} \ No newline at end of file +}