44
55namespace CrowdSecBouncer ;
66
7- use CrowdSecBouncer \Fixes \Memcached \TagAwareAdapter as MemcachedTagAwareAdapter ;
87use DateTime ;
98use ErrorException ;
109use Exception ;
1110use Psr \Cache \InvalidArgumentException ;
1211use Psr \Log \LoggerInterface ;
12+ use Symfony \Component \Cache \Adapter \AdapterInterface ;
1313use Symfony \Component \Cache \Adapter \MemcachedAdapter ;
1414use Symfony \Component \Cache \Adapter \PhpFilesAdapter ;
1515use Symfony \Component \Cache \Adapter \RedisAdapter ;
3131abstract class AbstractCache
3232{
3333 public const CACHE_SEP = '_ ' ;
34- /** @var TagAwareAdapter|MemcachedTagAwareAdapter|RedisTagAwareAdapter */
34+ /** @var AdapterInterface */
3535 protected $ adapter ;
3636 /**
3737 * @var ApiClient
@@ -125,9 +125,9 @@ public function __construct(
125125 }
126126
127127 /**
128- * @return TagAwareAdapterInterface
128+ * @return AdapterInterface
129129 */
130- public function getAdapter (): TagAwareAdapterInterface
130+ public function getAdapter (): AdapterInterface
131131 {
132132 return $ this ->adapter ;
133133 }
@@ -290,7 +290,9 @@ protected function addRemediationToCacheItem(
290290
291291 $ item ->set ($ prioritizedRemediations );
292292 $ item ->expiresAt (new DateTime ('@ ' . $ maxLifetime ));
293- $ item ->tag (Constants::CACHE_TAG_REM );
293+ if ($ this ->adapter instanceof TagAwareAdapterInterface) {
294+ $ item ->tag (Constants::CACHE_TAG_REM );
295+ }
294296
295297 // Save the cache without committing it to the cache system.
296298 // Useful to improve performance when updating the cache.
@@ -457,7 +459,7 @@ protected function removeDecisionFromRemediationItem(string $cacheKey, int $deci
457459 'type ' => 'CACHE_ITEM_REMOVED ' ,
458460 'cache_key ' => $ cacheKey ,
459461 ]);
460- $ this ->adapter ->delete (base64_encode ($ cacheKey ));
462+ $ this ->adapter ->deleteItem (base64_encode ($ cacheKey ));
461463
462464 return true ;
463465 }
@@ -466,7 +468,9 @@ protected function removeDecisionFromRemediationItem(string $cacheKey, int $deci
466468 $ cacheContent = Remediation::sortRemediationByPriority ($ remediations );
467469 $ item ->expiresAt (new DateTime ('@ ' . $ maxLifetime ));
468470 $ item ->set ($ cacheContent );
469- $ item ->tag (Constants::CACHE_TAG_REM );
471+ if ($ this ->adapter instanceof TagAwareAdapterInterface) {
472+ $ item ->tag (Constants::CACHE_TAG_REM );
473+ }
470474
471475 // Save the cache without committing it to the cache system.
472476 // Useful to improve performance when updating the cache.
@@ -497,7 +501,9 @@ protected function saveCacheItem(string $cacheTag, string $cacheKey, $cachedVari
497501 $ item = $ this ->adapter ->getItem (base64_encode ($ cacheKey ));
498502 $ item ->set ($ cachedVariables );
499503 $ item ->expiresAt (new DateTime ("+ $ duration seconds " ));
500- $ item ->tag ($ cacheTag );
504+ if ($ this ->adapter instanceof TagAwareAdapterInterface) {
505+ $ item ->tag ($ cacheTag );
506+ }
501507 $ this ->adapter ->save ($ item );
502508 }
503509
@@ -508,7 +514,7 @@ protected function saveCacheItem(string $cacheTag, string $cacheKey, $cachedVari
508514 */
509515 protected function setCustomErrorHandler (): void
510516 {
511- if ($ this ->adapter instanceof MemcachedTagAwareAdapter ) {
517+ if ($ this ->adapter instanceof MemcachedAdapter ) {
512518 set_error_handler (function ($ errno , $ errstr ) {
513519 $ message = "Error when connecting to Memcached. (Error level: $ errno) " .
514520 "Please fix the Memcached DSN or select another cache technology. " .
@@ -523,7 +529,7 @@ protected function setCustomErrorHandler(): void
523529 * */
524530 protected function unsetCustomErrorHandler (): void
525531 {
526- if ($ this ->adapter instanceof MemcachedTagAwareAdapter ) {
532+ if ($ this ->adapter instanceof MemcachedAdapter ) {
527533 restore_error_handler ();
528534 }
529535 }
@@ -552,10 +558,11 @@ private function configureAdapter(): void
552558 throw new BouncerException ('The selected cache technology is Memcached. ' .
553559 ' Please set a Memcached DSN or select another cache technology. ' );
554560 }
555-
556- $ this ->adapter = new MemcachedTagAwareAdapter (
557- new MemcachedAdapter (MemcachedAdapter::createConnection ($ memcachedDsn ))
558- );
561+ /**
562+ * Using a MemcachedAdapter with a TagAwareAdapter for storing tags is discouraged.
563+ * @see \Symfony\Component\Cache\Adapter\MemcachedAdapter::__construct comment
564+ */
565+ $ this ->adapter = new MemcachedAdapter (MemcachedAdapter::createConnection ($ memcachedDsn ));
559566 break ;
560567
561568 case Constants::CACHE_SYSTEM_REDIS :
0 commit comments