/** * Constructor * * @param array $options associative array of options * @throws IfwPsn_Vendor_Zend_Cache_Exception */ public function __construct(array $options = array()) { if (!function_exists('zend_shm_cache_store')) { IfwPsn_Vendor_Zend_Cache::throwException('IfwPsn_Vendor_Zend_Cache_ZendServer_ShMem backend has to be used within Zend Server environment.'); } parent::__construct($options); }
/** * 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 IfwPsn_Vendor_Zend_Cache_Exception * @return void * @deprecated Not usable until perhaps ZF 2.0 */ protected static function _validateIdOrTag($string) { if (!is_string($string)) { IfwPsn_Vendor_Zend_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)) { IfwPsn_Vendor_Zend_Cache::throwException("Invalid id or tag '{$string}' : must be a valid URL path"); } }
/** * Specific setter for the 'regexps' option (with some additional tests) * * @param array $options Associative array * @throws IfwPsn_Vendor_Zend_Cache_Exception * @return void */ protected function _setRegexps($regexps) { if (!is_array($regexps)) { IfwPsn_Vendor_Zend_Cache::throwException('regexps option must be an array !'); } foreach ($regexps as $regexp => $conf) { if (!is_array($conf)) { IfwPsn_Vendor_Zend_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)) { IfwPsn_Vendor_Zend_Cache::throwException("unknown option [{$key}] !"); } $key = strtolower($key); if (!in_array($key, $validKeys)) { unset($regexps[$regexp][$key]); } } } $this->setOption('regexps', $regexps); }
/** * Make a control key with the string containing datas * * @param string $data Data * @param string $controlType Type of control 'md5', 'crc32' or 'strlen' * @throws IfwPsn_Vendor_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: IfwPsn_Vendor_Zend_Cache::throwException("Incorrect hash function : {$controlType}"); } }
/** * Log a message at the WARN (4) priority. * * @param string $message * @throws IfwPsn_Vendor_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 IfwPsn_Vendor_Zend_Log)) { IfwPsn_Vendor_Zend_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 : * IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) * IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) * This mode is not supported in this backend * IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags * ($tags can be an array of strings or a single string) * IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => unsupported * IfwPsn_Vendor_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 IfwPsn_Vendor_Zend_Cache_Exception * @return boolean True if no problem */ public function clean($mode = IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL: case IfwPsn_Vendor_Zend_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 IfwPsn_Vendor_Zend_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 IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG: $this->_log("IfwPsn_Vendor_Zend_Cache_Backend_ZendPlatform::clean() : CLEANING_MODE_NOT_MATCHING_TAG is not supported by the Zend Platform backend"); return false; break; case IfwPsn_Vendor_Zend_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: IfwPsn_Vendor_Zend_Cache::throwException('Invalid mode for clean() method'); break; } }
/** * Return the filling percentage of the backend storage * * @throws IfwPsn_Vendor_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) { IfwPsn_Vendor_Zend_Cache::throwException('Can\'t get filling percentage'); } return (int) (100.0 * ($memUsed / $memSize)); }
/** * Frontend Constructor * * @param string $frontend * @param array $frontendOptions * @param boolean $customFrontendNaming * @param boolean $autoload * @return IfwPsn_Vendor_Zend_Cache_Core|IfwPsn_Vendor_Zend_Cache_Frontend */ public static function _makeFrontend($frontend, $frontendOptions = array(), $customFrontendNaming = false, $autoload = false) { if (!$customFrontendNaming) { $frontend = self::_normalizeName($frontend); } if (in_array($frontend, self::$standardFrontends)) { // we use a standard frontend // For perfs reasons, with frontend == 'Core', we can interact with the Core itself $frontendClass = 'IfwPsn_Vendor_Zend_Cache_' . ($frontend != 'Core' ? 'Frontend_' : '') . $frontend; // security controls are explicit require_once IFW_PSN_LIB_ROOT . str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php'; } else { // we use a custom frontend if (!preg_match('~^[\\w\\\\]+$~D', $frontend)) { IfwPsn_Vendor_Zend_Cache::throwException("Invalid frontend name [{$frontend}]"); } if (!$customFrontendNaming) { // we use this boolean to avoid an API break $frontendClass = 'IfwPsn_Vendor_Zend_Cache_Frontend_' . $frontend; } else { $frontendClass = $frontend; } if (!$autoload) { $file = str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php'; if (!self::_isReadable($file)) { self::throwException("file {$file} not found in include_path"); } require_once $file; } } return new $frontendClass($frontendOptions); }
/** * Return the filling percentage of the backend storage * * @throws IfwPsn_Vendor_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) { IfwPsn_Vendor_Zend_Cache::throwException('can\'t get apc memory size'); } if ($memUsed > $memSize) { return 100; } return (int) (100.0 * ($memUsed / $memSize)); }
/** * Main method : call the specified method or get the result from cache * * @param string $name Method name * @param array $parameters Method parameters * @return mixed Result * @throws Exception */ public function __call($name, $parameters) { $callback = array($this->_cachedEntity, $name); if (!is_callable($callback, false)) { IfwPsn_Vendor_Zend_Cache::throwException('Invalid callback'); } $cacheBool1 = $this->_specificOptions['cache_by_default']; $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']); $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']); $cache = ($cacheBool1 || $cacheBool2) && !$cacheBool3; if (!$cache) { // We do not have not cache return call_user_func_array($callback, $parameters); } $id = $this->makeId($name, $parameters); if (($rs = $this->load($id)) && array_key_exists(0, $rs) && array_key_exists(1, $rs)) { // A cache is available $output = $rs[0]; $return = $rs[1]; } else { // A cache is not available (or not valid for this frontend) ob_start(); ob_implicit_flush(false); try { $return = call_user_func_array($callback, $parameters); $output = ob_get_clean(); $data = array($output, $return); $this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority); } catch (Exception $e) { ob_end_clean(); throw $e; } } echo $output; return $return; }
/** * 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 IfwPsn_Vendor_Zend_Cache_Exception * @return boolean true if no problem */ public function clean($mode = IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL: $this->_clear(); return true; break; case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_OLD: $this->_log("IfwPsn_Vendor_Zend_Cache_Backend_ZendServer::clean() : CLEANING_MODE_OLD is unsupported by the Zend Server backends."); break; case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_MATCHING_TAG: case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG: case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG: $this->_clear(); $this->_log('IfwPsn_Vendor_Zend_Cache_Backend_ZendServer::clean() : tags are unsupported by the Zend Server backends.'); break; default: IfwPsn_Vendor_Zend_Cache::throwException('Invalid mode for clean() method'); break; } }
/** * Make a cache id from the function name and parameters * * @param callback $callback A valid callback * @param array $args Function parameters * @throws IfwPsn_Vendor_Zend_Cache_Exception * @return string Cache id */ public function makeId($callback, array $args = array()) { if (!is_callable($callback, true, $name)) { IfwPsn_Vendor_Zend_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) { IfwPsn_Vendor_Zend_Cache::throwException($e->getMessage()); } if (!$tmp) { $lastErr = error_get_last(); IfwPsn_Vendor_Zend_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) { IfwPsn_Vendor_Zend_Cache::throwException($e->getMessage()); } if (!$argsStr) { $lastErr = error_get_last(); throw IfwPsn_Vendor_Zend_Cache::throwException("Can't serialize arguments to generate id: {$lastErr['message']}"); } } return md5($name . $argsStr); }
/** * Log a message at the WARN (4) priority. * * @param string $message * @throws IfwPsn_Vendor_Zend_Cache_Exception * @return void */ protected function _log($message, $priority = 4) { if (!$this->_directives['logging']) { return; } if (!isset($this->_directives['logger'])) { IfwPsn_Vendor_Zend_Cache::throwException('Logging is enabled but logger is not set.'); } $logger = $this->_directives['logger']; if (!$logger instanceof IfwPsn_Vendor_Zend_Log) { IfwPsn_Vendor_Zend_Cache::throwException('Logger object is not an instance of IfwPsn_Vendor_Zend_Log class.'); } $logger->log($message, $priority); }
/** * Change the master_files option * * @param array $masterFiles the complete paths and name of the master files */ public function setMasterFiles(array $masterFiles) { $this->_specificOptions['master_file'] = null; // to keep a compatibility $this->_specificOptions['master_files'] = null; $this->_masterFile_mtimes = array(); clearstatcache(); $i = 0; foreach ($masterFiles as $masterFile) { if (file_exists($masterFile)) { $mtime = filemtime($masterFile); } else { $mtime = false; } if (!$this->_specificOptions['ignore_missing_master_files'] && !$mtime) { IfwPsn_Vendor_Zend_Cache::throwException('Unable to read master_file : ' . $masterFile); } $this->_masterFile_mtimes[$i] = $mtime; $this->_specificOptions['master_files'][$i] = $masterFile; if ($i === 0) { // to keep a compatibility $this->_specificOptions['master_file'] = $masterFile; } $i++; } }
/** * Check if the database structure is ok (with the good version), if no : build it * * @throws IfwPsn_Vendor_Zend_Cache_Exception * @return boolean True if ok */ private function _checkAndBuildStructure() { if (!$this->_structureChecked) { if (!$this->_checkStructureVersion()) { $this->_buildStructure(); if (!$this->_checkStructureVersion()) { IfwPsn_Vendor_Zend_Cache::throwException("Impossible to build cache structure in " . $this->_options['cache_db_complete_path']); } } $this->_structureChecked = true; } return true; }
/** * 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_clean(); } else { $data =& $forcedDatas; } $id = array_pop($this->_idStack); if ($id === null) { IfwPsn_Vendor_Zend_Cache::throwException('use of end() without a start()'); } $this->save($data, $id, $tags, $specificLifetime, $priority); if ($echoData) { echo $data; } }
/** * 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 IfwPsn_Vendor_Zend_Cache_Exception * @return boolean true if no problem */ public function clean($mode = IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case IfwPsn_Vendor_Zend_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']; } $cnt = xcache_count(XC_TYPE_VAR); for ($i = 0; $i < $cnt; $i++) { xcache_clear_cache(XC_TYPE_VAR, $i); } 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 IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_OLD: $this->_log("IfwPsn_Vendor_Zend_Cache_Backend_Xcache::clean() : CLEANING_MODE_OLD is unsupported by the Xcache backend"); break; case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_MATCHING_TAG: case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG: case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG: $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND); break; default: IfwPsn_Vendor_Zend_Cache::throwException('Invalid mode for clean() method'); break; } }
/** * Return the filling percentage of the backend storage * * @throws IfwPsn_Vendor_Zend_Cache_Exception * @return int integer between 0 and 100 */ public function getFillingPercentage() { $mem = wincache_ucache_meminfo(); $memSize = $mem['memory_total']; $memUsed = $memSize - $mem['memory_free']; if ($memSize == 0) { IfwPsn_Vendor_Zend_Cache::throwException('can\'t get WinCache memory size'); } if ($memUsed > $memSize) { return 100; } return (int) (100.0 * ($memUsed / $memSize)); }
/** * Clean some cache records * * Available modes are : * IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) * IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) * IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags * ($tags can be an array of strings or a single string) * IfwPsn_Vendor_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) * IfwPsn_Vendor_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 IfwPsn_Vendor_Zend_Cache_Exception * @return boolean true if no problem */ public function clean($mode = IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL, $tags = array()) { switch ($mode) { case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL: $boolFast = $this->_fastBackend->clean(IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL); $boolSlow = $this->_slowBackend->clean(IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_ALL); return $boolFast && $boolSlow; break; case IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_OLD: return $this->_slowBackend->clean(IfwPsn_Vendor_Zend_Cache::CLEANING_MODE_OLD); case IfwPsn_Vendor_Zend_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 IfwPsn_Vendor_Zend_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 IfwPsn_Vendor_Zend_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: IfwPsn_Vendor_Zend_Cache::throwException('Invalid mode for clean() method'); break; } }