Skip to content

Commit e77f6de

Browse files
andreromnicolas-grekas
authored andcommitted
[Cache] Redis Tag Aware warn on wrong eviction policy
1 parent 1374abd commit e77f6de

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Predis\Connection\Aggregate\ClusterInterface;
1515
use Predis\Connection\Aggregate\PredisCluster;
1616
use Predis\Response\Status;
17+
use Symfony\Component\Cache\CacheItem;
1718
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1819
use Symfony\Component\Cache\Marshaller\DeflateMarshaller;
1920
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
@@ -58,6 +59,11 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter
5859
*/
5960
private const DEFAULT_CACHE_TTL = 8640000;
6061

62+
/**
63+
* @var string|null detected eviction policy used on Redis server
64+
*/
65+
private $redisEvictionPolicy;
66+
6167
/**
6268
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient The redis client
6369
* @param string $namespace The default namespace
@@ -87,6 +93,13 @@ public function __construct($redisClient, string $namespace = '', int $defaultLi
8793
*/
8894
protected function doSave(array $values, ?int $lifetime, array $addTagData = [], array $delTagData = []): array
8995
{
96+
$eviction = $this->getRedisEvictionPolicy();
97+
if ('noeviction' !== $eviction && 0 !== strpos($eviction, 'volatile-')) {
98+
CacheItem::log($this->logger, sprintf('Redis maxmemory-policy setting "%s" is *not* supported by RedisTagAwareAdapter, use "noeviction" or "volatile-*" eviction policies', $eviction));
99+
100+
return false;
101+
}
102+
90103
// serialize values
91104
if (!$serialized = $this->marshaller->marshall($values, $failed)) {
92105
return $failed;
@@ -260,4 +273,20 @@ private function renameKeys($redis, array $ids): array
260273

261274
return $newIds;
262275
}
276+
277+
private function getRedisEvictionPolicy(): string
278+
{
279+
if (null !== $this->redisEvictionPolicy) {
280+
return $this->redisEvictionPolicy;
281+
}
282+
283+
foreach ($this->getHosts() as $host) {
284+
$info = $host->info('Memory');
285+
$info = isset($info['Memory']) ? $info['Memory'] : $info;
286+
287+
return $this->redisEvictionPolicy = $info['maxmemory_policy'];
288+
}
289+
290+
return $this->redisEvictionPolicy = '';
291+
}
263292
}

0 commit comments

Comments
 (0)