📄 file.php
字号:
*/ private function _cleanMetadatas() { $this->_metadatasArray = array(); } /** * Load metadatas from disk * * @param string $id Cache id * @return array|false Metadatas associative array */ private function _loadMetadatas($id) { $file = $this->_metadatasFile($id); $result = $this->_fileGetContents($file); if (!$result) { return false; } $tmp = @unserialize($result); return $tmp; } /** * Save metadatas to disk * * @param string $id Cache id * @param array $metadatas Associative array * @return boolean True if no problem */ private function _saveMetadatas($id, $metadatas) { $file = $this->_metadatasFile($id); $result = $this->_filePutContents($file, serialize($metadatas)); if (!$result) { return false; } return true; } /** * Make and return a file name (with path) for metadatas * * @param string $id Cache id * @return string Metadatas file name (with path) */ private function _metadatasFile($id) { $path = $this->_path($id); $fileName = $this->_idToFileName('internal-metadatas---' . $id); return $path . $fileName; } /** * Check if the given filename is a metadatas one * * @param string $fileName File name * @return boolean True if it's a metadatas one */ private function _isMetadatasFile($fileName) { $id = $this->_fileNameToId($fileName); if (substr($id, 0, 21) == 'internal-metadatas---') { return true; } else { return false; } } /** * Remove a file * * If we can't remove the file (because of locks or any problem), we will touch * the file to invalidate it * * @param string $file Complete file path * @return boolean True if ok */ private function _remove($file) { if (!is_file($file)) { return false; } if (!@unlink($file)) { # we can't remove the file (because of locks or any problem) $this->_log("Zend_Cache_Backend_File::_remove() : we can't remove $file"); return false; } return true; } /** * Clean some cache records (private method used for recursive stuff) * * 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) * * @param string $dir Directory to clean * @param string $mode Clean mode * @param array $tags Array of tags * @throws Zend_Cache_Exception * @return boolean True if no problem */ private function _clean($dir, $mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) { if (!is_dir($dir)) { return false; } $result = true; $prefix = $this->_options['file_name_prefix']; $glob = @glob($dir . $prefix . '--*'); if ($glob === false) { return true; } foreach ($glob as $file) { if (is_file($file)) { $fileName = basename($file); if ($this->_isMetadatasFile($fileName)) { // in CLEANING_MODE_ALL, we drop anything, even remainings old metadatas files if ($mode != Zend_Cache::CLEANING_MODE_ALL) { continue; } } $id = $this->_fileNameToId($fileName); $metadatas = $this->_getMetadatas($id); if ($metadatas === FALSE) { $metadatas = array('expire' => 1, 'tags' => array()); } switch ($mode) { case Zend_Cache::CLEANING_MODE_ALL: $res = $this->remove($id); if (!$res) { // in this case only, we accept a problem with the metadatas file drop $res = $this->_remove($file); } $result = $result && $res; break; case Zend_Cache::CLEANING_MODE_OLD: if (time() > $metadatas['expire']) { $result = ($result) && ($this->remove($id)); } break; case Zend_Cache::CLEANING_MODE_MATCHING_TAG: $matching = true; foreach ($tags as $tag) { if (!in_array($tag, $metadatas['tags'])) { $matching = false; break; } } if ($matching) { $result = ($result) && ($this->remove($id)); } break; case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG: $matching = false; foreach ($tags as $tag) { if (in_array($tag, $metadatas['tags'])) { $matching = true; break; } } if (!$matching) { $result = ($result) && $this->remove($id); } break; default: Zend_Cache::throwException('Invalid mode for clean() method'); break; } } if ((is_dir($file)) and ($this->_options['hashed_directory_level']>0)) { // Recursive call $result = ($result) && ($this->_clean($file . DIRECTORY_SEPARATOR, $mode, $tags)); if ($mode=='all') { // if mode=='all', we try to drop the structure too @rmdir($file); } } } return $result; } /** * Compute & return the expire time * * @return int expire time (unix timestamp) */ private function _expireTime($lifetime) { if (is_null($lifetime)) { return 9999999999; } return time() + $lifetime; } /** * 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 */ private 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: Zend_Cache::throwException("Incorrect hash function : $controlType"); } } /** * Transform a cache id into a file name and return it * * @param string $id Cache id * @return string File name */ private function _idToFileName($id) { $prefix = $this->_options['file_name_prefix']; $result = $prefix . '---' . $id; return $result; } /** * Make and return a file name (with path) * * @param string $id Cache id * @return string File name (with path) */ private function _file($id) { $path = $this->_path($id); $fileName = $this->_idToFileName($id); return $path . $fileName; } /** * Return the complete directory path of a filename (including hashedDirectoryStructure) * * @param string $id Cache id * @return string Complete directory path */ private function _path($id) { $root = $this->_options['cache_dir']; $prefix = $this->_options['file_name_prefix']; if ($this->_options['hashed_directory_level']>0) { $hash = hash('adler32', $id); for ($i=0 ; $i < $this->_options['hashed_directory_level'] ; $i++) { $root = $root . $prefix . '--' . substr($hash, 0, $i + 1) . DIRECTORY_SEPARATOR; } } return $root; } /** * Test if the given cache id is available (and still valid as a cache record) * * @param string $id Cache id * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested * @return boolean|mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record */ private function _test($id, $doNotTestCacheValidity) { $metadatas = $this->_getMetadatas($id); if (!$metadatas) { return false; } if ($doNotTestCacheValidity || (time() <= $metadatas['expire'])) { return $metadatas['mtime']; } return false; } /** * Return the file content of the given file * * @param string $file File complete path * @return string File content (or false if problem) */ private function _fileGetContents($file) { $result = false; if (!is_file($file)) { return false; } $mqr = get_magic_quotes_runtime(); set_magic_quotes_runtime(0); $f = @fopen($file, 'rb'); if ($f) { if ($this->_options['file_locking']) @flock($f, LOCK_SH); $fsize = @filesize($file); if ($fsize == 0) { $result = ''; } else { $result = fread($f, $fsize); } if ($this->_options['file_locking']) @flock($f, LOCK_UN); @fclose($f); } set_magic_quotes_runtime($mqr); return $result; } /** * Put the given string into the given file * * @param string $file File complete path * @param string $string String to put in file * @return boolean true if no problem */ private function _filePutContents($file, $string) { $result = false; $f = @fopen($file, 'wb'); if ($f) { if ($this->_options['file_locking']) @flock($f, LOCK_EX); $tmp = @fwrite($f, $string); if (!($tmp === FALSE)) { $result = true; } if ($this->_options['file_locking']) @flock($f, LOCK_UN); @fclose($f); } @chmod($file, $this->_options['cache_file_umask']); return $result; } /** * Transform a file name into cache id and return it * * @param string $fileName File name * @return string Cache id */ private function _fileNameToId($fileName) { $prefix = $this->_options['file_name_prefix']; return preg_replace('~^' . $prefix . '---(.*)$~', '$1', $fileName); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -