<?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 server远程访问Oracle数据库

    在sql server上新建了连接服务器后 在指定的链接服务器上执行指定的传递查询. 该服务器是 OLE DB 数据源. OPENQUERY 可以在查询的 FROM 子句中引用,就好象它是一个表名. ...

  2. (转)C#使用Mysql记录

    (1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序 http://dev.mysql.com/downloads/connector/ 我下载的版本为: mysql-connector ...

  3. Spark Standalone模式伪分布式环境搭建

    前提:安装好jdk1.7,hadoop 安装步骤: 1.安装scala 下载地址:http://www.scala-lang.org/download/ 配置环境变量: export SCALA_HO ...

  4. VS2010 MSDN配置

    安装VS2010之后总是要装MSDN的,不然写起程序来还真不方便.前段时间换了电脑后,折腾了好久才把VS和MSDN装好,所以为了方便自己和别人特地把配置MSDN的详细步骤写出来: 1.         ...

  5. 翻译:深入 AngularUI Router

    原文地址:http://www.ng-newsletter.com/posts/angular-ui-router.html ui-router: https://angular-ui.github. ...

  6. Android开发-API指南-创建 Content Provider

    Creating a Content Provider 英文原文:http://developer.android.com/guide/topics/providers/content-provide ...

  7. c# 获取excel所有工作表

    var filePath="f:\xx.xlsx" string connStr = "Provider=Microsoft.Ace.OleDb.12.0;" ...

  8. [linux] 替换字符串

    Linux下批量替换多个文件中的字符串的简单方法.用sed命令可以批量替换多个文件中的字符串. 命令如下:sed -i “s/原字符串/新字符串/g” `grep 原字符串 -rl 所在目录` 例如: ...

  9. Android基础总结(1)

    1.Android开发的特点 四大组件:活动(Activity).服务(Service).广播接收器(Broadcast Receiver).内容提供器(Content Provider).其中活动是 ...

  10. 【EF 5】结合项目实战分析EF三大工作模式之—Database First

    导读:所谓的EF的Databasefirst工作模式,是目前我们(不涉及社会领域)用的最广的一种模式,也是本次ITOO开发所采用的工作模式.本篇博客,就分析在项目中通过Database First模式 ...