• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php简单数据缓存类

php 搞代码 3年前 (2022-01-23) 16次浏览 已收录 0个评论

公司手机触屏站 ,由于页面图片太多,所以需要做数据缓存,就随便写一个数据缓存类。

直接贴代码

<?php
/**
*
* [email protected]
* 缓存类
* 把数据查询出,并序列化写入文件
**/
class Cache{
function __construct($config){
//定义是否开启缓存
$this->is_cache=$config[‘is_cache’];
//定义缓存目录
$this->cache_file=$config[‘cache_file’];
//定义缓存时间
$this->cache_time=$config[‘cache_time’];
}
//读取缓存文件
public function open($name){
$arr=array();
$filename=$this->cache_file.$name;
$status=filemtime($filename)+$this->cache_time>time();//定义缓存时间
if( file_exists($filename) && $status && $this->is_cache){
$c//读取缓存文件
$arr=unserialize($content);
return $arr;
}else{
return false;
}
}
//写入缓存文件
public function write($name,$data=array()){
$filename=$this->cache_file.$name;
$c
file_put_contents($filename, $content);//写入缓存文件
}

}

?>

其实无非就是,把select的数组 然后序列化 放进文本中 然后读出来。

使用方法

//定义缓存是否开启
require(‘cache.class.php’);
$c /> ‘is_cache’=>1,//是否开启缓存
‘cache_file’=>’./cache/’,//缓存文件夹
‘cache_time’=>’60’,//缓存时间
);
$cache=new Cache($config);

//打开缓存,传入缓存文本文@来#源gaodai$ma#com搞$$代**码网搞代gaodaima码件名字

$row=$cache->open($filename);

//写入缓存传入文件名字 和数据(数组)

$cache->write($filename,$data);

ps:有不懂的 可以给我留言 非囍勿喷,大神绕过,菜鸟学习!

以上就介绍了php简单数据缓存类,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php简单数据缓存类

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址