_host = $config['host']; } if (isset($config['port'])) { $this->_port = $config['port']; } if (isset($config['timeout'])) { $this->_timeout = $config['timeout']; } if (isset($config['prefix'])) { $this->_prefix = $config['prefix']; } $this->_cache = new Redis(); if (!$this->_cache->connect($this->_host, $this->_port)) { // dont use fatal here since we can go in an infinite loop echo 'Could not connect to redisd server'; CRM_Utils_System::civiExit(); } if (CRM_Utils_Constant::value('CIVICRM_DB_CACHE_PASSWORD')) { $this->_cache->auth(CIVICRM_DB_CACHE_PASSWORD); } } /** * @param $key * @param $value * * @return bool * @throws Exception */ public function set($key, &$value) { if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) { if (PHP_SAPI === 'cli' || (Civi\Core\Container::isContainerBooted() && CRM_Core_Permission::check('view debug output'))) { CRM_Core_Error::fatal("Redis set ($key) failed: " . $this->_cache->getLastError()); } else { Civi::log()->error("Redis set ($key) failed: " . $this->_cache->getLastError()); CRM_Core_Error::fatal("Redis set ($key) failed"); } return FALSE; } return TRUE; } /** * @param $key * @param mixed $default * * @return mixed */ public function get($key, $default = NULL) { $result = $this->_cache->get($this->_prefix . $key); return ($result === FALSE) ? $default : unserialize($result); } /** * @param $key * * @return mixed */ public function delete($key) { return $this->_cache->delete($this->_prefix . $key); } /** * @return mixed */ public function flush() { // FIXME: Ideally, we'd map each prefix to a different 'hash' object in Redis, // and this would be simpler. However, that needs to go in tandem with a // more general rethink of cache expiration/TTL. $keys = $this->_cache->keys($this->_prefix . '*'); return $this->_cache->del($keys); } }