Skip to content

Commit 16667cf

Browse files
authored
Merge pull request #34 from DamImpr/difference_redis_predis
redis cache with phpredis or predis
2 parents 1e8eb4c + 2e357b9 commit 16667cf

10 files changed

Lines changed: 372 additions & 39 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
- [2026-01-09] DamImpr: Redis and memcache cache, instance passed in construction [#32](https://github.com/DamImpr/cache-multi-layer/pull/32)
1919

2020
- [2026-01-09] DamImpr: Instance configuration [#33](https://github.com/DamImpr/cache-multi-layer/pull/33)
21+
22+
- [2026-01-12] DamImpr: redis cache with phpredis or predis [#34](https://github.com/DamImpr/cache-multi-layer/pull/34)

src/Enum/CacheEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ enum CacheEnum: int
1111
case APCU = 1;
1212
case REDIS = 2;
1313
case MEMCACHE = 3;
14+
case PREDIS = 4;
1415
}

src/Exception/CacheMissingConfigurationException.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,4 @@
1212
*/
1313
class CacheMissingConfigurationException extends Exception
1414
{
15-
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
16-
{
17-
parent::__construct($message, $code, $previous);
18-
}
1915
}

src/Service/Cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public static function factory(CacheEnum $cacheEnum, int $ttl, array $configurat
117117
return match ($cacheEnum) {
118118
CacheEnum::APCU => new ApcuCache($ttl, $configuration),
119119
CacheEnum::REDIS => new RedisCache($ttl, $configuration),
120+
CacheEnum::PREDIS => new PRedisCache($ttl, $configuration),
120121
CacheEnum::MEMCACHE => new MemcacheCache($ttl, $configuration)
121122
};
122123
}

src/Service/CacheManagerImplDryMode.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
*/
1313
class CacheManagerImplDryMode extends CacheManagerImpl
1414
{
15-
protected function __construct(?CacheConfiguration $cacheConfiguration = null)
16-
{
17-
parent::__construct($cacheConfiguration);
18-
}
19-
2015
#[\Override]
2116
public function appendCache(Cache $cache): bool
2217
{

src/Service/MemcacheCache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ protected function __construct(int $ttl, array $configuration = [])
172172
} else {
173173
$this->memcache = new Memcache();
174174
$port = $configuration['port'] ?? 11211;
175-
if (array_key_exists('persistent', $configuration)) {
175+
if (array_key_exists('persistent', $configuration) && $configuration['persistent']) {
176176
$resultConnection = $this->memcache->pconnect($configuration['server_address'], $port);
177177
} else {
178178
$resultConnection = $this->memcache->connect($configuration['server_address'], $port);
@@ -182,6 +182,7 @@ protected function __construct(int $ttl, array $configuration = [])
182182
throw new Exception("Connection not found");
183183
}
184184
}
185+
185186
$this->compress = array_key_exists('compress', $configuration) && $configuration['compress'];
186187
}
187188

@@ -194,8 +195,11 @@ protected function assertConfig(array $configuration): void
194195
throw new CacheMissingConfigurationException("instance must be " . Memcache::class . " class");
195196
}
196197
}
198+
197199
private readonly Memcache $memcache;
200+
198201
private readonly bool $compress;
202+
199203
private array $mandatoryKeys = [
200204
'server_address'
201205
];

src/Service/PRedisCache.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
namespace CacheMultiLayer\Service;
4+
5+
use CacheMultiLayer\Enum\CacheEnum;
6+
use CacheMultiLayer\Exception\CacheMissingConfigurationException;
7+
use CacheMultiLayer\Interface\Cacheable;
8+
use Override;
9+
use Predis\Client as PredisClient;
10+
11+
/**
12+
*
13+
* PREDIS cache implementation
14+
* @author Damiano Improta <code@damianoimprota.dev>
15+
*/
16+
class PRedisCache extends Cache
17+
{
18+
/**
19+
*
20+
* {@InheritDoc}
21+
*/
22+
#[\Override]
23+
public function decrement(string $key, ?int $ttl = null): int|false
24+
{
25+
$value = $this->predisClient->decr($key);
26+
if (empty($this->getRemainingTTL($key))) {
27+
$this->predisClient->expire($key, $this->getTtlToUse($ttl));
28+
}
29+
30+
return $value;
31+
}
32+
33+
/**
34+
*
35+
* {@InheritDoc}
36+
*/
37+
#[\Override]
38+
public function get(string $key): int|float|string|Cacheable|array|null
39+
{
40+
$val = $this->predisClient->get($key);
41+
if ($val === null) {
42+
return null;
43+
}
44+
45+
$valDecoded = json_decode($val, true);
46+
return is_array($valDecoded) ? $this->unserializeVal($valDecoded) : $valDecoded;
47+
}
48+
49+
/**
50+
*
51+
* {@InheritDoc}
52+
*/
53+
#[\Override]
54+
public function set(string $key, int|float|string|Cacheable|array $val, ?int $ttl = null): bool
55+
{
56+
$data = is_array($val) ? $this->serializeValArray($val) : $this->serializeVal($val);
57+
return $this->predisClient->setex($key, $this->getTtlToUse($ttl), json_encode($data)) !== null;
58+
}
59+
60+
/**
61+
*
62+
* {@InheritDoc}
63+
*/
64+
#[Override]
65+
public function increment(string $key, ?int $ttl = null): int|false
66+
{
67+
$value = $this->predisClient->incr($key);
68+
if (empty($this->getRemainingTTL($key))) {
69+
$this->predisClient->expire($key, $this->getTtlToUse($ttl));
70+
}
71+
72+
return $value;
73+
}
74+
75+
/**
76+
*
77+
* {@inheritDoc}
78+
*/
79+
#[Override]
80+
public function clear(string $key): bool
81+
{
82+
return (bool) $this->predisClient->del($key);
83+
}
84+
85+
/**
86+
*
87+
* {@inheritDoc}
88+
*/
89+
#[Override]
90+
public function clearAllCache(): bool
91+
{
92+
return $this->predisClient->flushall() !== null;
93+
}
94+
95+
/**
96+
*
97+
* {@InheritDoc}
98+
*/
99+
#[Override]
100+
public function getRemainingTTL(string $key): ?int
101+
{
102+
$ttl = $this->predisClient->ttl($key);
103+
return $ttl !== false ? $ttl : null;
104+
}
105+
106+
/**
107+
*
108+
* {@InheritDoc}
109+
*/
110+
protected function __construct(int $ttl, array $configuration = [])
111+
{
112+
parent::__construct($ttl, $configuration);
113+
if (array_key_exists('instance', $configuration)) {
114+
$this->predisClient = $configuration['instance'];
115+
} else {
116+
$this->predisClient = new PredisClient([
117+
'scheme' => $configuration['tcp'] ?? 'tcp',
118+
'host' => $configuration['server_address'],
119+
'port' => $configuration['port'] ?? 6379,
120+
'password' => $configuration['password'] ?? '',
121+
'database' => $configuration['database'] ?? 0,
122+
'persistent' => $configuration['persistent'] ?? false,
123+
'conn_uid' => $configuration['connection_id'] ?? ''
124+
]);
125+
}
126+
}
127+
128+
/**
129+
*
130+
* {@InheritDoc}
131+
*/
132+
#[\Override]
133+
public function isConnected(): bool
134+
{
135+
return $this->predisClient->ping() !== null;
136+
}
137+
138+
/**
139+
*
140+
* {@InheritDoc}
141+
*/
142+
#[\Override]
143+
public function getEnum(): CacheEnum
144+
{
145+
return CacheEnum::PREDIS;
146+
}
147+
148+
/**
149+
*
150+
* {@InheritDoc}
151+
*/
152+
#[\Override]
153+
protected function getMandatoryConfig(): array
154+
{
155+
return $this->mandatoryKeys;
156+
}
157+
158+
#[\Override]
159+
protected function assertConfig(array $configuration): void
160+
{
161+
if (!array_key_exists('instance', $configuration) || $configuration['instance'] instanceof PredisClient) {
162+
parent::assertConfig($configuration);
163+
} elseif (array_key_exists('instance', $configuration)) {
164+
throw new CacheMissingConfigurationException("instance must be " . PredisClient::class . " class");
165+
}
166+
}
167+
168+
private readonly PredisClient $predisClient;
169+
170+
private array $mandatoryKeys = [
171+
'server_address'
172+
];
173+
}

src/Service/RedisCache.php

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use CacheMultiLayer\Exception\CacheMissingConfigurationException;
77
use CacheMultiLayer\Interface\Cacheable;
88
use Override;
9-
use Predis\Client as PredisClient;
109

1110
/**
1211
*
@@ -22,9 +21,9 @@ class RedisCache extends Cache
2221
#[\Override]
2322
public function decrement(string $key, ?int $ttl = null): int|false
2423
{
25-
$value = $this->predisClient->decr($key);
24+
$value = $this->redis->decr($key);
2625
if (empty($this->getRemainingTTL($key))) {
27-
$this->predisClient->expire($key, $this->getTtlToUse($ttl));
26+
$this->redis->expire($key, $this->getTtlToUse($ttl));
2827
}
2928

3029
return $value;
@@ -37,12 +36,12 @@ public function decrement(string $key, ?int $ttl = null): int|false
3736
#[\Override]
3837
public function get(string $key): int|float|string|Cacheable|array|null
3938
{
40-
$val = $this->predisClient->get($key);
39+
$val = $this->redis->get($key);
4140
if ($val === null) {
4241
return null;
4342
}
4443

45-
$valDecoded = json_decode($val, true);
44+
$valDecoded = json_decode((string) $val, true);
4645
return is_array($valDecoded) ? $this->unserializeVal($valDecoded) : $valDecoded;
4746
}
4847

@@ -54,7 +53,7 @@ public function get(string $key): int|float|string|Cacheable|array|null
5453
public function set(string $key, int|float|string|Cacheable|array $val, ?int $ttl = null): bool
5554
{
5655
$data = is_array($val) ? $this->serializeValArray($val) : $this->serializeVal($val);
57-
return $this->predisClient->setex($key, $this->getTtlToUse($ttl), json_encode($data)) !== null;
56+
return $this->redis->setex($key, $this->getTtlToUse($ttl), json_encode($data)) !== null;
5857
}
5958

6059
/**
@@ -64,9 +63,9 @@ public function set(string $key, int|float|string|Cacheable|array $val, ?int $tt
6463
#[Override]
6564
public function increment(string $key, ?int $ttl = null): int|false
6665
{
67-
$value = $this->predisClient->incr($key);
66+
$value = $this->redis->incr($key);
6867
if (empty($this->getRemainingTTL($key))) {
69-
$this->predisClient->expire($key, $this->getTtlToUse($ttl));
68+
$this->redis->expire($key, $this->getTtlToUse($ttl));
7069
}
7170

7271
return $value;
@@ -79,7 +78,7 @@ public function increment(string $key, ?int $ttl = null): int|false
7978
#[Override]
8079
public function clear(string $key): bool
8180
{
82-
return (bool) $this->predisClient->del($key);
81+
return (bool) $this->redis->del($key);
8382
}
8483

8584
/**
@@ -89,7 +88,7 @@ public function clear(string $key): bool
8988
#[Override]
9089
public function clearAllCache(): bool
9190
{
92-
return $this->predisClient->flushall() !== null;
91+
return $this->redis->flushall() !== null;
9392
}
9493

9594
/**
@@ -99,7 +98,7 @@ public function clearAllCache(): bool
9998
#[Override]
10099
public function getRemainingTTL(string $key): ?int
101100
{
102-
$ttl = $this->predisClient->ttl($key);
101+
$ttl = $this->redis->ttl($key);
103102
return $ttl !== false ? $ttl : null;
104103
}
105104

@@ -111,16 +110,14 @@ protected function __construct(int $ttl, array $configuration = [])
111110
{
112111
parent::__construct($ttl, $configuration);
113112
if (array_key_exists('instance', $configuration)) {
114-
$this->predisClient = $configuration['instance'];
113+
$this->redis = $configuration['instance'];
115114
} else {
116-
$this->predisClient = new PredisClient([
117-
'scheme' => $configuration['tcp'] ?? 'tcp',
118-
'host' => $configuration['server_address'],
119-
'port' => $configuration['port'] ?? 6379,
120-
'password' => $configuration['password'] ?? '',
121-
'database' => $configuration['database'] ?? 0,
122-
'persistent' => $configuration['persistent'] ?? false
123-
]);
115+
$this->redis = new \Redis();
116+
if (array_key_exists('persistent', $configuration) && $configuration['persistent']) {
117+
$this->redis->connect($configuration['server_address'], $configuration['port'] ?? 6379, $configuration['timeout'] ?? 3, $configuration['connection_id'] ?? 'app_redis_connection');
118+
} else {
119+
$this->redis->connect($configuration['server_address'], $configuration['port'] ?? 6379, $configuration['timeout'] ?? 3);
120+
}
124121
}
125122
}
126123

@@ -131,7 +128,7 @@ protected function __construct(int $ttl, array $configuration = [])
131128
#[\Override]
132129
public function isConnected(): bool
133130
{
134-
return $this->predisClient->ping() !== null;
131+
return $this->redis->ping() !== null;
135132
}
136133

137134
/**
@@ -157,13 +154,15 @@ protected function getMandatoryConfig(): array
157154
#[\Override]
158155
protected function assertConfig(array $configuration): void
159156
{
160-
if (!array_key_exists('instance', $configuration) || $configuration['instance'] instanceof PredisClient) {
157+
if (!array_key_exists('instance', $configuration) || $configuration['instance'] instanceof \Redis) {
161158
parent::assertConfig($configuration);
162159
} elseif (array_key_exists('instance', $configuration)) {
163-
throw new CacheMissingConfigurationException("instance must be " . PredisClient::class . " class");
160+
throw new CacheMissingConfigurationException("instance must be " . \Redis::class . " class");
164161
}
165162
}
166-
private readonly PredisClient $predisClient;
163+
164+
private readonly \Redis $redis;
165+
167166
private array $mandatoryKeys = [
168167
'server_address'
169168
];

0 commit comments

Comments
 (0)