X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FCache%2FRedis.php;h=01fc538c9355cd38af7dd5b35bbd5a3ae4c2dcaf;hb=9f70b0e4bc774d25e5bcff3de677e35d2808b646;hp=6a1bb482be20c9546dacdcae1e437ff7af188115;hpb=4e991547de8ccf4aef81b4579521f4bb26022978;p=civicrm-core.git diff --git a/CRM/Utils/Cache/Redis.php b/CRM/Utils/Cache/Redis.php index 6a1bb482be..01fc538c93 100644 --- a/CRM/Utils/Cache/Redis.php +++ b/CRM/Utils/Cache/Redis.php @@ -1,7 +1,7 @@ _cache->auth(CIVICRM_DB_CACHE_PASSWORD); + if (CRM_Utils_Constant::value('CIVICRM_DB_CACHE_PASSWORD')) { + $this->_cache->auth(CIVICRM_DB_CACHE_PASSWORD); + } } /** * @param $key * @param $value + * @param null|int|\DateInterval $ttl * * @return bool * @throws Exception */ - public function set($key, &$value) { + public function set($key, $value, $ttl = NULL) { + if ($ttl !== NULL) { + throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL"); + } if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) { - CRM_Core_Error::fatal("Redis set failed, wondering why?, $key", $value); + 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; @@ -125,28 +141,40 @@ class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface { /** * @param $key + * @param mixed $default * * @return mixed */ - public function get($key) { + public function get($key, $default = NULL) { $result = $this->_cache->get($this->_prefix . $key); - return unserialize($result); + return ($result === FALSE) ? $default : unserialize($result); } /** * @param $key * - * @return mixed + * @return bool */ public function delete($key) { - return $this->_cache->delete($this->_prefix . $key); + $this->_cache->delete($this->_prefix . $key); + return TRUE; } /** - * @return mixed + * @return bool */ public function flush() { - return $this->_cache->flushDB(); + // 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 . '*'); + $this->_cache->del($keys); + return TRUE; + } + + public function clear() { + return $this->flush(); } }