简单好用的缓存函数

 1 <?php
 2  /**
 3 * 读取或设置缓存
 4 *
 5 * @access  public
 6 * @param   string  $name   缓存名称
 7 * @param   mixed   $value  缓存内容, null删除缓存
 8 * @param   string  $path   缓存路径
 9 * @return  mixed
10  */
11  function cache($name, $value = '', $path = '')
12 {
13     return false;   //调试阶段, 不进行缓存
14     $path = empty($path? './Cache/' : $path;
15     $file = $path . $name . '.php';
16     
17     if (empty($value)) {
18         //缓存不存在
19         if (!is_file($file)) {
20             return false;
21         }
22         //删除缓存
23         if (is_null($value)) {
24             unlink($file);
25             return true;
26         }
27         $data = include $file;
28         return $data;
29     }
30     $value = var_export($value, true);
31     $value = "<?php \r\n!defined('ROOT_PATH') && exit('Access Denied');\r\n return {$value};\r\n?>";
32     return file_put_contents($file, $value);
33 }
34 /* 文件结束 FileName */

 

使用方法也挺简单的.

1 cache('name', array('a', 'b', 'c')); //写入缓存 name为缓存名称, 后面那个数组是缓存的内容
2 cache('name'); //读取缓存
3 cache('name', null); //删除缓存

 

posted on 2010-01-31 13:48  李恺  阅读(122)  评论(0)    收藏  举报