@@ -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 ];
0 commit comments