|
14 | 14 | use Predis\Connection\Aggregate\ClusterInterface;
|
15 | 15 | use Predis\Connection\Aggregate\PredisCluster;
|
16 | 16 | use Predis\Response\Status;
|
| 17 | +use Symfony\Component\Cache\CacheItem; |
17 | 18 | use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
18 | 19 | use Symfony\Component\Cache\Marshaller\DeflateMarshaller;
|
19 | 20 | use Symfony\Component\Cache\Marshaller\MarshallerInterface;
|
@@ -58,6 +59,11 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter
|
58 | 59 | */
|
59 | 60 | private const DEFAULT_CACHE_TTL = 8640000;
|
60 | 61 |
|
| 62 | + /** |
| 63 | + * @var string|null detected eviction policy used on Redis server |
| 64 | + */ |
| 65 | + private $redisEvictionPolicy; |
| 66 | + |
61 | 67 | /**
|
62 | 68 | * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient The redis client
|
63 | 69 | * @param string $namespace The default namespace
|
@@ -87,6 +93,13 @@ public function __construct($redisClient, string $namespace = '', int $defaultLi
|
87 | 93 | */
|
88 | 94 | protected function doSave(array $values, ?int $lifetime, array $addTagData = [], array $delTagData = []): array
|
89 | 95 | {
|
| 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 | + |
90 | 103 | // serialize values
|
91 | 104 | if (!$serialized = $this->marshaller->marshall($values, $failed)) {
|
92 | 105 | return $failed;
|
@@ -260,4 +273,20 @@ private function renameKeys($redis, array $ids): array
|
260 | 273 |
|
261 | 274 | return $newIds;
|
262 | 275 | }
|
| 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 | + } |
263 | 292 | }
|
0 commit comments