PEAR::Cache_Lite是个不错的文件缓存,现在项目中如果有需要用到文件缓存的地方,都会把cache_lite列为首选。
一直使用它的原因之一是:cache_lite可以自动清除哪些过期而又没有更新的文件,像这些文件如果是自己写的文件缓存的话,如果没有清除过期文件的话,这些文件就会一直存在。当然cache_lite可不止这一点好处,可以到cache_lite官网查看更多的相关信息:http://pear.php.net/manual/en/package.caching.cache-lite.intro.php
cache_lite安装:
1、下载cache_lite包:http://pear.php.net/package/Cache_Lite/download,目前最新版为1.7.15
2、下载PEAR包:http://pear.php.net/package/PEAR/download,目前最新版为:1.9.4,下载PEAR包的原因是cache_lite调用了它的错误处理机制。
3、解压以上2个安装包,复制其中的文件到include目录中
./PEAR-1.9.4/PEAR.php ./PEAR-1.9.4/PEAR5.php ./Cache_Lite-1.7.15/Lite.php
4、在include目录中包含了以上3个文件,这样就安装完成了。
cache_lite的使用:
cache_lite使用需要设置一些参数,详情可以见Lite.php这个文件,我喜欢另建立一个类,把一些常用的设置项都设置好,调用这个新建的类,再根据实际情况重置个别设置项。
下面就是自己的cache类:
<?php include 'include/Lite.php'; class filecache { public $_cache = ''; public $path = ''; public $option = array(); public function __construct($cache_path, $cache_option = '') { $this->path = $cache_path; $this->option = $cache_option; //文件缓存路径 $this->option['cacheDir'] = $this->path; //缓存所在的组,默认为 default $this->option['group'] = isset($cache_option['group']) ? $cache_option['group'] : 'default'; //缓存有效期,默认为3600秒 $this->option['lifeTime'] = isset($cache_option['lifeTime']) ? $cache_option['lifeTime'] : 3600; //是否开启对缓存文件名的包含,默认开启 $this->option['fileNameProtection'] = isset($cache_option['fileNameProtection']) ? $cache_option['fileNameProtection'] : true; //是否开启自动清除过期缓存操作,默认为开启,该操作发生在新缓存写入时 $this->option['automaticCleaningFactor'] = isset($cache_option['automaticCleaningFactor']) ? $cache_option['automaticCleaningFactor'] : 1; //是否开启递归目录,默认不开启,1为递归1层目录,2为递归2层目录 $this->option['hashedDirectoryLevel'] = isset($cache_option['hashedDirectoryLevel']) ? $cache_option['hashedDirectoryLevel'] : 0; //开启递归目录时,创建目录时的权限值,默认为0777 $this->option['hashedDirectoryUmask'] = isset($cache_option['hashedDirectoryUmask']) ? $cache_option['hashedDirectoryUmask'] : 0777; $this->_cache = new Cache_Lite($this->option); } public function __destruct() { unset($this->_cache); } public function save_cache($cache_data, $cache_id = '') { if(!file_exists($this->path)) { mkdir($this->path, 0777, true); } return $this->_cache->save($cache_data, $cache_id); } public function get_cache($cache_id) { $result = $this->_cache->get($cache_id); return $result; } public function delete_cache() { return $this->_cache->clean(); } }
这个类基本上把一些常见的参数都设置了默认的初始值,使用时根据情况再重置。
cache_lite常用方法:
Cache_Lite::Cache_Lite() – 构造函数 Cache_Lite::get() – 测试cache是否存在 并(如果是) 返回它 Cache_Lite::save() – 保存数据到一个cache 文件 Cache_Lite::remove() – 删除一个cache文件 Cache_Lite::clean() – 清除cache Cache_Lite::setToDebug() –设置为调试模式 Cache_Lite::setLifeTime() – 设置新的生命周期 Cache_Lite::saveMemoryCachingState() -- Cache_Lite::getMemoryCachingState() -- Cache_Lite::lastModified() – 返回cache最后更新时间。 Cache_Lite::raiseError() – 触发一个 PEAR 错误 constructor Cache_Lite::extendLife() - 重新设定缓存文件修改时间
晚上详细的看了下cache_lite,功能比我想象中的强大的多。当然目前我还没有发现它是否支持直接保存PHP代码来缓存的功能,不过我想应该是可以扩展实现的。
这里有几个例子:
<?
require_once('../libs/cache/Lite.php');
$options = array(
'cacheDir' => '../cache/test/',
'fileLocking' =>true,
'writeControl'=>true,
'readControl'=>false,
'fileNameProtection'=>false,//关闭文件名安全模式。cache id和组名将直接应用到 cache文件的文件名,所以要小心使用特殊字符.
'automaticSerialization'=>false,//关闭自动序列
'hashedDirectoryLevel'=>2,//设置两级缓存路径
'lifeTime' => 60
);
$Cache = new Cache_Lite($options);
$id='test';
if($data=$Cache->get($id,'test')){
echo $data;
}else{
$data=time();
$Cache->save($data);
echo $data;
}
?>
对输出进行缓存
<?
require_once('../libs/cache/Lite.php');
require_once('../libs/cache/Lite/output.php');
$options = array(
'cacheDir' => '../cache/test/',
'lifeTime' => 60,
'pearErrorMode' => CACHE_LITE_ERROR_DIE
);
$cache =new Cache_Lite_Output($options);
if (!($cache->start('id_of_the_page'))) {
// 没有发现Cache !
echo 'test time:'.time().'<br>test<br>';
$cache->end(); // 缓冲的输出现在被存储到一个cache文件中
}
?>
对函数进行缓存
<?
require_once('../libs/cache/Lite.php');
require_once('../libs/cache/Lite/Function.php');
$options = array(
'cacheDir' => '../cache/test/',
'lifeTime' => 3600,
'pearErrorMode' => CACHE_LITE_ERROR_DIE
);
$cache = new Cache_Lite_Function($options);
$cache->call('function_to_bench', 12, 45);
function function_to_bench($arg1, $arg2)
{
echo "This is the output of the function function_to_bench($arg1, $arg2) !<br>";
return "This is the result of the function function_to_bench($arg1, $arg2) !<br>";
}
?>
评论暂时关闭