8000 [Cache] fix error handling when using Redis by nicolas-grekas · Pull Request #45339 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] fix error handling when using Redis #45339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ public function commit()
$retry = $this->deferred = [];

if ($expiredIds) {
$this->doDelete($expiredIds);
try {
$this->doDelete($expiredIds);
} catch (\Exception $e) {
$ok = false;
CacheItem::log($this->logger, 'Failed to delete expired items: '.$e->getMessage(), ['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
}
}
foreach ($byLifetime as $lifetime => $values) {
try {
Expand Down
15 changes: 12 additions & 3 deletions src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ public function commit(): bool

if ($expiredIds) {
// Tags are not cleaned up in this case, however that is done on invalidateTags().
$this->doDelete($expiredIds);
try {
$this->doDelete($expiredIds);
} catch (\Exception $e) {
$ok = false;
CacheItem::log($this->logger, 'Failed to delete expired items: '.$e->getMessage(), ['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
}
}
foreach ($byLifetime as $lifetime => $values) {
try {
Expand Down Expand Up @@ -295,8 +300,12 @@ public function invalidateTags(array $tags)
$tagIds[] = $this->getId(self::TAGS_PREFIX.$tag);
}

if ($this->doInvalidate($tagIds)) {
return true;
try {
if ($this->doInvalidate($tagIds)) {
return true;
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to invalidate tags: '.$e->getMessage(), ['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
}

return false;
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Predis\Connection\Aggregate\ClusterInterface;
use Predis\Connection\Aggregate\PredisCluster;
use Predis\Connection\Aggregate\ReplicationInterface;
use Predis\Response\ErrorInterface;
use Predis\Response\Status;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -58,6 +59,7 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter
* @var string|null detected eviction policy used on Redis server
*/
private $redisEvictionPolicy;
private $namespace;

/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis The redis client
Expand All @@ -81,6 +83,7 @@ public function __construct($redis, string $namespace = '', int $defaultLifetime
}

$this->init($redis, $namespace, $defaultLifetime, new TagAwareMarshaller($marshaller));
$this->namespace = $namespace;
}

/**
Expand Down Expand Up @@ -160,7 +163,7 @@ protected function doDeleteYieldTags(array $ids): iterable
});

foreach ($results as $id => $result) {
if ($result instanceof \RedisException) {
if ($result instanceof \RedisException || $result instanceof ErrorInterface) {
CacheItem::log($this->logger, 'Failed to delete key "{key}": '.$result->getMessage(), ['key' => substr($id, \strlen($this->namespace)), 'exception' => $result]);

continue;
Expand Down Expand Up @@ -254,7 +257,7 @@ protected function doInvalidate(array $tagIds): bool

$success = true;
foreach ($results as $id => $values) {
if ($values instanceof \RedisException) {
if ($values instanceof \RedisException || $values instanceof ErrorInterface) {
CacheItem::log($this->logger, 'Failed to invalidate key "{key}": '.$values->getMessage(), ['key' => substr($id, \strlen($this->namespace)), 'exception' => $values]);
$success = false;

Expand Down Expand Up @@ -303,6 +306,11 @@ private function getRedisEvictionPolicy(): string

foreach ($hosts as $host) {
$info = $host->info('Memory');

if ($info instanceof ErrorInterface) {
continue;
}

$info = $info['Memory'] ?? $info;

return $this->redisEvictionPolicy = $info['maxmemory_policy'];
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Predis\Connection\Aggregate\ClusterInterface;
use Predis\Connection\Aggregate\RedisCluster;
use Predis\Connection\Aggregate\ReplicationInterface;
use Predis\Response\ErrorInterface;
use Predis\Response\Status;
use Symfony\Component\Cache\Exception\CacheException;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -386,7 +387,7 @@ protected function doClear($namespace)
}

$info = $host->info('Server');
$info = $info['Server'] ?? $info;
$info = !$info instanceof ErrorInterface ? $info['Server'] ?? $info : ['redis_version' => '2.0'];

if (!$host instanceof \Predis\ClientInterface) {
$prefix = \defined('Redis::SCAN_PREFIX') && (\Redis::SCAN_PREFIX & $host->getOption(\Redis::OPT_SCAN)) ? '' : $host->getOption(\Redis::OPT_PREFIX);
Expand Down
0