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. Lambda 表达式有何用处?如何使用?

    强烈推荐!!!   作者:Sevenvidia链接:https://www.zhihu.com/question/20125256/answer/324121308来源:知乎著作权归作者所有.商业转载 ...

  2. ecplise 修改编码

    1.修改eclipse默认工作空间编码方式 window->preferences->general->workspace 2.修改工程编码方式 项目右键->propertie ...

  3. 洛谷P1848 书架

    好,我一直以为书架是splay,然后发现还有个优化DP的书架.妃的书架 蓝书和PPT上面都讲了,应该比较经典吧. 题意: 有n个物品,每个都有宽,高. 把它们分成若干段,使得每段的最大值的总和最小.且 ...

  4. c#中数组的总结

    Array数组: 基本定义:string[] Scences={"sky","star","sun"}  或者 int[] Scences= ...

  5. 绑定本地的Session

    绑定本地的Session图示解析: 代码的结构: 代码: SaveServlet.java package com.itheima.servlet; import java.io.IOExceptio ...

  6. 10款 Mac 系统优化清理工具软件推荐和下载

    本文内容及图片来源[风云社区 SCOEE] 在Windows上有各种安全卫士.系统助手等系统优化和清理工具,比如360安全卫士.腾讯安全管家等,同样MacOS系统也有很多好用的系统优化清理工具,体验比 ...

  7. Linux报错

    Linux报错 ------------------- 在VMware虚拟机中配置yum源时,执行 mount /dev/cdrom /mnt/cdrom 出现 mount: no medium fo ...

  8. Docker笔记一:Docker介绍

    目录 什么是Docker? Docker的核心概念 Docker镜像命令 Docker容器命令 Docker实战 查看我的镜像 启动Redis Docker中国镜像加速 血与泪的教训 什么是Docke ...

  9. Java Web之下载文件

    下载的文件,不能随便的被访问,放在外面的文件夹肯定不行,url一敲就能访问了,所以我们要放在WEB-INF文件夹里面,WEB-INF文件夹只有Servlet才能访问,我们新建一个文件夹,叫downlo ...

  10. 11、JDBC-Druid

    依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...