From 6baafde163f58eeef1e0e07f6fad36a559ed3f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Tue, 29 Oct 2019 20:19:10 +0100 Subject: [PATCH 1/4] [Cache] Make RedisTagAwareAdapter throw if Redis does not use supported eviction policies --- .../Cache/Adapter/RedisTagAwareAdapter.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php index d52c08d642c6f..e9316f099f1ec 100644 --- a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php @@ -58,6 +58,11 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter */ private const DEFAULT_CACHE_TTL = 8640000; + /** + * @var string|null Detected eviction policy used on Redis server. + */ + private $redisEvictionPolicy; + /** * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient The redis client * @param string $namespace The default namespace @@ -79,6 +84,11 @@ public function __construct($redisClient, string $namespace = '', int $defaultLi } } + $eviction = $this->getRedisEvictionPolicy(); + if ('noeviction' !== $eviction && strpos($eviction, 'volatile-') !== 0) { + throw new InvalidArgumentException(sprintf('Redis maxmemory_policy setting "%s" is *not* supported by RedisTagAwareAdapter, use one "noeviction" or preferably one of "volatile-" eviction policies', $eviction)); + } + $this->init($redisClient, $namespace, $defaultLifetime, new TagAwareMarshaller($marshaller)); } @@ -260,4 +270,20 @@ private function renameKeys($redis, array $ids): array return $newIds; } + + private function getRedisEvictionPolicy(): string + { + if (null !== $this->redisEvictionPolicy) { + return $this->redisEvictionPolicy; + } + + foreach ($this->getHosts() as $host) { + $info = $host->info('Memory'); + $info = isset($info['Memory']) ? $info['Memory'] : $info; + + return $this->redisEvictionPolicy = $info['maxmemory_policy']; + } + + return $this->redisEvictionPolicy = ''; + } } From 36784d1461b9be9082932df647a0e9b9e1b65506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Tue, 29 Oct 2019 20:25:41 +0100 Subject: [PATCH 2/4] CS --- src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php index e9316f099f1ec..1317451ef68f2 100644 --- a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php @@ -59,7 +59,7 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter private const DEFAULT_CACHE_TTL = 8640000; /** - * @var string|null Detected eviction policy used on Redis server. + * @var string|null detected eviction policy used on Redis server */ private $redisEvictionPolicy; @@ -85,7 +85,7 @@ public function __construct($redisClient, string $namespace = '', int $defaultLi } $eviction = $this->getRedisEvictionPolicy(); - if ('noeviction' !== $eviction && strpos($eviction, 'volatile-') !== 0) { + if ('noeviction' !== $eviction && 0 !== strpos($eviction, 'volatile-')) { throw new InvalidArgumentException(sprintf('Redis maxmemory_policy setting "%s" is *not* supported by RedisTagAwareAdapter, use one "noeviction" or preferably one of "volatile-" eviction policies', $eviction)); } From f39f1da6139285bd062ff51d5646f412ac644ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Fri, 15 Nov 2019 21:42:21 +0100 Subject: [PATCH 3/4] Change to check eviction policy on save --- .../Component/Cache/Adapter/RedisTagAwareAdapter.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php index 1317451ef68f2..9c1b5cbac9db3 100644 --- a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php @@ -14,6 +14,7 @@ use Predis\Connection\Aggregate\ClusterInterface; use Predis\Connection\Aggregate\PredisCluster; use Predis\Response\Status; +use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\Exception\InvalidArgumentException; use Symfony\Component\Cache\Marshaller\DeflateMarshaller; use Symfony\Component\Cache\Marshaller\MarshallerInterface; @@ -84,11 +85,6 @@ public function __construct($redisClient, string $namespace = '', int $defaultLi } } - $eviction = $this->getRedisEvictionPolicy(); - if ('noeviction' !== $eviction && 0 !== strpos($eviction, 'volatile-')) { - throw new InvalidArgumentException(sprintf('Redis maxmemory_policy setting "%s" is *not* supported by RedisTagAwareAdapter, use one "noeviction" or preferably one of "volatile-" eviction policies', $eviction)); - } - $this->init($redisClient, $namespace, $defaultLifetime, new TagAwareMarshaller($marshaller)); } @@ -97,6 +93,12 @@ public function __construct($redisClient, string $namespace = '', int $defaultLi */ protected function doSave(array $values, ?int $lifetime, array $addTagData = [], array $delTagData = []): array { + $eviction = $this->getRedisEvictionPolicy(); + if ('noeviction' !== $eviction && 0 !== strpos($eviction, 'volatile-')) { + CacheItem::log($this->logger, sprintf('Redis maxmemory-policy setting "%s" is *not* supported by RedisTagAwareAdapter, use "noeviction" or "volatile-*" eviction policies', $eviction)); + return false; + } + // serialize values if (!$serialized = $this->marshaller->marshall($values, $failed)) { return $failed; From 304ea8d0371407811fbe5ef8194be5795582fadd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Fri, 15 Nov 2019 21:48:28 +0100 Subject: [PATCH 4/4] CS --- src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php index 9c1b5cbac9db3..1dbce4b0db7d4 100644 --- a/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php @@ -96,6 +96,7 @@ protected function doSave(array $values, ?int $lifetime, array $addTagData = [], $eviction = $this->getRedisEvictionPolicy(); if ('noeviction' !== $eviction && 0 !== strpos($eviction, 'volatile-')) { CacheItem::log($this->logger, sprintf('Redis maxmemory-policy setting "%s" is *not* supported by RedisTagAwareAdapter, use "noeviction" or "volatile-*" eviction policies', $eviction)); + return false; }