ThinkPHP文件缓存类代码分享
更新时间:2015年04月22日 09:35:24 投稿:hebedich
本文给大家分享的是取自ThinkPHP中的关于文件缓存类的代码,非常的实用,效率也非常不错,这里推荐给大家,有需要的小伙伴参考下。
取自ThinkPHP的文件缓存类代码,这里就不多废话了,小伙伴们自己看注释吧。
<?php /** * @desc 文件缓存 */ class Cache{ const C_FILE = '/Runtime/'; private $dir = ''; const EXT = '.tpl'; private $filename = ''; public function __construct($dir = ''){ $this->dir = $dir; } /** * @desc 设置文件缓存 * @param string $key 文件名 * @param unkonw $data 缓存数据 * @param int $expire 过期时间 */ public function set($key,$data,$expire = 0){ $this->filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT; if(file_exists($this->filename)){ $res = $this->get($key); if(md5($res) == md5(json_encode($data) ) ){ return true; } } if(!is_dir(dirname($this->filename))){ mkdir(dirname($this->filename),0777); } $source = fopen($this->filename,'w+'); fwrite($source,json_encode($data)); fclose($source); } /** * @desc 获取文件 * @param string $key 文件名 */ public function get($key){ //$filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT; if(!file_exists($this->filename)){ return '缓存文件已经不存在'; }else{ $res = file_get_contents($this->filename); } return $res; } /** * @desc 删除文件 * @param string $key 文件名 */ public function del($key){ unlink($this->filename); } } $data = array('name'=>'song','age'=>20,'sex'=>'man','favority'=>array('apple','banana')); $cache = new Cache(); $cache->set('cache',$data); //$cache->get('cache'); //$cache->del('cache');
您可能感兴趣的文章:
- ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
- 获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)
- thinkphp跨库操作的简单代码实例
- thinkPHP2.1自定义标签库的导入方法详解
- ThinkPHP分组下自定义标签库实例
- ThinkPHP使用UTFWry地址库进行IP定位实例
- Thinkphp调用Image类生成缩略图的方法
- ThinkPHP调用百度翻译类实现在线翻译
- thinkPHP引入类的方法详解
- thinkPHP自定义类实现方法详解
- thinkphp实现无限分类(使用递归)
- ThinkPHP进程计数类Process用法实例详解
- thinkPHP简单调用函数与类库的方法
相关文章
基于Laravel Auth自定义接口API用户认证的实现方法
这篇文章主要给大家介绍了基于Laravel Auth自定义接口API用户认证的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2018-07-07Yii2 hasOne(), hasMany() 实现三表关联的方法(两种)
这篇文章主要介绍了Yii2 hasOne(), hasMany() 实现三表关联的方法(两种),非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-02-02thinkphp3.2中Lite文件替换框架入口文件或应用入口文件的方法
这篇文章主要介绍了thinkphp3.2中Lite文件替换框架入口文件或应用入口文件的方法,涉及ThinkPHP相关配置技巧,需要的朋友可以参考下2015-05-05
最新评论