Cache_Lite提供了快速,轻便和安全的缓存系统。它针对文件容器进行了优化,并且防止缓存损坏(因为它使用文件锁定和/或散列测试).
个人感觉还是挺方便的。
Cache_Lite 官方参考地址。
Cache_Lite 缓存包下载地址。
局部缓存:
<?php
header("content-type:text/html;charset=utf8");
//引入Cache_Lite缓存类
require_once('Caches/Cache/Lite.php');
$options = array(
'cacheDir' => '/like/', //设置存放的目录,后面一定要加 /。(此目录要自己创建)
'lifeTime' => 32 //缓存周期
);
$Cache_Lite = new Cache_Lite($options);
if ($data = $Cache_Lite->get('block1')) {
echo($data);
echo "woshi block1 cache";
} else {
$data = 'Data of the block 1';
$Cache_Lite->save($data);
echo $data."没缓存";
}
echo "<br>";
//测试cache是否存在 并(如果是) 返回它。
if ($data = $Cache_Lite->get('block3')) {
echo($data);
echo "woshi block2 cache";
} else {
$data = 'Data of the block 2';
//保存数据到一个cache 文件。
$Cache_Lite->save($data);
echo $data."没缓存";
}
//删除缓存可用如下方式:
//$Cache_Lite->remove('block1');
//$Cache_Lite->remove('block3');
?>
页面缓存:
<?php
header("content-type:text/html;charset=utf8");
//引人缓存文件。
require_once('Caches/Cache/Lite/Output.php');
$options = array(
'cacheDir' => '/like/', //设置存放的目录,后面一定要加 /。(此目录要自己创建)
'lifeTime' => 3, //缓存时长。
);
//实例化对象。
$cache = new Cache_Lite_Output($options);
//测试是否一个cache有效 并(如果有效)返回它输出到浏览器. 否则,激 活输出缓冲.
if (!($cache->start('isd_of_the_page'))) {
/**
* 没有发现cache !
* 直到遇到end()方法 所有的输出将被缓冲进缓存区。
* 下次会自动从缓存区取数据。
*/
//停止由start()方法开始的输出缓冲并且保存输出到一个cache文件
$cache->end();
}
?>
函数缓存:
<?php
header("content-type:text/html;charset=utf8");
//引入方法缓存类。
require_once('Caches/Cache/Lite/Function.php');
$options = array(
//缓存目录。
'cacheDir' => '/like/',
//缓存周期。
'lifeTime' =>15
);
//方法缓存类。
$cache = new Cache_Lite_Function($options);
/**
* 有缓存会直接输出缓存。
* 没有会将缓存内的输出输出到浏览器并缓冲到缓存。
*/
$cache->call('functions', 14, 45);
//定义一个缓存方法。
function functions($arg1, $arg2)
{
//输出内容。
echo "This is the output of the function_to_bench($arg1, $arg2) !";
}
/**
* 清除函数的缓存。不建议用,报错级别高。
* 且还需要传递旧值,传递错误直接操作。
* 可采用清除。
*/
//$cache->drop('functions', 14 ,45);
?>
清洗缓存:
<?php
header("content-type:text/html;charset=utf8");
//引入Cache_Lite缓存类
require_once('Caches/Cache/Lite.php');
$options = array(
'cacheDir' => '/like/',
'lifeTime' => 32
);
$Cache_Lite = new Cache_Lite($options);
/**
* 清洗缓存。返回 boor 值
*/
$Cache_Lite->clean();
?>