diff --git a/src/Cache/PhpRedis.php b/src/Cache/PhpRedis.php index a683075..b345085 100644 --- a/src/Cache/PhpRedis.php +++ b/src/Cache/PhpRedis.php @@ -5,7 +5,6 @@ namespace Drupal\redis\Cache; use Drupal\Component\Serialization\SerializationInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheTagsChecksumInterface; -use Drupal\Core\Cache\ChainedFastBackend; /** * PhpRedis cache backend. @@ -50,14 +49,31 @@ class PhpRedis extends CacheBase { // Optimize for the common case when only a single cache entry needs to // be fetched, no pipeline is needed then. if (count($keys) > 1) { - $pipe = $this->client->multi(); - foreach ($keys as $key) { - $pipe->hgetall($key); + try { + $pipe = $this->client->multi(); + foreach ($keys as $key) { + $pipe->hgetall($key); + } + $result = $pipe->exec(); + } catch (\RedisException $e) { + static $error_logged = false; + if (!$error_logged) { + \Drupal::logger('redis')->error($e->getMessage()); + $error_logged = true; + } + return []; + } + } else { + try { + $result = [$this->client->hGetAll(reset($keys))]; + } catch (\RedisException $e) { + static $error_logged = false; + if (!$error_logged) { + \Drupal::logger('redis')->error($e->getMessage()); + $error_logged = true; + } + return []; } - $result = $pipe->exec(); - } - else { - $result = [$this->client->hGetAll(reset($keys))]; } // Loop over the cid values to ensure numeric indexes. diff --git a/src/Cache/RedisCacheTagsChecksum.php b/src/Cache/RedisCacheTagsChecksum.php index 1cb8e02..f21bef7 100644 --- a/src/Cache/RedisCacheTagsChecksum.php +++ b/src/Cache/RedisCacheTagsChecksum.php @@ -83,23 +83,36 @@ class RedisCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTagsInv } /** - * {@inheritdoc} - */ - protected function getTagInvalidationCounts(array $tags) { - // Do not use MGET for a single key as Relay does not support in-memory - // caching for MGET misses. - if (count($tags) == 1) { - $tag = reset($tags); - return [$tag => (int) $this->client->get($this->getTagKey($tag))]; - } + * {@inheritdoc} + */ +/** + * {@inheritdoc} + */ +protected function getTagInvalidationCounts(array $tags) { + try { + // Do not use MGET for a single key as Relay does not support in-memory + // caching for MGET misses. + if (count($tags) == 1) { + $tag = reset($tags); + $value = $this->client->get($this->getTagKey($tag)); + return [$tag => (int) ($value !== false ? $value : PHP_INT_MAX)]; + } - $keys = array_map([$this, 'getTagKey'], $tags); - // The mget command returns the values as an array with numeric keys, - // combine it with the tags array to get the expected return value and run - // it through intval() to convert to integers and FALSE to 0. - $values = $this->client->mget($keys); - return $values ? array_map('intval', array_combine($tags, $values)) : []; + $keys = array_map([$this, 'getTagKey'], $tags); + // The mget command returns the values as an array with numeric keys, + // combine it with the tags array to get the expected return value and run + // it through intval() to convert to integers and FALSE to PHP_INT_MAX. + $values = $this->client->mget($keys); + return $values ? array_map(function ($value) { + return (int) ($value !== false ? $value : PHP_INT_MAX); + }, array_combine($tags, $values)) : []; + } catch (\Exception $e) { + // Handle the exception appropriately, e.g., log the error, return a default value, etc. + \Drupal::logger('redis')->error('Error retrieving tag invalidation counts: ' . $e->getMessage()); + return array_fill_keys($tags, PHP_INT_MAX); } +} + /** * Return the key for the given cache tag. diff --git a/src/Client/PhpRedis.php b/src/Client/PhpRedis.php index 7fe1141..2d6eaf2 100644 --- a/src/Client/PhpRedis.php +++ b/src/Client/PhpRedis.php @@ -19,25 +19,41 @@ class PhpRedis implements ClientInterface { // Sentinel mode, get the real master. if (is_array($host)) { - $ip_host = $this->askForMaster($client, $host, $password); - if (is_array($ip_host)) { - list($host, $port) = $ip_host; + try { + $ip_host = $this->askForMaster($client, $host, $password); + if (is_array($ip_host)) { + list($host, $port) = $ip_host; + } + } catch (\Exception $e) { + throw new \Exception('Error getting master in sentinel mode: ' . $e->getMessage()); } } - if ($persistent) { - $client->pconnect($host, $port); - } - else { - $client->connect($host, $port); + try { + if ($persistent) { + $client->pconnect($host, $port); + } + else { + $client->connect($host, $port); + } + } catch (\Exception $e) { + throw new \Exception('Error connecting to Redis: ' . $e->getMessage()); } - if (isset($password)) { - $client->auth($password); + try { + if (isset($password)) { + $client->auth($password); + } + } catch (\Exception $e) { + throw new \Exception('Error authenticating to Redis: ' . $e->getMessage()); } - if (isset($base)) { - $client->select($base); + try { + if (isset($base)) { + $client->select($base); + } + } catch (\Exception $e) { + throw new \Exception('Error selecting Redis database: ' . $e->getMessage()); } // Do not allow PhpRedis serialize itself data, we are going to do it