Skip to content

Commit 4d978e8

Browse files
fix(memcached): Handle custom handler for a memcached tagaware adpater
1 parent 01f008d commit 4d978e8

File tree

6 files changed

+67
-32
lines changed

6 files changed

+67
-32
lines changed

scripts/refresh-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Instantiate the "PhpFilesAdapter" cache adapter
1616
$cacheAdapter = new TagAwareAdapter(new Symfony\Component\Cache\Adapter\PhpFilesAdapter('', 0, $cachePath));
17-
// 0Or Redis: $cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://your-redis-host:6379'));
17+
// Or Redis: $cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://your-redis-host:6379'));
1818
// Or Memcached: $cacheAdapter = new TagAwareAdapter(new MemcachedAdapter(MemcachedAdapter::createConnection
1919
//('memcached://your-memcached-host:11211')));
2020
// Parse argument

src/ApiCache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
use Psr\Cache\InvalidArgumentException;
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Cache\Adapter\AbstractAdapter;
16-
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
1716
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1817
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1918
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
2019
use Symfony\Component\Cache\PruneableInterface;
20+
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
2121

2222
/**
2323
* The cache mechanism to store every decision from LAPI/CAPI. Symfony Cache component powered.
@@ -826,7 +826,7 @@ public function prune(): bool
826826
*/
827827
private function setCustomErrorHandler(): void
828828
{
829-
if ($this->adapter instanceof MemcachedAdapter) {
829+
if ($this->adapter instanceof MemcachedTagAwareAdapter) {
830830
set_error_handler(function ($errno, $errstr) {
831831
throw new BouncerException("Error when connecting to Memcached. (Error level: $errno) Please fix the Memcached DSN or select another cache technology. Original message was: $errstr");
832832
});
@@ -838,7 +838,7 @@ private function setCustomErrorHandler(): void
838838
* */
839839
private function unsetCustomErrorHandler(): void
840840
{
841-
if ($this->adapter instanceof MemcachedAdapter) {
841+
if ($this->adapter instanceof MemcachedTagAwareAdapter) {
842842
restore_error_handler();
843843
}
844844
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace CrowdSecBouncer\Fixes\Memcached;
4+
5+
use Symfony\Component\Cache\Adapter\TagAwareAdapter as SymfonyTagAwareAdapter;
6+
// This class is used only to know explicitly that we instantiiate a Memcached adapter
7+
class TagAwareAdapter extends SymfonyTagAwareAdapter
8+
{
9+
10+
}

src/StandaloneBounce.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use IPLib\Factory;
88
use Symfony\Component\Cache\Adapter\AbstractAdapter;
99
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
10+
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
1011
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1112
use Symfony\Component\Cache\Adapter\RedisAdapter;
1213
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
@@ -92,7 +93,7 @@ private function getCacheAdapterInstance(bool $forceReload = false): TagAwareAda
9293
throw new BouncerException('The selected cache technology is Memcached.'.' Please set a Memcached DSN or select another cache technology.');
9394
}
9495

95-
$this->cacheAdapter = new TagAwareAdapter(
96+
$this->cacheAdapter = new MemcachedTagAwareAdapter(
9697
new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedDsn)));
9798
break;
9899

tests/IpVerificationTest.php

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,30 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapter, $origCac
6060
$bouncer = new Bouncer(null, $this->logger, $apiCache);
6161
$bouncer->configure($bouncerConfig);
6262

63-
if (in_array($origCacheName, ['PhpFilesAdapter', 'MemcachedAdapter'])) {
64-
$this->assertEquals(
65-
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
66-
get_class($cacheAdapter),
67-
'Tested adapter should be correct'
68-
);
69-
} elseif ('RedisAdapter' == $origCacheName) {
70-
$this->assertEquals(
71-
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
72-
get_class($cacheAdapter),
73-
'Tested adapter should be correct'
74-
);
63+
switch ($origCacheName) {
64+
case 'PhpFilesAdapter':
65+
$this->assertEquals(
66+
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
67+
get_class($cacheAdapter),
68+
'Tested adapter should be correct'
69+
);
70+
break;
71+
case 'MemcachedAdapter':
72+
$this->assertEquals(
73+
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
74+
get_class($cacheAdapter),
75+
'Tested adapter should be correct'
76+
);
77+
break;
78+
case 'RedisAdapter':
79+
$this->assertEquals(
80+
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
81+
get_class($cacheAdapter),
82+
'Tested adapter should be correct'
83+
);
84+
break;
85+
default:
86+
break;
7587
}
7688

7789
// At the end of test, we should have exactly 3 "cache miss")
@@ -167,20 +179,31 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapter, $origC
167179
$bouncer = new Bouncer(null, $this->logger, $apiCache);
168180
$bouncer->configure($bouncerConfig);
169181

170-
if (in_array($origCacheName, ['PhpFilesAdapter', 'MemcachedAdapter'])) {
171-
$this->assertEquals(
172-
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
173-
get_class($cacheAdapter),
174-
'Tested adapter should be correct'
175-
);
176-
} elseif ('RedisAdapter' == $origCacheName) {
177-
$this->assertEquals(
178-
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
179-
get_class($cacheAdapter),
180-
'Tested adapter should be correct'
181-
);
182+
switch ($origCacheName) {
183+
case 'PhpFilesAdapter':
184+
$this->assertEquals(
185+
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
186+
get_class($cacheAdapter),
187+
'Tested adapter should be correct'
188+
);
189+
break;
190+
case 'MemcachedAdapter':
191+
$this->assertEquals(
192+
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
193+
get_class($cacheAdapter),
194+
'Tested adapter should be correct'
195+
);
196+
break;
197+
case 'RedisAdapter':
198+
$this->assertEquals(
199+
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
200+
get_class($cacheAdapter),
201+
'Tested adapter should be correct'
202+
);
203+
break;
204+
default:
205+
break;
182206
}
183-
184207
// As we are in stream mode, no live call should be done to the API.
185208

186209
/** @var MockObject $apiClientMock */
@@ -269,7 +292,7 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapter, $origC
269292
$this->assertEquals(
270293
'ban',
271294
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP),
272-
'The cache warm up should be stored across each instanciation'
295+
'The cache warm up should be stored across each instantiation'
273296
);
274297

275298
$this->logger->info('', ['message' => 'set "Large IPV4 range banned" + "IPV6 range banned" state']);

tests/TestHelpers.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Monolog\Handler\StreamHandler;
77
use Monolog\Logger;
88
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
9+
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
910
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1011
use Symfony\Component\Cache\Adapter\RedisAdapter;
1112
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
@@ -56,7 +57,7 @@ public static function cacheAdapterProvider(): array
5657

5758
/** @var string */
5859
$memcachedCacheAdapterDsn = getenv('MEMCACHED_DSN');
59-
$memcachedAdapter = new TagAwareAdapter(
60+
$memcachedAdapter = new MemcachedTagAwareAdapter(
6061
new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedCacheAdapterDsn)));
6162

6263
return [

0 commit comments

Comments
 (0)