Cache_Lite 是一个非常不错的文件缓存类。 见:http://pear.php.net/package/Cache_Lite/
// 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(); }