Skip to content

Commit 0e5de14

Browse files
authored
Merge pull request #35 from DamImpr/prefix
prefix key
2 parents ea1c497 + 0aea1ba commit 0e5de14

8 files changed

Lines changed: 104 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@
2020
- [2026-01-09] DamImpr: Instance configuration [#33](https://github.com/DamImpr/cache-multi-layer/pull/33)
2121

2222
- [2026-01-12] DamImpr: redis cache with phpredis or predis [#34](https://github.com/DamImpr/cache-multi-layer/pull/34)
23+
24+
- [2026-01-16] DamImpr: prefix key [#35](https://github.com/DamImpr/cache-multi-layer/pull/35)

commands

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,29 @@ case "$1" in
55
for i in 8.2 8.3 8.4
66
do
77
PHP_VERSION=$i docker compose build
8-
docker compose run --rm --remove-orphans app test-sw && docker compose down
8+
docker compose run --rm --remove-orphans app test-sw
9+
docker compose down
910
done
1011
;;
1112
'update-vendor')
12-
docker compose run --rm --remove-orphans app update-vendor && docker compose down
13+
docker compose run --rm --remove-orphans app update-vendor
14+
docker compose down
1315
;;
1416
'php-cs-fixer')
15-
docker compose run --rm --remove-orphans app php-cs-fixer && docker compose down
17+
docker compose run --rm --remove-orphans app php-cs-fixer
18+
docker compose down
1619
;;
1720
'rector')
18-
docker compose run --rm --remove-orphans app rector && docker compose down
21+
docker compose run --rm --remove-orphans app rector
22+
docker compose down
1923
;;
2024
'phpstan')
21-
docker compose run --rm --remove-orphans app phpstan && docker compose down
25+
docker compose run --rm --remove-orphans app phpstan
26+
docker compose down
2227
;;
2328
'sh')
24-
docker compose run --rm --remove-orphans app sh && docker compose down
29+
docker compose run --rm --remove-orphans app sh
30+
docker compose down
2531
;;
2632
*)
2733
echo "comands allowed"

src/Service/MemcacheCache.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function getMandatoryConfig(): array
3232
#[Override]
3333
public function clear(string $key): bool
3434
{
35-
return $this->memcache->delete($key);
35+
return $this->memcache->delete($this->getEffectiveKey($key));
3636
}
3737

