Skip to content

Commit 909668c

Browse files
Merge pull request #112 from julienloizelet/feat/no-tag-for-memcached
Feat/no tag for memcached
2 parents 742769d + 19c0b86 commit 909668c

File tree

5 files changed

+24
-35
lines changed

5 files changed

+24
-35
lines changed

src/AbstractCache.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace CrowdSecBouncer;
66

7-
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
87
use DateTime;
98
use ErrorException;
109
use Exception;
1110
use Psr\Cache\InvalidArgumentException;
1211
use Psr\Log\LoggerInterface;
12+
use Symfony\Component\Cache\Adapter\AdapterInterface;
1313
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
1414
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1515
use Symfony\Component\Cache\Adapter\RedisAdapter;
@@ -31,7 +31,7 @@
3131
abstract 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:

src/Bouncer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Monolog\Logger;
1515
use Psr\Cache\InvalidArgumentException;
1616
use Psr\Log\LoggerInterface;
17-
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
17+
use Symfony\Component\Cache\Adapter\AdapterInterface;
1818
use Symfony\Component\Cache\Exception\CacheException;
1919
use Symfony\Component\Config\Definition\Processor;
2020

@@ -150,7 +150,7 @@ public function getApiCache(): ApiCache
150150
return $this->apiCache;
151151
}
152152

153-
public function getCacheAdapter(): TagAwareAdapterInterface
153+
public function getCacheAdapter(): AdapterInterface
154154
{
155155
return $this->getApiCache()->getAdapter();
156156
}

src/Fixes/Memcached/TagAwareAdapter.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/Integration/IpVerificationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private function cacheAdapterCheck($cacheAdapter, $origCacheName)
4848
break;
4949
case 'MemcachedAdapter':
5050
$this->assertEquals(
51-
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
51+
'Symfony\Component\Cache\Adapter\MemcachedAdapter',
5252
get_class($cacheAdapter),
5353
'Tested adapter should be correct'
5454
);

tests/Integration/TestHelpers.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@
55
namespace CrowdSecBouncer\Tests\Integration;
66

77
use CrowdSecBouncer\Constants;
8-
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
98
use Monolog\Formatter\LineFormatter;
109
use Monolog\Handler\StreamHandler;
1110
use Monolog\Logger;
12-
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
13-
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
14-
use Symfony\Component\Cache\Adapter\RedisAdapter;
15-
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
16-
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1711
use Symfony\Component\Cache\Exception\CacheException;
1812

1913
class TestHelpers

0 commit comments

Comments
 (0)