: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://opensource.org/licenses/mit-license.php MIT License
namespace Cake\Cache\Engine;
use Cake\Cache\CacheEngine;
* Redis storage engine for cache.
class RedisEngine extends CacheEngine
* The default config used unless overridden by runtime configuration
* - `database` database number to use for connection.
* - `duration` Specify how long items in this cache configuration last.
* - `groups` List of groups or 'tags' associated to every key stored in this config.
* handy for deleting a complete group from cache.
* - `password` Redis server password.
* - `persistent` Connect to the Redis server with a persistent connection
* - `port` port number to the Redis server.
* - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace
* with either another cache config or another application.
* - `probability` Probability of hitting a cache gc cleanup. Setting to 0 will disable
* cache::gc from ever being called automatically.
* - `server` URL or ip to the Redis server host.
* - `timeout` timeout in seconds (float).
* - `unix_socket` Path to the unix socket file (default: false)
protected $_defaultConfig = [
* Initialize the Cache Engine
* Called automatically by the cache frontend
* @param array $config array of setting for the engine
* @return bool True if the engine has been successfully initialized, false if not
public function init(array $config = [])
if (!extension_loaded('redis')) {
if (!empty($config['host'])) {
$config['server'] = $config['host'];
return $this->_connect();
* Connects to a Redis server
* @return bool True if Redis server was connected
protected function _connect()
$this->_Redis = new Redis();
if (!empty($this->_config['unix_socket'])) {
$return = $this->_Redis->connect($this->_config['unix_socket']);
} elseif (empty($this->_config['persistent'])) {
$return = $this->_Redis->connect($this->_config['server'], $this->_config['port'], $this->_config['timeout']);
$persistentId = $this->_config['port'] . $this->_config['timeout'] . $this->_config['database'];
$return = $this->_Redis->pconnect($this->_config['server'], $this->_config['port'], $this->_config['timeout'], $persistentId);
} catch (RedisException $e) {
if ($return && $this->_config['password']) {
$return = $this->_Redis->auth($this->_config['password']);
$return = $this->_Redis->select($this->_config['database']);
* Write data for key into cache.
* @param string $key Identifier for the data
* @param mixed $value Data to be cached
* @return bool True if the data was successfully cached, false on failure
public function write($key, $value)
$key = $this->_key($key);
$value = serialize($value);
$duration = $this->_config['duration'];
return $this->_Redis->set($key, $value);
return $this->_Redis->setEx($key, $duration, $value);
* Read a key from the cache
* @param string $key Identifier for the data
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
public function read($key)
$key = $this->_key($key);
$value = $this->_Redis->get($key);
if (preg_match('/^[-]?\d+$/', $value)) {
if ($value !== false && is_string($value)) {
return unserialize($value);
* Increments the value of an integer cached key & update the expiry time
* @param string $key Identifier for the data
* @param int $offset How much to increment
* @return int|false New incremented value, false otherwise
public function increment($key, $offset = 1)
$duration = $this->_config['duration'];
$key = $this->_key($key);
$value = (int)$this->_Redis->incrBy($key, $offset);
$this->_Redis->expire($key, $duration);
* Decrements the value of an integer cached key & update the expiry time
* @param string $key Identifier for the data
* @param int $offset How much to subtract
* @return int|false New decremented value, false otherwise
public function decrement($key, $offset = 1)
$duration = $this->_config['duration'];
$key = $this->_key($key);
$value = (int)$this->_Redis->decrBy($key, $offset);
$this->_Redis->expire($key, $duration);
* Delete a key from the cache
* @param string $key Identifier for the data
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
public function delete($key)
$key = $this->_key($key);
return $this->_Redis->del($key) > 0;
* Delete all keys from the cache
* @param bool $check If true will check expiration, otherwise delete all.
* @return bool True if the cache was successfully cleared, false otherwise
public function clear($check)
$this->_Redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$pattern = $this->_config['prefix'] . '*';
$keys = $this->_Redis->scan($iterator, $pattern);
foreach ($keys as $key) {
$isDeleted = ($this->_Redis->del($key) > 0);
$isAllDeleted = $isAllDeleted && $isDeleted;
* Write data for key into cache if it doesn't exist already.
* If it already exists, it fails and returns false.
* @param string $key Identifier for the data.
* @param mixed $value Data to be cached.
* @return bool True if the data was successfully cached, false on failure.
* @link https://github.com/phpredis/phpredis#set
public function add($key, $value)
$duration = $this->_config['duration'];
$key = $this->_key($key);
$value = serialize($value);
if ($this->_Redis->set($key, $value, ['nx', 'ex' => $duration])) {
* Returns the `group value` for each of the configured groups
* If the group initial value was not found, then it initializes
foreach ($this->_config['groups'] as $group) {
$value = $this->_Redis->get($this->_config['prefix'] . $group);
$this->_Redis->set($this->_config['prefix'] . $group, $value);
$result[] = $group . $value;
* Increments the group value to simulate deletion of all keys under a group
* old values will remain in storage until they expire.
* @param string $group name of the group to be cleared
public function clearGroup($group)
return (bool)$this->_Redis->incr($this->_config['prefix'] . $group);
* Disconnects from the redis server
public function __destruct()
if (empty($this->_config['persistent']) && $this->_Redis instanceof Redis) {