_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 Memcache(); 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 Memcached server'; CRM_Utils_System::civiExit(); } } /** * @param $key * @param $value * @param null|int|\DateInterval $ttl * * @return bool */ 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, $value, FALSE, $this->_timeout)) { return FALSE; } return TRUE; } /** * @param $key * @param mixed $default * * @return mixed */ public function get($key, $default = NULL) { if ($default !== NULL) { throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); } $result = $this->_cache->get($this->_prefix . $key); return $result; } /** * @param $key * * @return mixed */ public function delete($key) { return $this->_cache->delete($this->_prefix . $key); } /** * @return bool */ public function flush() { // FIXME: Only delete items matching `$this->_prefix`. return $this->_cache->flush(); } }