Cache_Lite 是一个非常不错的文件缓存类。 见:http://pear.php.net/package/Cache_Lite/

基本概念和技巧

  • 组概念: group 主要是clean缓存时可以指定清除某一个组里的缓存。设置缓存时加上group: get(id,group)
  • 对于id、group等属性, 使用 Cache_Lite::get($id, $group = ‘default’, $doNotTestCacheValidity = false) 时会自动存起来,在 Cache_Lite::save($data, $id = NULL, $group = ‘default’)时就不需要再重复写了。直接Cache_Lite::save($data)即可。 看实例中第一个例子。
  • Cache_Lite::clean($group = false, $mode = ‘ingroup’) 方法,清除缓存。$goup 为空时,默认清除所有缓存。
  • 默认缓存都放在一个文件夹里,可以用 ‘hashedDirectoryLevel’ 参数设置目录级别。
  • 过期时间设置是’lifeTime’ 。如果实例化以后还需要修改,使用 Cache_Lite::setLifeTime($newLifeTime)

使用实例

// Set a few options
$options = array(
    'cacheDir' => ROOT_PATH.'/cache/',  //缓存目录路径
    'lifeTime' => 3,                    //过期时间
    'hashedDirectoryLevel' => 2,        //缓存使用目录级别,0为不使用
);
 
// Create a Cache_Lite object
$Cache_Lite = new Cache_Lite($options);
if ($data = $Cache_Lite->get($id, "list_3")) {
 
    echo " Cache hit ! ";
    // Content is in $data
    // (...)
 
} else { // No valid cache found (you have to make the page)
 
    // Cache miss !
    // Put in $data datas to put in cache
    $data = ' string '.date("Y-m-d H:i:s") ;
    $Cache_Lite->save($data);         // :!: 注意:save的完整方法为:save($data, $id = NULL, $group = 'default') 这里save方法并没有带上id,group等参数。
}
A few examples of Cache_Lite_Output using:
require_once('Cache/Lite/Output.php');
 
$options = array(
    'cacheDir' => '/tmp/',
    'lifeTime' => 10
);
 
$cache = new Cache_Lite_Output($options);
 
if (!($cache->start('123'))) {
    // Cache missed...
    for($i=0;$i<1000;$i++) { // Making of the page...
        echo('0123456789');
    }
    $cache->end();
}
php/cache_lite.txt · 最后更改: 2007/10/26 07:27 由 58.31.118.11
到顶部
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0 红麦软件 红麦软件