3838
/**
@@ -52,7 +52,7 @@ public function clearAllCache(): bool
5252
#[Override]
5353
public function decrement(string $key, ?int $ttl = null): int|false
5454
{
55-
$pair = $this->memcache->get($key);
55+
$pair = $this->memcache->get($this->getEffectiveKey($key));
5656
if (empty($pair)) {
5757
$this->set($key, -1, $ttl);
5858
return -1;
@@ -65,7 +65,7 @@ public function decrement(string $key, ?int $ttl = null): int|false
6565

6666
--$value;
6767
$pair['data'] = $value;
68-
$this->memcache->set($key, $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key));
68+
$this->memcache->set($this->getEffectiveKey($key), $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key));
6969
return $value;
7070
}
7171

@@ -76,7 +76,7 @@ public function decrement(string $key, ?int $ttl = null): int|false
7676
#[Override]
7777
public function get(string $key): int|float|string|Cacheable|array|null
7878
{
79-
$val = $this->memcache->get($key);
79+
$val = $this->memcache->get($this->getEffectiveKey($key));
8080
if (empty($val)) {
8181
return null;
8282
}
@@ -102,7 +102,7 @@ public function getEnum(): CacheEnum
102102
#[Override]
103103
public function getRemainingTTL(string $key): ?int
104104
{
105-
$val = $this->memcache->get($key);
105+
$val = $this->memcache->get($this->getEffectiveKey($key));
106106
if (empty($val)) {
107107
return null;
108108
}
@@ -117,7 +117,7 @@ public function getRemainingTTL(string $key): ?int
117117
#[Override]
118118
public function increment(string $key, ?int $ttl = null): int|false
119119
{
120-
$pair = $this->memcache->get($key);
120+
$pair = $this->memcache->get($this->getEffectiveKey($key));
121121
if (empty($pair)) {
122122
$this->set($key, 1, $ttl);
123123
return 1;
@@ -130,7 +130,7 @@ public function increment(string $key, ?int $ttl = null): int|false
130130

131131
++$value;
132132
$pair['data'] = $value;
133-
$this->memcache->set($key, $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key));
133+
$this->memcache->set($this->getEffectiveKey($key), $pair, $this->compress ? MEMCACHE_COMPRESSED : 0, $this->getRemainingTTL($key));
134134
return $value;
135135
}
136136

@@ -157,7 +157,7 @@ public function set(string $key, int|float|string|Cacheable|array $val, ?int $tt
157157
'data' => json_encode($values)
158158
, 'exipres_at' => time() + $ttlToUse
159159
];
160-
return $this->memcache->set($key, $dataToStore, $this->compress ? MEMCACHE_COMPRESSED : 0, $ttlToUse);
160+
return $this->memcache->set($this->getEffectiveKey($key), $dataToStore, $this->compress ? MEMCACHE_COMPRESSED : 0, $ttlToUse);
161161
}
162162

163163
/**
@@ -182,7 +182,7 @@ protected function __construct(int $ttl, array $configuration = [])
182182
throw new Exception("Connection not found");
183183
}
184184
}
185-
185+
$this->prefixKey = $configuration['key_prefix'] ?? '';
186186
$this->compress = array_key_exists('compress', $configuration) && $configuration['compress'];
187187
}
188188

@@ -196,10 +196,18 @@ protected function assertConfig(array $configuration): void
196196
}
197197
}
198198

199+
/**
200+
* manages keys by adding the prefix set during configuration
201+
* @param string $key cache key
202+
* @return string key to be used
203+
*/
204+
private function getEffectiveKey(string $key): string
205+
{
206+
return $this->prefixKey . $key;
207+
}
199208
private readonly Memcache $memcache;
200-
209+
private readonly string $prefixKey;
201210
private readonly bool $compress;
202-
203211
private array $mandatoryKeys = [
204212
'server_address'
205213
];

src/Service/PRedisCache.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class PRedisCache extends Cache
2222
#[\Override]
2323
public function decrement(string $key, ?int $ttl = null): int|false
2424
{
25-
$value = $this->predisClient->decr($key);
25+
$value = $this->predisClient->decr($this->getEffectiveKey($key));
2626
if (empty($this->getRemainingTTL($key))) {
27-
$this->predisClient->expire($key, $this->getTtlToUse($ttl));
27+
$this->predisClient->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl));
2828
}
2929

3030
return $value;
@@ -37,7 +37,7 @@ public function decrement(string $key, ?int $ttl = null): int|false
3737
#[\Override]
3838
public function get(string $key): int|float|string|Cacheable|array|null
3939
{
40-
$val = $this->predisClient->get($key);
40+
$val = $this->predisClient->get($this->getEffectiveKey($key));
4141
if ($val === null) {
4242
return null;
4343
}
@@ -54,7 +54,7 @@ public function get(string $key): int|float|string|Cacheable|array|null
5454
public function set(string $key, int|float|string|Cacheable|array $val, ?int $ttl = null): bool
5555
{
5656
$data = is_array($val) ? $this->serializeValArray($val) : $this->serializeVal($val);
57-
return $this->predisClient->setex($key, $this->getTtlToUse($ttl), json_encode($data)) !== null;
57+
return $this->predisClient->setex($this->getEffectiveKey($key), $this->getTtlToUse($ttl), json_encode($data)) !== null;
5858
}
5959

6060
/**
@@ -64,9 +64,9 @@ public function set(string $key, int|float|string|Cacheable|array $val, ?int $tt
6464
#[Override]
6565
public function increment(string $key, ?int $ttl = null): int|false
6666
{
67-
$value = $this->predisClient->incr($key);
67+
$value = $this->predisClient->incr($this->getEffectiveKey($key));
6868
if (empty($this->getRemainingTTL($key))) {
69-
$this->predisClient->expire($key, $this->getTtlToUse($ttl));
69+
$this->predisClient->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl));
7070
}
7171

7272
return $value;
@@ -79,7 +79,7 @@ public function increment(string $key, ?int $ttl = null): int|false
7979
#[Override]
8080
public function clear(string $key): bool
8181
{
82-
return (bool) $this->predisClient->del($key);
82+
return (bool) $this->predisClient->del($this->getEffectiveKey($key));
8383
}
8484

8585
/**
@@ -123,6 +123,7 @@ protected function __construct(int $ttl, array $configuration = [])
123123
'conn_uid' => $configuration['connection_id'] ?? ''
124124
]);
125125
}
126+
$this->prefixKey = $configuration['key_prefix'] ?? '';
126127
}
127128

128129
/**
@@ -165,8 +166,18 @@ protected function assertConfig(array $configuration): void
165166
}
166167
}
167168

168-
private readonly PredisClient $predisClient;
169169

170+
/**
171+
* manages keys by adding the prefix set during configuration
172+
* @param string $key cache key
173+
* @return string key to be used
174+
*/
175+
private function getEffectiveKey(string $key): string
176+
{
177+
return $this->prefixKey . $key;
178+
}
179+
private readonly PredisClient $predisClient;
180+
private readonly string $prefixKey;
170181
private array $mandatoryKeys = [
171182
'server_address'
172183
];

src/Service/RedisCache.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class RedisCache extends Cache
2121
#[\Override]
2222
public function decrement(string $key, ?int $ttl = null): int|false
2323
{
24-
$value = $this->redis->decr($key);
24+
$value = $this->redis->decr($this->getEffectiveKey($key));
2525
if (empty($this->getRemainingTTL($key))) {
26-
$this->redis->expire($key, $this->getTtlToUse($ttl));
26+
$this->redis->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl));
2727
}
2828

2929
return $value;
@@ -36,7 +36,7 @@ public function decrement(string $key, ?int $ttl = null): int|false
3636
#[\Override]
3737
public function get(string $key): int|float|string|Cacheable|array|null
3838
{
39-
$val = $this->redis->get($key);
39+
$val = $this->redis->get($this->getEffectiveKey($key));
4040
if ($val === null) {
4141
return null;
4242
}
@@ -53,7 +53,7 @@ public function get(string $key): int|float|string|Cacheable|array|null
5353
public function set(string $key, int|float|string|Cacheable|array $val, ?int $ttl = null): bool
5454
{
5555
$data = is_array($val) ? $this->serializeValArray($val) : $this->serializeVal($val);
56-
return $this->redis->setex($key, $this->getTtlToUse($ttl), json_encode($data)) !== null;
56+
return $this->redis->setex($this->getEffectiveKey($key), $this->getTtlToUse($ttl), json_encode($data)) !== null;
5757
}
5858

5959
/**
@@ -63,9 +63,9 @@ public function set(string $key, int|float|string|Cacheable|array $val, ?int $tt
6363
#[Override]
6464
public function increment(string $key, ?int $ttl = null): int|false
6565
{
66-
$value = $this->redis->incr($key);
66+
$value = $this->redis->incr($this->getEffectiveKey($key));
6767
if (empty($this->getRemainingTTL($key))) {
68-
$this->redis->expire($key, $this->getTtlToUse($ttl));
68+
$this->redis->expire($this->getEffectiveKey($key), $this->getTtlToUse($ttl));
6969
}
7070

7171
return $value;
@@ -78,7 +78,7 @@ public function increment(string $key, ?int $ttl = null): int|false
7878
#[Override]
7979
public function clear(string $key): bool
8080
{
81-
return (bool) $this->redis->del($key);
81+
return (bool) $this->redis->del($this->getEffectiveKey($key));
8282
}
8383

8484
/**
@@ -98,7 +98,7 @@ public function clearAllCache(): bool
9898
#[Override]
9999
public function getRemainingTTL(string $key): ?int
100100
{
101-
$ttl = $this->redis->ttl($key);
101+
$ttl = $this->redis->ttl($this->getEffectiveKey($key));
102102
return $ttl !== false ? $ttl : null;
103103
}
104104

@@ -119,6 +119,7 @@ protected function __construct(int $ttl, array $configuration = [])
119119
$this->redis->connect($configuration['server_address'], $configuration['port'] ?? 6379, $configuration['timeout'] ?? 3);
120120
}
121121
}
122+
$this->prefixKey = $configuration['key_prefix'] ?? '';
122123
}
123124

124125
/**
@@ -161,8 +162,17 @@ protected function assertConfig(array $configuration): void
161162
}
162163
}
163164

165+
/**
166+
* manages keys by adding the prefix set during configuration
167+
* @param string $key cache key
168+
* @return string key to be used
169+
*/
170+
private function getEffectiveKey(string $key): string
171+
{
172+
return $this->prefixKey . $key;
173+
}
164174
private readonly \Redis $redis;
165-
175+
private readonly string $prefixKey;
166176
private array $mandatoryKeys = [
167177
'server_address'
168178
];

tests/MemcacheCacheTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ public function testEnum(): void
152152
$this->doTestRealEnum(CacheEnum::MEMCACHE);
153153
}
154154

155+
public function testPrefix(): void
156+
{
157+
$val = 10; //maradona
158+
$key = "test_prefix";
159+
$cacheSamePrefix = Cache::factory(CacheEnum::MEMCACHE, 60, ['key_prefix' => '','server_address' => 'memcache-server']);
160+
$cacheOtherPrefix = Cache::factory(CacheEnum::MEMCACHE, 10, ['key_prefix' => 'other_','server_address' => 'memcache-server']);
161+
$this->getCache()->set($key, $val);
162+
$this->assertEquals($cacheSamePrefix->get($key), $val);
163+
$this->assertNull($cacheOtherPrefix->get($key));
164+
}
165+
155166
#[\Override]
156167
public static function tearDownAfterClass(): void
157168
{

tests/PRedisCacheTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ public function testMissingInstance(): void
155155
$this->expectException(CacheMissingConfigurationException::class);
156156
Cache::factory(CacheEnum::PREDIS, 60, ['instance' => 5]);
157157
}
158+
159+
public function testPrefix(): void
160+
{
161+
$val = 10; //maradona
162+
$key = "test_prefix";
163+
$cacheSamePrefix = Cache::factory(CacheEnum::PREDIS, 60, ['key_prefix' => '','server_address' => 'redis-server']);
164+
$cacheOtherPrefix = Cache::factory(CacheEnum::PREDIS, 10, ['key_prefix' => 'other_','server_address' => 'redis-server']);
165+
$this->getCache()->set($key, $val);
166+
$this->assertEquals($cacheSamePrefix->get($key), $val);
167+
$this->assertNull($cacheOtherPrefix->get($key));
168+
}
158169

159170
#[\Override]
160171
public static function tearDownAfterClass(): void

tests/RedisCacheTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ public function testMissingInstance(): void
153153
Cache::factory(CacheEnum::REDIS, 60, ['instance' => 5]);
154154
}
155155

156+
public function testPrefix(): void
157+
{
158+
$val = 10; //maradona
159+
$key = "test_prefix";
160+
$cacheSamePrefix = Cache::factory(CacheEnum::REDIS, 60, ['key_prefix' => '','server_address' => 'redis-server']);
161+
$cacheOtherPrefix = Cache::factory(CacheEnum::REDIS, 10, ['key_prefix' => 'other_','server_address' => 'redis-server']);
162+
$this->getCache()->set($key, $val);
163+
$this->assertEquals($cacheSamePrefix->get($key), $val);
164+
$this->assertNull($cacheOtherPrefix->get($key));
165+
}
166+
156167
#[\Override]
157168
public static function tearDownAfterClass(): void
158169
{

0 commit comments

Comments
 (0)