_cache = []; $this->_expires = []; } /** * @param string $key * @param mixed $value * @param null|int|\DateInterval $ttl * @return bool * @throws \Psr\SimpleCache\InvalidArgumentException */ public function set($key, $value, $ttl = NULL) { CRM_Utils_Cache::assertValidKey($key); $this->_cache[$key] = $this->reobjectify($value); $this->_expires[$key] = CRM_Utils_Date::convertCacheTtlToExpires($ttl, self::DEFAULT_TIMEOUT); return TRUE; } /** * @param string $key * @param mixed $default * * @return mixed * @throws \Psr\SimpleCache\InvalidArgumentException */ public function get($key, $default = NULL) { CRM_Utils_Cache::assertValidKey($key); if (isset($this->_expires[$key]) && is_numeric($this->_expires[$key]) && $this->_expires[$key] <= time()) { return $default; } if (array_key_exists($key, $this->_cache)) { return $this->reobjectify($this->_cache[$key]); } return $default; } /** * @param string $key * @return bool * @throws \Psr\SimpleCache\InvalidArgumentException */ public function delete($key) { CRM_Utils_Cache::assertValidKey($key); unset($this->_cache[$key]); unset($this->_expires[$key]); return TRUE; } public function flush() { unset($this->_cache); unset($this->_expires); $this->_cache = []; return TRUE; } public function clear() { return $this->flush(); } private function reobjectify($value) { if (is_object($value)) { return unserialize(serialize($value)); } if (is_array($value)) { foreach ($value as $p) { if (is_object($p)) { return unserialize(serialize($value)); } } } return $value; } /** * @param string $key * @return int|null */ public function getExpires($key) { return $this->_expires[$key] ?: NULL; } }