_cache = array(); } /** * @param $key * * @return string */ public function fileName($key) { if (strlen($key) > 50) { return CIVICRM_TEMPLATE_COMPILEDIR . "CRM_" . md5($key) . ".php"; } return CIVICRM_TEMPLATE_COMPILEDIR . $key . ".php"; } /** * @param string $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"); } if (array_key_exists($key, $this->_cache)) { return $this->_cache[$key]; } if (!file_exists($this->fileName($key))) { return; } $this->_cache[$key] = unserialize(substr(file_get_contents($this->fileName($key)), 8)); return $this->_cache[$key]; } /** * @param string $key * @param mixed $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 (file_exists($this->fileName($key))) { return FALSE; // WTF, write-once cache?! } $this->_cache[$key] = $value; $bytes = file_put_contents($this->fileName($key), "fileName($key))) { unlink($this->fileName($key)); } unset($this->_cache[$key]); return TRUE; } /** * @param null $key * @return bool */ public function flush($key = NULL) { $prefix = "CRM_"; if (!$handle = opendir(CIVICRM_TEMPLATE_COMPILEDIR)) { return FALSE; // die? Error? } while (FALSE !== ($entry = readdir($handle))) { if (substr($entry, 0, 4) == $prefix) { unlink(CIVICRM_TEMPLATE_COMPILEDIR . $entry); } } closedir($handle); unset($this->_cache); $this->_cache = array(); return TRUE; } public function clear() { return $this->flush(); } }