<?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简单缓存类的更多相关文章

  1. php简单数据缓存类

    公司手机触屏站 ,由于页面图片太多,所以需要做数据缓存,就随便写一个数据缓存类. 直接贴代码 <?php/**** fianl_m@foxmail.com* 缓存类* 把数据查询出,并序列化写入 ...

  2. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  3. 【转】asp.net mvc3 简单缓存实现sql依赖

    asp.net mvc3 简单缓存实现sql依赖   议题 随 着网站的发展,大量用户访问流行内容和动态内容,这两个方面的因素会增加平均的载入时间,给Web服务器和数据库服务器造成大量的请求压力.而大 ...

  4. iOS缓存类的设计

    使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...

  5. 比较全面的一个PHP缓存类解析

    转自:http://www.blhere.com/1164.html 一.引论 PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站 ...

  6. 分享个 之前写好的 android 文件流缓存类,专门处理 ArrayList、bean。

    转载麻烦声明出处:http://www.cnblogs.com/linguanh/ 目录: 1,前序 2,作用 3,特点 4,代码 1,前序  在开发过程中,client 和 server 数据交流一 ...

  7. (实用篇)PHP缓存类完整实例

    本文完整描述了一个简洁实用的PHP缓存类,可用来检查缓存文件是否在设置更新时间之内.清除缓存文件.根据当前动态文件生成缓存文件名.连续创建目录.缓存文件输出静态等功能.对于采用PHP开发CMS系统来说 ...

  8. ASP缓存类收集

    木鸟写的 '********************************************** ' vbs Cache类 ' ' 属性valid,是否可用,取值前判断 ' 属性name,ca ...

  9. 基于Android 下载文件时,更新UI简单帮助类

    因为在项目开发时.有这种简单需求,问谷歌,网络上也有好多Utils工具类,可是比較冗余.自己就简单的写了一个简单帮助类. /** * 下载文件,更新UI简单帮助类 * * @author jarlen ...

随机推荐

  1. [SQL]向3个表插入数据的存储过程 和 C# 代码

    public int UpdateQty(string strPartID, int iQty, int iUpdateQty, string strBarCode, string strCreate ...

  2. systemstate dump 介绍

    systemstate dump 介绍 By Janezhang-Oracle on 十二月 13, 2012 当数据库出现严重的性能问题或者hang了的时候,我们非常需要通过systemstate ...

  3. 海外支付:抵御信用卡欺诈的CyberSource

    海外支付:抵御信用卡欺诈的CyberSource 吴剑 2014-06-04 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian 吴剑 http://www.cn ...

  4. The 2013 ACM-ICPC Asia Changsha Regional Contest - A

    Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print ser ...

  5. TesCase-GUI(图形用户界面)测试

    GUI测试是功能测试的一种表现形式.不仅要考虑GUI本身的测试,也要考虑GUI所表现的系统功能的测试.   GUI应具有的要素 1.符合标准和规范 2.直观性 (1)用户界面是否洁净.不唐突.不拥挤? ...

  6. SQL Server :Stored procedures存储过程初级篇

    对于SQL Server,我是个拿来主义.很多底层的原理并不了解,就直接模仿拿着来用了,到了报错的时候,才去找原因进而逐步深入底层.我想,是每一次的报错,逼着我一点点进步的吧. 近期由于项目的原因,我 ...

  7. TFS客户端登录用户修改

    1.操作环境: Windows 10 Pro Visual Studio 2013 TFS客户端 2.操作背景 安装完成TFS系统后,使用administrator在客户端进行登陆测试(已勾选保存用户 ...

  8. 【PL/SQL练习】游标cursor :oracle 在执行sql语句时,为sql语句所分配的一个私有的内存区域

    隐式游标:一次只能返回一行结果(不需要定义,默认自动建立)  显式游标: 需要开发人员提前定义,可以通过循环的方式处理游标里的sql语句,返回多行结果    隐式游标的属性:   sql%rowcou ...

  9. socket学习笔记——select函数的使用(windows)

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h ...

  10. NTP服务器地址及IP

    yum install ntp */20 * * * * /usr/sbin/ntpdate 61.172.254.29 210.72.145.44 (国家授时中心服务器IP地址)133.100.11 ...