<?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. sikuli常用方法学习

    Screen s = new Screen(); 1.在文本框中填入文本 以下两个方法都可以 type是用来在文本框中输入指定的文本 paste是用来在文本框中复制指定的文本 s.type(imgpa ...

  2. C Primer Plus(第五版)7

    第 7 章 C 控制语句:分支和跳转 在本章中你将学习下列内容: · 关键字:if(如果),else(否则),switch(切换),continue(继续),break(中断), case(情况),d ...

  3. .net中三种数据类型转换区别((int),Int32.Parse() 和 Convert.toInt32() )

    (typename)valuename,是通用方法: Convert类提供了灵活的类型转换封装: Parse方法,适用于向数字类型的转换. 例如,(int),Int32.Parse() 和 Conve ...

  4. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  5. [原]iptables的NAT策略

    #*nat #:PREROUTING ACCEPT [:] #:POSTROUTING ACCEPT [:] #:OUTPUT ACCEPT [:] # #-A PREROUTING –s IP1 - ...

  6. Oracle逻辑读详解

    1.物理读(physical read) 当数据块第一次读取到,就会缓存到buffer cache 中,而第二次读取和修改该数据块时就在内存buffer cache 了 以下是例子: 1.1  第一次 ...

  7. 在iis中注册.net framework

    首先定位到文件夹:cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 执行命令:aspnet_regiis.exe -i

  8. 菜鸟-手把手教你把Acegi应用到实际项目中(12)-Run-As认证服务

    有这样一些场合,系统用户必须以其他角色身份去操作某些资源.例如,用户A要访问资源B,而用户A拥有的角色为AUTH_USER,资源B访问的角色必须为AUTH_RUN_AS_DATE,那么此时就必须使用户 ...

  9. Android——ScrollView

    1.activity_scrollview.xml <?xml version="1.0" encoding="utf-8"?><Scroll ...

  10. 深度解析国内O2O模式

    今日在网上发现这篇文章很棒,详细的分析了当前BAT矩阵下的o2o 的模式.所以转载过来与大家一起分享. 文章来自于:http://www.siilu.com/20151214/158917.shtml ...