From 11f0c3aaa098ef1a52e246aa11dd208e900735e5 Mon Sep 17 00:00:00 2001 From: Stiliyan Ivanov Date: Mon, 2 Nov 2026 03:03:23 +0200 Subject: [PATCH] fix: Enhance Redis caching to support scalars cached as plain strings --- .../libraries/Cache/drivers/Cache_redis.php | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php index 2dd68e6deb..d691c253b3 100644 --- a/system/libraries/Cache/drivers/Cache_redis.php +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -162,16 +162,16 @@ public function __construct() public function get($key) { $data = $this->_redis->hMGet($key, array('__ci_type', '__ci_value')); - - if ($this->_redis->sIsMember('_ci_redis_serialized', $key)) + // Also serves scalars that 3.1.x cached as plain strings + if ($data === FALSE) { - return FALSE; + return $this->_get_v31($key); } - // Also serves scalars that 3.1.x cached as plain strings - if ($data === FALSE) + // Key doesn't exist (hMGet returns an array of FALSE values) + if ( ! isset($data['__ci_type'], $data['__ci_value']) OR $data['__ci_value'] === FALSE) { - return $this->_redis->get($key); + return FALSE; } switch ($data['__ci_type']) @@ -295,14 +295,15 @@ public function decrement($id, $offset = 1) */ protected function _increment($id, $offset) { - $value = $this->_redis->incrBy($id, $offset); - - if ($value === FALSE) + // Also serves scalars that 3.1.x cached as plain strings, incrBy creates the key if not existing + if ($this->_redis->type($id) === Redis::REDIS_STRING) { - $value = $this->_redis->hIncrBy($id, '__ci_value', $offset); - $value === FALSE OR $this->_redis->hSet($id, '__ci_type', 'integer'); + return $this->_redis->incrBy($id, $offset); } + $value = $this->_redis->hIncrBy($id, '__ci_value', $offset); + $value === FALSE OR $this->_redis->hSet($id, '__ci_type', 'integer'); + return $value; } @@ -386,4 +387,24 @@ public function __destruct() $this->_redis->close(); } } + + // ------------------------------------------------------------------------ + + /** + * Get scalars that 3.1.x cached as plain strings + * + * @param string $key Cache ID + * @return mixed + */ + private function _get_v31($key) + { + $value = $this->_redis->get($key); + + if ($value !== FALSE && $this->_redis->sIsMember('_ci_redis_serialized', $key)) + { + return unserialize($value); + } + + return $value; + } }