/** * Constructor * * @param array $options associative array of options * @throws \Zend\Cache\Exception */ public function __construct(array $options = array()) { if (!function_exists('zend_disk_cache_store')) { Cache\Cache::throwException('Zend_Cache_ZendServer_Disk backend has to be used within Zend Server environment.'); } parent::__construct($options); }
/** * callback for output buffering * (shouldn't really be called manually) * * @param string $data Buffered output * @return string Data to send to browser */ public function _flush($data) { $id = array_pop($this->_idStack); if (is_null($id)) { Cache::throwException('use of _flush() without a start()'); } if ($this->_extension) { $this->save(serialize(array($data, $this->_extension)), $id, $this->_tags); } else { $this->save($data, $id, $this->_tags); } return $data; }
/** * Stop the cache * * @param array $tags Tags array * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime) * @param string $forcedDatas If not null, force written datas with this * @param boolean $echoData If set to true, datas are sent to the browser * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends * @return void */ public function end($tags = array(), $specificLifetime = false, $forcedDatas = null, $echoData = true, $priority = 8) { if ($forcedDatas === null) { $data = ob_get_contents(); ob_end_clean(); } else { $data =& $forcedDatas; } $id = array_pop($this->_idStack); if ($id === null) { Cache::throwException('use of end() without a start()'); } $this->save($data, $id, $tags, $specificLifetime, $priority); if ($echoData) { echo $data; } }
/** * Log a message at the WARN (4) priority. * * @param string $message * @throws \Zend\Cache\Exception * @return void */ protected function _log($message, $priority = 4) { if (!$this->_options['logging']) { return; } if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Log\Logger)) { Cache::throwException('Logging is enabled but logger is not set'); } $logger = $this->_options['logger']; $logger->log($message, $priority); }
/** * Clean some cache records * * Available modes are : * 'all' (default) => remove all cache entries ($tags is not used) * 'old' => unsupported * 'matchingTag' => unsupported * 'notMatchingTag' => unsupported * 'matchingAnyTag' => unsupported * * @param string $mode clean mode * @param array $tags array of tags * @throws \Zend\Cache\Exception * @return boolean true if no problem */ public function clean($mode = Cache\CacheCache\Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case Cache\Cache::CLEANING_MODE_ALL: // Necessary because xcache_clear_cache() need basic authentification $backup = array(); if (isset($_SERVER['PHP_AUTH_USER'])) { $backup['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER']; } if (isset($_SERVER['PHP_AUTH_PW'])) { $backup['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW']; } if ($this->_options['user']) { $_SERVER['PHP_AUTH_USER'] = $this->_options['user']; } if ($this->_options['password']) { $_SERVER['PHP_AUTH_PW'] = $this->_options['password']; } xcache_clear_cache(XC_TYPE_VAR, 0); if (isset($backup['PHP_AUTH_USER'])) { $_SERVER['PHP_AUTH_USER'] = $backup['PHP_AUTH_USER']; $_SERVER['PHP_AUTH_PW'] = $backup['PHP_AUTH_PW']; } return true; break; case Cache\Cache::CLEANING_MODE_OLD: $this->_log("Zend_Cache_Backend_Xcache::clean() : CLEANING_MODE_OLD is unsupported by the Xcache backend"); break; case Cache\Cache::CLEANING_MODE_MATCHING_TAG: case Cache\Cache::CLEANING_MODE_NOT_MATCHING_TAG: case Cache\Cache::CLEANING_MODE_MATCHING_ANY_TAG: $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND); break; default: Cache\Cache::throwException('Invalid mode for clean() method'); break; } }
/** * Change the master_file option * * @param string $masterFile the complete path and name of the master file */ public function setMasterFiles($masterFiles) { clearstatcache(); $this->_specificOptions['master_file'] = $masterFiles[0]; // to keep a compatibility $this->_specificOptions['master_files'] = $masterFiles; $this->_masterFile_mtimes = array(); $i = 0; foreach ($masterFiles as $masterFile) { $this->_masterFile_mtimes[$i] = @filemtime($masterFile); if (!$this->_specificOptions['ignore_missing_master_files'] && !$this->_masterFile_mtimes[$i]) { Cache::throwException('Unable to read master_file : ' . $masterFile); } $i++; } }
/** * Make a control key with the string containing datas * * @param string $data Data * @param string $controlType Type of control 'md5', 'crc32' or 'strlen' * @throws \Zend\Cache\Exception * @return string Control key */ protected function _hash($data, $controlType) { switch ($controlType) { case 'md5': return md5($data); case 'crc32': return crc32($data); case 'strlen': return strlen($data); case 'adler32': return hash('adler32', $data); default: Cache\Cache::throwException("Incorrect hash function : {$controlType}"); } }
public function testThrowMethod() { Cache\Cache::throwException('test'); }
/** * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...) * * Throw an exception if a problem is found * * @param string $string Cache id or tag * @throws \Zend\Cache\Exception * @return void * @deprecated Not usable until perhaps ZF 2.0 */ protected static function _validateIdOrTag($string) { if (!is_string($string)) { Cache\Cache::throwException('Invalid id or tag : must be a string'); } // Internal only checked in Frontend - not here! if (substr($string, 0, 9) == 'internal-') { return; } // Validation assumes no query string, fragments or scheme included - only the path if (!preg_match('/^(?:\\/(?:(?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*\'()\\[\\]:@&=+$,;])*)?)+$/', $string)) { Cache\Cache::throwException("Invalid id or tag '{$string}' : must be a valid URL path"); } }
/** * Log a message at the WARN (4) priority. * * @param string $message * @throws \Zend\Cache\Exception * @return void */ protected function _log($message, $priority = 4) { if (!$this->_directives['logging']) { return; } if (!isset($this->_directives['logger'])) { Cache\Cache::throwException('Logging is enabled but logger is not set.'); } $logger = $this->_directives['logger']; if (!$logger instanceof Log\Logger) { Cache\Cache::throwException('Logger object is not an instance of Zend_Log class.'); } $logger->log($message, $priority); }
/** * Make a cache id from the function name and parameters * * @param callback $callback A valid callback * @param array $args Function parameters * @throws \Zend\Cache\Exception * @return string Cache id */ public function makeId($callback, array $args = array()) { if (!is_callable($callback, true, $name)) { Cache::throwException('Invalid callback'); } // functions, methods and classnames are case-insensitive $name = strtolower($name); // generate a unique id for object callbacks if (is_object($callback)) { // Closures & __invoke $object = $callback; } elseif (isset($callback[0])) { // array($object, 'method') $object = $callback[0]; } if (isset($object)) { try { $tmp = @serialize($callback); } catch (\Exception $e) { Cache::throwException($e->getMessage()); } if (!$tmp) { $lastErr = error_get_last(); Cache::throwException("Can't serialize callback object to generate id: {$lastErr['message']}"); } $name .= '__' . $tmp; } // generate a unique id for arguments $argsStr = ''; if ($args) { try { $argsStr = @serialize(array_values($args)); } catch (\Exception $e) { Cache::throwException($e->getMessage()); } if (!$argsStr) { $lastErr = error_get_last(); throw Cache::throwException("Can't serialize arguments to generate id: {$lastErr['message']}"); } } return md5($name . $argsStr); }
/** * Specific setter for the 'regexps' option (with some additional tests) * * @param array $options Associative array * @throws \Zend\Cache\Exception * @return void */ protected function _setRegexps($regexps) { if (!is_array($regexps)) { Cache::throwException('regexps option must be an array !'); } foreach ($regexps as $regexp => $conf) { if (!is_array($conf)) { Cache::throwException('regexps option must be an array of arrays !'); } $validKeys = array_keys($this->_specificOptions['default_options']); foreach ($conf as $key => $value) { if (!is_string($key)) { Cache::throwException("unknown option [{$key}] !"); } $key = strtolower($key); if (!in_array($key, $validKeys)) { unset($regexps[$regexp][$key]); } } } $this->setOption('regexps', $regexps); }
/** * Specific method to set the cachedEntity * * if set to a class name, we will cache an abstract class and will use only static calls * if set to an object, we will cache this object methods * * @param mixed $cachedEntity */ public function setCachedEntity($cachedEntity) { if (!is_string($cachedEntity) && !is_object($cachedEntity)) { Cache::throwException('cached_entity must be an object or a class name'); } $this->_cachedEntity = $cachedEntity; $this->_specificOptions['cached_entity'] = $cachedEntity; if (is_string($this->_cachedEntity)) { $this->_cachedEntityLabel = $this->_cachedEntity; } else { $ro = new \ReflectionObject($this->_cachedEntity); $this->_cachedEntityLabel = $ro->getName(); } }
/** * Clean some cache records * * Available modes are : * 'all' (default) => remove all cache entries ($tags is not used) * 'old' => unsupported * 'matchingTag' => unsupported * 'notMatchingTag' => unsupported * 'matchingAnyTag' => unsupported * * @param string $mode clean mode * @param array $tags array of tags * @throws \Zend\Cache\Exception * @return boolean true if no problem */ public function clean($mode = Cache\Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case Cache\Cache::CLEANING_MODE_ALL: $this->_clear(); return true; break; case Cache\Cache::CLEANING_MODE_OLD: $this->_log("Zend_Cache_Backend_ZendServer::clean() : CLEANING_MODE_OLD is unsupported by the Zend Server backends."); break; case Cache\Cache::CLEANING_MODE_MATCHING_TAG: case Cache\Cache::CLEANING_MODE_NOT_MATCHING_TAG: case Cache\Cache::CLEANING_MODE_MATCHING_ANY_TAG: $this->_clear(); $this->_log('Zend_Cache_Backend_ZendServer::clean() : tags are unsupported by the Zend Server backends.'); break; default: Cache\Cache::throwException('Invalid mode for clean() method'); break; } }
/** * Clean some cache records * * Available modes are : * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags * ($tags can be an array of strings or a single string) * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} * ($tags can be an array of strings or a single string) * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags * ($tags can be an array of strings or a single string) * * @param string $mode Clean mode * @param array $tags Array of tags * @throws \Zend\Cache\Exception * @return boolean true if no problem */ public function clean($mode = Cache\CacheCache\Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case Cache\Cache::CLEANING_MODE_ALL: $boolFast = $this->_fastBackend->clean(Cache\Cache::CLEANING_MODE_ALL); $boolSlow = $this->_slowBackend->clean(Cache\Cache::CLEANING_MODE_ALL); return $boolFast && $boolSlow; break; case Cache\Cache::CLEANING_MODE_OLD: return $this->_slowBackend->clean(Cache\Cache::CLEANING_MODE_OLD); case Cache\Cache::CLEANING_MODE_MATCHING_TAG: $ids = $this->_slowBackend->getIdsMatchingTags($tags); $res = true; foreach ($ids as $id) { $bool = $this->remove($id); $res = $res && $bool; } return $res; break; case Cache\Cache::CLEANING_MODE_NOT_MATCHING_TAG: $ids = $this->_slowBackend->getIdsNotMatchingTags($tags); $res = true; foreach ($ids as $id) { $bool = $this->remove($id); $res = $res && $bool; } return $res; break; case Cache\Cache::CLEANING_MODE_MATCHING_ANY_TAG: $ids = $this->_slowBackend->getIdsMatchingAnyTags($tags); $res = true; foreach ($ids as $id) { $bool = $this->remove($id); $res = $res && $bool; } return $res; break; default: Cache\Cache::throwException('Invalid mode for clean() method'); break; } }
/** * Clean some cache records * * Available modes are : * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) * This mode is not supported in this backend * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags * ($tags can be an array of strings or a single string) * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => unsupported * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags * ($tags can be an array of strings or a single string) * * @param string $mode Clean mode * @param array $tags Array of tags * @throws \Zend\Cache\Exception * @return boolean True if no problem */ public function clean($mode = Cache\CacheCache\Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case Cache\Cache::CLEANING_MODE_ALL: case Cache\Cache::CLEANING_MODE_OLD: $cache_dir = ini_get('zend_accelerator.output_cache_dir'); if (!$cache_dir) { return false; } $cache_dir .= '/.php_cache_api/'; return $this->_clean($cache_dir, $mode); break; case Cache\Cache::CLEANING_MODE_MATCHING_TAG: $idlist = null; foreach ($tags as $tag) { $next_idlist = output_cache_get(self::TAGS_PREFIX . $tag, $this->_directives['lifetime']); if ($idlist) { $idlist = array_intersect_assoc($idlist, $next_idlist); } else { $idlist = $next_idlist; } if (count($idlist) == 0) { // if ID list is already empty - we may skip checking other IDs $idlist = null; break; } } if ($idlist) { foreach ($idlist as $id) { output_cache_remove_key($id); } } return true; break; case Cache\Cache::CLEANING_MODE_NOT_MATCHING_TAG: $this->_log("Zend_Cache_Backend_ZendPlatform::clean() : CLEANING_MODE_NOT_MATCHING_TAG is not supported by the Zend Platform backend"); return false; break; case Cache\Cache::CLEANING_MODE_MATCHING_ANY_TAG: $idlist = null; foreach ($tags as $tag) { $next_idlist = output_cache_get(self::TAGS_PREFIX . $tag, $this->_directives['lifetime']); if ($idlist) { $idlist = array_merge_recursive($idlist, $next_idlist); } else { $idlist = $next_idlist; } if (count($idlist) == 0) { // if ID list is already empty - we may skip checking other IDs $idlist = null; break; } } if ($idlist) { foreach ($idlist as $id) { output_cache_remove_key($id); } } return true; break; default: Cache\Cache::throwException('Invalid mode for clean() method'); break; } }
/** * Check if the database structure is ok (with the good version), if no : build it * * @throws \Zend\Cache\Exception * @return boolean True if ok */ private function _checkAndBuildStructure() { if (!$this->_structureChecked) { if (!$this->_checkStructureVersion()) { $this->_buildStructure(); if (!$this->_checkStructureVersion()) { Cache\Cache::throwException("Impossible to build cache structure in " . $this->_options['cache_db_complete_path']); } } $this->_structureChecked = true; } return true; }
/** * Return the filling percentage of the backend storage * * @throws \Zend\Cache\Exception * @return int integer between 0 and 100 */ public function getFillingPercentage() { $mem = apc_sma_info(true); $memSize = $mem['num_seg'] * $mem['seg_size']; $memAvailable = $mem['avail_mem']; $memUsed = $memSize - $memAvailable; if ($memSize == 0) { Cache\Cache::throwException('can\'t get apc memory size'); } if ($memUsed > $memSize) { return 100; } return (int) (100.0 * ($memUsed / $memSize)); }
/** * Return the filling percentage of the backend storage * * @throws \Zend\Cache\Exception * @return int integer between 0 and 100 */ public function getFillingPercentage() { $mems = $this->_memcache->getExtendedStats(); $memSize = null; $memUsed = null; foreach ($mems as $key => $mem) { if ($mem === false) { $this->_log('can\'t get stat from ' . $key); continue; } $eachSize = $mem['limit_maxbytes']; $eachUsed = $mem['bytes']; if ($eachUsed > $eachSize) { $eachUsed = $eachSize; } $memSize += $eachSize; $memUsed += $eachUsed; } if ($memSize === null || $memUsed === null) { Cache\Cache::throwException('Can\'t get filling percentage'); } return (int) (100.0 * ($memUsed / $memSize)); }
/** * Make a cache id from the function name and parameters * * @param string $name Function name * @param array $parameters Function parameters * @throws \Zend\Cache\Exception * @return string Cache id */ private function _makeId($name, $parameters) { if (!is_string($name)) { Cache::throwException('Incorrect function name'); } if (!is_array($parameters)) { Cache::throwException('parameters argument must be an array'); } return md5($name . serialize($parameters)); }