php简单缓存类
<?php
class Cache {
private $cache_path;//path for the cache
private $cache_expire;//seconds that the cache expires
//cache constructor, optional expiring time and cache path
public function Cache($exp_time=3600,$path){
$this->cache_expire=$exp_time;
$this->cache_path=$path;
$this->CreateFolder($path);
}
//returns the filename for the cache
private function fileName($key){
return $this->cache_path.$key;
}
//creates new cache files with the given data, $key== name of the cache, data the info/values to store
public 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 key
public function get($key){
$filename = $this->fileName($key);
if (!file_exists($filename) || !is_readable($filename)){//can't read the cache
return 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
}
public function CreateFolder($dir, $mode = 0777){
if (is_dir($dir) || @mkdir($dir, $mode))
return true;
if (!self::CreateFolder(dirname($dir), $mode))
return false;
return @mkdir($dir, $mode);
}
}
?>
用法如下:
<?php
include 'cache.class.php'; //引入缓存类
$cache_time = '86400'; //设置缓存时间
$cache_path = 'cache/'; //设置缓存路径
$cache_filename = 'cachefile'; //设置缓存文件名
$cache = new Cache($cache_time,$cache_path); //实例化一个缓存类
$key = $cache_filename;
$value = $cache->get($key);
if ($value == false) {
$value = getDataFromDb(); //getDataFromDb是从数据库中读取数据的函数
$cache->put($key, $value); //写入缓存
}
得到$value就是想要的数据 ,根据需求自己写方法。
?>
php简单缓存类的更多相关文章
- php简单数据缓存类
公司手机触屏站 ,由于页面图片太多,所以需要做数据缓存,就随便写一个数据缓存类. 直接贴代码 <?php/**** fianl_m@foxmail.com* 缓存类* 把数据查询出,并序列化写入 ...
- ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core
背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...
- 【转】asp.net mvc3 简单缓存实现sql依赖
asp.net mvc3 简单缓存实现sql依赖 议题 随 着网站的发展,大量用户访问流行内容和动态内容,这两个方面的因素会增加平均的载入时间,给Web服务器和数据库服务器造成大量的请求压力.而大 ...
- iOS缓存类的设计
使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...
- 比较全面的一个PHP缓存类解析
转自:http://www.blhere.com/1164.html 一.引论 PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站 ...
- 分享个 之前写好的 android 文件流缓存类,专门处理 ArrayList、bean。
转载麻烦声明出处:http://www.cnblogs.com/linguanh/ 目录: 1,前序 2,作用 3,特点 4,代码 1,前序 在开发过程中,client 和 server 数据交流一 ...
- (实用篇)PHP缓存类完整实例
本文完整描述了一个简洁实用的PHP缓存类,可用来检查缓存文件是否在设置更新时间之内.清除缓存文件.根据当前动态文件生成缓存文件名.连续创建目录.缓存文件输出静态等功能.对于采用PHP开发CMS系统来说 ...
- ASP缓存类收集
木鸟写的 '********************************************** ' vbs Cache类 ' ' 属性valid,是否可用,取值前判断 ' 属性name,ca ...
- 基于Android 下载文件时,更新UI简单帮助类
因为在项目开发时.有这种简单需求,问谷歌,网络上也有好多Utils工具类,可是比較冗余.自己就简单的写了一个简单帮助类. /** * 下载文件,更新UI简单帮助类 * * @author jarlen ...
随机推荐
- Ext vtype
//form验证中vtype的默认支持类型1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等)2.alphanum//只能输入字母和数字,无法输入其他3.email//email验证, ...
- js注意事项
在数组顶部插入一条数据 data.result.unshift({ Value: 'all', Text: '请选择分类' }); 执行iframe中的javascript方法 window.fram ...
- SGU 176 【带上下界的有源汇的最小流】
---恢复内容开始--- 题意: 给了n个点,m条有向边. 接下来m行,每条边给起点终点与容量,以及一个标记. 标记为1则该边必须满容量,0表示可以在容量范围内任意流. 求: 从源点1号点到终点n号点 ...
- Oracle 数据类型映射C#
Oracle 数据类型映射 下表列出 Oracle 数据类型及其与 OracleDataReader 的映射. Oracle 数据类型 由 OracleDataReader.GetValue 返回的 ...
- 菜鸟-手把手教你把Acegi应用到实际项目中(8)-扩展UserDetailsService接口
一个能为DaoAuthenticationProvider提供存取认证库的的类,它必须要实现UserDetailsService接口: public UserDetails loadUserByUse ...
- Android——Dialog
public class DialogActivity extends Activity { //进度对话框 ProgressDialog progressDialog; @Override ...
- SVN-服务器搭建、apache2整合、eclipse使用
如题,分成3个部分: 1.SVN服务器搭建.操作系统Ubuntu 14.04.2 LTS.具体方法度娘很多,不再细数. 安装:sudo apt-get install subversion 创建版本库 ...
- spark1.2.0版本SparkSQL使用parquet类型注意事项
在Spark1.2.0版本中是用parquet存储类型时注意事项: sql语句: select * from order_created_dynamic_partition_parquet; 在spa ...
- 为什么要使用Spark?
现有的hadoop生态系统中存在的问题 1)使用mapreduce进行批量离线分析: 2)使用hive进行历史数据的分析: 3)使用hbase进行实时数据的查询: 4)使用storm进行实时的流处理: ...
- git学习第一课
##git管理工具学习(Windows7)###1.下载安装git安装包###2.注册github账号①github.com官网注册并创建项目用户名xiaqiubo项目名xiaoxia②复制http ...