php Cache 类
来源:互联网 发布:印度主流聊天软件 编辑:程序博客网 时间:2024/06/05 15:49
1、实例化
$cache = new Cache();
2、设置缓存时间和缓存目录
$cache = new Cache(60, '/any_other_path/');
第一个参数是缓存秒数,第二个参数是缓存路径,根据需要配置。
默认情况下,缓存时间是 3600 秒,缓存目录是 cache/
3、读取缓存
$value = $cache->get('data_key');
4、写入缓存
$value = $cache->put('data_key', 'data_value');
完整实例:
$cache = new Cache();//从缓存从读取键值 $key 的数据$values = $cache->get($key);//如果没有缓存数据if ($values == false) {//insert code here...//写入键值 $key 的数据$cache->put($key, $values);} else {//insert code here...}
<?phpclass Cache {private $cache_path;//path for the cacheprivate $cache_expire;//seconds that the cache expires//cache constructor, optional expiring time and cache pathpublic function Cache($exp_time=3600,$path="cache/"){$this->cache_expire=$exp_time;$this->cache_path=$path;}//returns the filename for the cacheprivate function fileName($key){return $this->cache_path.md5($key);}//creates new cache files with the given data, $key== name of the cache, data the info/values to storepublic function put($key, $data){$values = serialize($data);$filename = $this->fileName($key);$file = fopen($filename, 'w'); if ($file){//able to create the file fwrite($file, $values); fclose($file); } else return false;}//returns cache for the given keypublic function get($key){$filename = $this->fileName($key);if (!file_exists($filename) || !is_readable($filename)){//can't read the cachereturn false;}if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired$file = fopen($filename, "r");// read data file if ($file){//able to open the file $data = fread($file, filesize($filename)); fclose($file); return unserialize($data);//return the values } else return false;}else return false;//was expired you need to create new }}?>
- php cache类
- php Cache 类
- php cache 缓存方法类一
- php cache 缓存方法类二
- ThinkPHP3.1.3核心类 Cache.class.php
- php cache类代码(php数据缓存类)
- APC(Alternative PHP Cache)
- PHP Cache-control
- php apc cache 缓存
- PHP Caching / Cache || FILTERS
- PHP cache应用
- PHP的realpath cache
- 个人技术作品 - PHP Page Cache 页面缓存类
- cache---php控制浏览器缓存
- Memcache类(From ThinkPHP/Lib/Think/Util/Cache/CacheMemcache.class.php)
- php性能研究--cache(一)
- php性能研究--cache(二)
- php性能研究--cache(三)
- C语言时间日期函数总结
- C语言有string吗?
- 三星S5368成功Root
- 自然语言处理之字符串距离
- Windows Phone开发精华文章
- php Cache 类
- 三星S5368 ZMKL1官方ROM包下载+刷机教程
- 触摸板的映话 之 打造可持续学习动力
- 关于Linux log机制的一些整理
- 不知不觉中开始有点迂腐了
- Qt QRegExp正则表达式学习及应用
- 停等协议学习
- eyeBeam采用TCP传输的一个BUG
- C#学习笔记