file.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 646 行 · 第 1/2 页
PHP
646 行
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Cache
* @subpackage Backend
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Zend_Cache_Backend_Interface
*/
require_once 'Zend/Cache/Backend/Interface.php';
/**
* Zend_Cache_Backend
*/
require_once 'Zend/Cache/Backend.php';
/**
* @package Zend_Cache
* @subpackage Backend
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Cache_Backend_File extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
{
// ------------------
// --- Properties ---
// ------------------
/**
* Available options
*
* =====> (string) cacheDir :
* - Directory where to put the cache files
*
* =====> (boolean) fileLocking :
* - Enable / disable fileLocking
* - Can avoid cache corruption under bad circumstances but it doesn't work on multithread
* webservers and on NFS filesystems for example
*
* =====> (boolean) readControl :
* - Enable / disable read control
* - If enabled, a control key is embeded in cache file and this key is compared with the one
* calculated after the reading.
*
* =====> (string) readControlType :
* - Type of read control (only if read control is enabled). Available values are :
* 'md5' for a md5 hash control (best but slowest)
* 'crc32' for a crc32 hash control (lightly less safe but faster, better choice)
* 'strlen' for a length only test (fastest)
*
* =====> (int) hashedDirectoryLevel :
* - Hashed directory level
* - Set the hashed directory structure level. 0 means "no hashed directory
* structure", 1 means "one level of directory", 2 means "two levels"...
* This option can speed up the cache only when you have many thousands of
* cache file. Only specific benchs can help you to choose the perfect value
* for you. Maybe, 1 or 2 is a good start.
*
* =====> (int) hashedDirectoryUmask :
* - Umask for hashed directory structure
*
* =====> (string) fileNamePrefix :
* - prefix for cache files
* - be really carefull with this option because a too generic value in a system cache dir
* (like /tmp) can cause disasters when cleaning the cache
*
* @var array available options
*/
protected $_options = array(
'cacheDir' => '/tmp',
'fileLocking' => true,
'readControl' => true,
'readControlType' => 'crc32',
'hashedDirectoryLevel' => 0,
'hashedDirectoryUmask' => 0700,
'fileNamePrefix' => 'zend_cache'
);
// ----------------------
// --- Public methods ---
// ----------------------
/**
* Constructor
*
* @param array $options associative array of options
*/
public function __construct($options = array())
{
parent::__construct($options);
if (isset($options['cacheDir'])) { // particular case for this option
$this->setCacheDir($options['cacheDir']);
} else {
$this->_options['cacheDir'] = self::getTmpDir() . DIRECTORY_SEPARATOR;
}
if (isset($options['fileNamePrefix'])) { // particular case for this option
if (!preg_match('~^[\w]+$~', $options['fileNamePrefix'])) {
Zend_Cache::throwException('Invalid fileNamePrefix : must use only [a-zA-A0-9_]');
}
}
}
/**
* Set the cacheDir (particular case of setOption() method)
*
* @param mixed $value
*/
public function setCacheDir($value)
{
// add a trailing DIRECTORY_SEPARATOR if necessary
$value = rtrim($value, '\\/') . DIRECTORY_SEPARATOR;
$this->setOption('cacheDir', $value);
}
/**
* Test if a cache is available for the given id and (if yes) return it (false else)
*
* @param string $id cache id
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
* @return string cached datas (or false)
*/
public function load($id, $doNotTestCacheValidity = false)
{
if (!($this->_test($id, $doNotTestCacheValidity))) {
// The cache is not hit !
return false;
}
$file = $this->_file($id);
if (is_null($file)) {
return false;
}
// There is an available cache file !
$fp = @fopen($file, 'rb');
if (!$fp) return false;
if ($this->_options['fileLocking']) @flock($fp, LOCK_SH);
$length = @filesize($file);
$mqr = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
if ($this->_options['readControl']) {
$hashControl = @fread($fp, 32);
$length = $length - 32;
}
if ($length) {
$data = @fread($fp, $length);
} else {
$data = '';
}
set_magic_quotes_runtime($mqr);
if ($this->_options['fileLocking']) @flock($fp, LOCK_UN);
@fclose($fp);
if ($this->_options['readControl']) {
$hashData = $this->_hash($data, $this->_options['readControlType']);
if ($hashData != $hashControl) {
// Problem detected by the read control !
if ($this->_directives['logging']) {
Zend_Log::log('Zend_Cache_Backend_File::load() / readControl : stored hash and computed hash do not match', Zend_Log::LEVEL_WARNING);
}
$this->_remove($file);
return false;
}
}
return $data;
}
/**
* Test if a cache is available or not (for the given id)
*
* @param string $id cache id
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
*/
public function test($id)
{
return $this->_test($id, false);
}
/**
* Save some string datas into a cache record
*
* Note : $data is always "string" (serialization is done by the
* core not by the backend)
*
* @param string $data datas to cache
* @param string $id cache id
* @param array $tags array of strings, the cache record will be tagged by each string entry
* @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
* @return boolean true if no problem
*/
public function save($data, $id, $tags = array(), $specificLifetime = false)
{
if ((!is_dir($this->_options['cacheDir'])) or (!is_writable($this->_options['cacheDir']))) {
if ($this->_directives['logging']) {
Zend_Log::log("Zend_Cache_Backend_File::save() : cacheDir doesn't exist or is not writable", Zend_Log::LEVEL_WARNING);
}
}
$this->remove($id); // to avoid multiple files with the same cache id
$lifetime = $this->getLifetime($specificLifetime);
$expire = $this->_expireTime($lifetime);
$file = $this->_file($id, $expire);
$firstTry = true;
$result = false;
while (1 == 1) {
$fp = @fopen($file, "wb");
if ($fp) {
// we can open the file, so the directory structure is ok
if ($this->_options['fileLocking']) @flock($fp, LOCK_EX);
if ($this->_options['readControl']) {
@fwrite($fp, $this->_hash($data, $this->_options['readControlType']), 32);
}
$mqr = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
@fwrite($fp, $data);
if ($this->_options['fileLocking']) @flock($fp, LOCK_UN);
@fclose($fp);
set_magic_quotes_runtime($mqr);
$result = true;
break;
}
// we can't open the file but it's maybe only the directory structure
// which has to be built
if ($this->_options['hashedDirectoryLevel']==0) break;
if ((!$firstTry) || ($this->_options['hashedDirectoryLevel'] == 0)) {
// it's not a problem of directory structure
break;
}
$firstTry = false;
// In this case, maybe we just need to create the corresponding directory
@mkdir($this->_path($id), $this->_options['hashedDirectoryUmask'], true);
@chmod($this->_options['hashedDirectoryUmask']); // see #ZF-320 (this line is required in some configurations)
}
if ($result) {
foreach ($tags as $tag) {
$this->_registerTag($id, $tag);
}
}
return $result;
}
/**
* Remove a cache record
*
* @param string $id cache id
* @return boolean true if no problem
*/
public function remove($id)
{
$result1 = true;
$files = @glob($this->_file($id, '*'));
if (count($files) == 0) {
return false;
}
foreach ($files as $file) {
$result1 = $result1 && $this->_remove($file);
}
$result2 = $this->_unregisterTag($id);
return ($result1 && $result2);
}
/**
* Clean some cache records
*
* Available modes are :
* 'all' (default) => remove all cache entries ($tags is not used)
* 'old' => remove too old cache entries ($tags is not used)
* 'matchingTag' => remove cache entries matching all given tags
* ($tags can be an array of strings or a single string)
* 'notMatchingTag' => remove cache entries not matching one of the given tags
* ($tags can be an array of strings or a single string)
*
* @param string $mode clean mode
* @param tags array $tags array of tags
* @return boolean true if no problem
*/
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
{
// We use this private method to hide the recursive stuff
clearstatcache();
return $this->_clean($this->_options['cacheDir'], $mode, $tags);
}
/**
* PUBLIC METHOD FOR UNIT TESTING ONLY !
*
* Force a cache record to expire
*
* @param string $id cache id
*/
public function ___expire($id)
{
$file = $this->_file($id);
if (!(is_null($file))) {
$file2 = $this->_file($id, 1);
@rename($file, $file2);
}
}
// -----------------------
// --- Private methods ---
// -----------------------
/**
* Remove a file
*
* If we can't remove the file (because of locks or any problem), we will touch
* the file to invalidate it
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?