Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 12 additions & 6 deletions commands
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 19 additions & 11 deletions src/Service/MemcacheCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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);
}

/**
Expand All @@ -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'];
}

Expand All @@ -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'
];
Expand Down
27 changes: 19 additions & 8 deletions src/Service/PRedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

/**
Expand All @@ -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;
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -123,6 +123,7 @@ protected function __construct(int $ttl, array $configuration = [])
'conn_uid' => $configuration['connection_id'] ?? ''
]);
}
$this->prefixKey = $configuration['key_prefix'] ?? '';
}

/**
Expand Down Expand Up @@ -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'
];
Expand Down
28 changes: 19 additions & 9 deletions src/Service/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

/**
Expand All @@ -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;
Expand All @@ -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));
}

/**
Expand All @@ -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;
}

Expand All @@ -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'] ?? '';
}

/**
Expand Down Expand Up @@ -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'
];
Expand Down
11 changes: 11 additions & 0 deletions tests/MemcacheCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
11 changes: 11 additions & 0 deletions tests/PRedisCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/RedisCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down