https://www.jb51.net/article/73836.htm

直接看代码吧!
使用说明:
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...
}

 

Cache.class.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="cache/"){
$this->cache_expire=$exp_time;
$this->cache_path=$path;
} //returns the filename for the cache
private 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 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
}
}
?>

 

一个简单至极的PHP缓存类代码的更多相关文章

  1. Qt5.9一个简单的多线程实例(类QThread)(第一种方法)

    Qt开启多线程,主要用到类QThread.有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run().当要开启新线程时,只需要实例该类,然后调用函数start(),就可以开启一条多线程 ...

  2. 实现一个简单的http请求工具类

    OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...

  3. 使用QT实现一个简单的登陆对话框(纯代码实现C++)

    使用QT实现一个简单的登陆对话框(纯代码实现C++) 效果展示 使用的QT控件 控件 描述 QLable 标签 QLineEdit 行文本框 QPushButton 按扭 QHBoxLayout 水平 ...

  4. 简单的php数据库操作类代码(增,删,改,查)

    这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...

  5. 一个简单小巧的CSV读取类

    最近在基于亚马逊MWS API做一些服务,需要读取亚马逊返回的报表,是一个按照\t分割的文本,所以就封装了一个简单小巧的CsvReader类 使用方法 使用方法非常简单,只需要传递一个stream子类 ...

  6. 一个简单IP防刷工具类, x秒内最多允许y次单ip操作

    IP防刷,也就是在短时间内有大量相同ip的请求,可能是恶意的,也可能是超出业务范围的.总之,我们需要杜绝短时间内大量请求的问题,怎么处理? 其实这个问题,真的是太常见和太简单了,但是真正来做的时候,可 ...

  7. Java下一个简单的数据库分库帮助类

    简介    前面两篇文章主要讲了数据库读写分离和分表分库的一些问题,这篇文章主要讲一下我个人实现的一个分表分库项目.     在此之前,我有写过一个.Net的分库,最近在做Java的项目,就顺便做出一 ...

  8. 【如何快速的开发一个简单的iOS直播app】(代码篇)

    开篇([如何快速的开发一个完整的iOS直播app](原理篇)) 好久没写简书,因为好奇的我跑去学习直播了,今天就分享一下我的感慨. 目前为止直播还是比较热点的技术的,简书,git上有几篇阅读量和含金量 ...

  9. 一个简单的异常/条件重试类(C#)

    单个类,简单好用 using System; using System.Linq.Expressions; using System.Threading; using System.Threading ...

随机推荐

  1. ACM-ICPC 2015 ChangChun

    比赛链接 :点击这里 大概会写 F G J L 吧 F 给你一个序列 最多删除一个数使他构成 最长不上升或者不下降子序列 这题不会不会on的算法只能 t*n*logn 了 还是压常过 求两次 LIS ...

  2. Building real-time dashboard applications with Apache Flink, Elasticsearch, and Kibana

    https://www.elastic.co/cn/blog/building-real-time-dashboard-applications-with-apache-flink-elasticse ...

  3. 洛谷P3674 小清新人渣的本愿

    题意:多次询问,区间内是否存在两个数,使得它们的和为x,差为x,积为x. n,m,V <= 100000 解: 毒瘤bitset...... 假如我们有询问区间的一个桶,那么我们就可以做到O(n ...

  4. A1108. Finding Average

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  5. PMP证书的获取,不知道10大注意事项会吃亏

    作为一个已经考过PMP的小项目经理我来说,近来接到不少咨询PMP的,有咨询考试事宜的,也有咨询后续的换审和PDU的,今天我这边就说说PMP项目管理证书要获取的一些注意事项,不注意的话可是会吃大亏的. ...

  6. zlib库交叉编译

    zlib-1.2.11 开发板:arm9 交叉编译器arm-fsl-linux-gnueabihf-gcc 编译方式: ./configure -h可以发现zlib并没有提供CC配置,所以 (1)ex ...

  7. 做错的题目——给Array附加属性

  8. testng报告-extentsReports使用-klov

    extentreport部分参考: https://blog.csdn.net/Care_sQueendom/article/details/78651950 https://testerhome.c ...

  9. 4.1、实现4个LED灯同时闪烁

    图中可以看出,P1的0.1.3.4引脚分别连接着4个LED.控制引脚状态,来控制LED. #include "ioCC2530.h" //引用CC2530头文件 /******** ...

  10. C++(3):./Encryptor: undefined symbol:Z11startserviceLAKJDFLJALDKJFLLLLL

    在Linux下编译得Encryptor文件,但是执行./Encryptor时 报了如上错误:undefined symbol:Z11startserviceLAKJDFLJALDKJFLLLLL 原因 ...