66use CacheMultiLayer \Exception \CacheMissingConfigurationException ;
77use CacheMultiLayer \Interface \Cacheable ;
88use 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