📄 file.php
字号:
/** * 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 (!@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 => we are going to try to invalidate it"); return false; } return true; } /** * 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) { clearstatcache(); $file = $this->_file($id); if (is_null($file)) { return false; } $fileName = @basename($file); $expire = (int) $this->_fileNameToExpireField($fileName); if ($doNotTestCacheValidity) { return $expire; } if (time() <= $expire) { return @filemtime($file); } return false; } /** * 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 tags array $tags array of tags * @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 . '--*'); foreach ($glob as $file) { if (is_file($file)) { if ($mode==Zend_Cache::CLEANING_MODE_ALL) { $result = ($result) && ($this->_remove($file)); } if ($mode==Zend_Cache::CLEANING_MODE_OLD) { $fileName = @basename($file); $expire = (int) $this->_fileNameToExpireField($fileName); if (time() > $expire) { $result = ($result) && ($this->_remove($file)); } } if ($mode==Zend_Cache::CLEANING_MODE_MATCHING_TAG) { $matching = true; $id = $this->_fileNameToId(basename($file)); if (!($this->_isATag($id))) { foreach ($tags as $tag) { if (!($this->_testTag($id, $tag))) { $matching = false; break; } } if ($matching) { $result = ($result) && ($this->remove($id)); } } } if ($mode==Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG) { $matching = false; $id = $this->_fileNameToId(basename($file)); if (!($this->_isATag($id))) { foreach ($tags as $tag) { if ($this->_testTag($id, $tag)) { $matching = true; break; } } if (!$matching) { $result = ($result) && ($this->remove($id)); } } } } 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; } /** * Register a cache id with the given tag * * @param string $id cache id * @param string $tag tag * @return boolean true if no problem */ private function _registerTag($id, $tag) { return $this->save('1', $this->_tagCacheId($id, $tag)); } /** * Unregister tags of a cache id * * @param string $id cache id * @return boolean true if no problem */ private function _unregisterTag($id) { $filesToRemove = @glob($this->_file($this->_tagCacheId($id, '*'), '*')); $result = true; foreach ($filesToRemove as $file) { $result = $result && ($this->_remove($file)); } return $result; } /** * Test if a cache id was saved with the given tag * * @param string $id cache id * @param string $tag tag name * @return true if the cache id was saved with the given tag */ private function _testTag($id, $tag) { if ($this->test($this->_tagCacheId($id, $tag))) { return true; } return false; } /** * Compute & return the expire time * * @return int expire time (unix timestamp) */ private function _expireTime($lifetime) { if (is_null($this->_directives['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' * @return string control key */ private function _hash($data, $controlType) { switch ($controlType) { case 'md5': return md5($data); case 'crc32': return sprintf('% 32d', crc32($data)); case 'strlen': return sprintf('% 32d', strlen($data)); default: Zend_Cache::throwException("Incorrect hash function : $controlType"); } } /** * Return a special/reserved cache id for storing the given tag on the given id * * @param string $id cache id * @param string $tag tag name * @return string cache id for the tag */ private function _tagCacheId($id, $tag) { return 'internal-' . $id . '-' . $tag; } /** * Return true is the given id is a tag * * @param string $id * @return boolean */ private function _isATag($id) { if (substr($id, 0, 9) == 'internal-') { return true; } return false; } /** * Transform a cache id into a file name and return it * * @param string $id cache id * @param int expire timestamp * @return string file name */ private function _idToFileName($id, $expire) { $prefix = $this->_options['file_name_prefix']; $result = $prefix . '---' . $id . '---' . $expire; return $result; } /** * Get the father cache id from the tag cache id * * @param string $id tag cache id * @return string father cache id */ private function _tagCacheIdToFatherCacheId($id) { return preg_replace('~internal-(\w*)-.*$~', '$1', $id); } /** * Return the expire field from the file name * * @param string $fileName * @return string expire field */ private function _fileNameToExpireField($fileName) { $prefix = $this->_options['file_name_prefix']; return preg_replace('~^' . $prefix . '---.*---(\d*)$~', '$1', $fileName); } /** * 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); } /** * 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) { if ($this->_isATag($id)) { // we store tags in the same directory than the father $id2 = $this->_tagCacheIdToFatherCacheId($id); $hash = md5($this->_tagCacheIdToFatherCacheId($id)); } else { $hash = md5($id); } for ($i=0 ; $i<$this->_options['hashed_directory_level'] ; $i++) { $root = $root . $prefix . '--' . substr($hash, 0, $i + 1) . DIRECTORY_SEPARATOR; } } return $root; } /** * Make and return a file name (with path) * * if $expire is null (default), the function try to guess the real file name * (if it fails (no cache files or several cache files for this id, the method returns null) * * @param string $id cache id * @param int expire timestamp * @return string file name (with path) */ private function _file($id, $expire = null) { $path = $this->_path($id); if (is_null($expire)) { $glob = @glob($path . $this->_idToFileName($id, '*')); $nbr = count($glob); if ($nbr == 1) { return $glob[0]; } return null; } $fileName = $this->_idToFileName($id, $expire); return $path . $fileName; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -