<?php
/**
* @desc 文件缓存
*/
class Cache{
const C_FILE = '/Runtime/';
private $dir = '';
const EXT = '.tpl';
private $filename = '';
public function __construct($dir = ''){
$this->dir = $dir; }
/**
* @desc 设置文件缓存
* @param string $key 文件名
* @param unkonw $data 缓存数据
* @param int $expire 过期时间
*/
public function set($key,$data,$expire = 0){
$this->filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;
if(file_exists($this->filename)){
$res = $this->get($key);
if(md5($res) == md5(json_encode($data) ) ){
return true;
}
}
if(!is_dir(dirname($this->filename))){
mkdir(dirname($this->filename),0777);
} $source = fopen($this->filename,'w+');
fwrite($source,json_encode($data));
fclose($source);
} /**
* @desc 获取文件
* @param string $key 文件名
*/
public function get($key){
//$filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;
if(!file_exists($this->filename)){
return '缓存文件已经不存在';
}else{
$res = file_get_contents($this->filename);
}
return $res;
}
/**
* @desc 删除文件
* @param string $key 文件名
*/
public function del($key){
unlink($this->filename);
} } $data = array('name'=>'song','age'=>20,'sex'=>'man','favority'=>array('apple','banana'));
$cache = new Cache();
$cache->set('cache',$data);
//$cache->get('cache');
//$cache->del('cache');

PHP文件缓存类的更多相关文章

  1. php 文件缓存类

    //文件缓存类 class FileCache { private $cacheTime = 3600; //默认缓存时间 秒 private $cacheDir = './filecache'; / ...

  2. 一个不错的PHP文件页面缓存类

    在php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考. 页面缓存类 <?php    /*    * 缓存类    cac ...

  3. php 文件缓存

    http://www.oschina.net/code/snippet_162279_6098 <?php class cache {       private static $_instan ...

  4. php文件缓存

    1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...

  5. android文件缓存,并SD卡创建目录未能解决和bitmap内存溢出解决

    1.相关代码: 加入权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ...

  6. C#之文件缓存

    写在开头 今天就放假了,照理说应该写今年的总结了,但是回头一看,很久没有写过技术类的文字了,还是先不吐槽了. 关于文件缓存 写了很多的代码,常常在写EXE(定时任务)或者写小站点(数据的使用和客户端调 ...

  7. php文件缓存方法总结

    为大家分享很全的php文件缓存,供大家参考,具体内容如下 <?php class cache {  private static $_instance = null;    protected ...

  8. php文件缓存数据

    最近在做微信的摇一摇跑马活动,实现原理是用户摇动手机,通过ajax往数据库写入数据(小马跑的步数),然后PC端用过ajax每一秒钟从数据库中调取一次数据(小马跑的步数),然后显示在PC屏幕上,这样就会 ...

  9. 很实用的php的缓存类文件示例

    http://www.php.cn/php-weizijiaocheng-376603.html <?php /* * 缓存类 cache */ class cache { //缓存目录 var ...

随机推荐

  1. Struts1.x下使用jquery的Ajax获取后台数据

        jquery中有多种Ajax方法来获取后台数据,我使用的是$.get()方法,具体的理论我不解释太多,要解释也是从别的地方copy过来的.下面就介绍我的项目中的实现方法.     前台页面: ...

  2. android listview Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

    android listview 适配器在多种类型viewType报错: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; ...

  3. Django中文无法转换成latin-1编码的解决方案

    在Ubuntu上用Django做Web开发的时候遇到了中文保存到Cookie无法解析的问题,经过了下面几个步骤终于把问题解决了: 修改/usr/lib/python3.4/wsgiref/header ...

  4. NodeJS和C++的性能比较(转)

    原文地址: http://www.web-tinker.com/article/20374.html 前段时间做了个实验,测试了1E9次的空循环在NodeJS和C++中的执行用时.于是我和小伙伴们瞬间 ...

  5. 身份证校验程序(上)- 零基础入门学习Delphi48

    身份证校验程序 让编程改变世界 Change the world by program [caption id="attachment_2699" align="alig ...

  6. GitLab CI

    GitLab CI持续集成配置方案   目录 1. 持续集成介绍 1.1 概念 1.2 持续集成的好处 2. GitLab持续集成(CI) 2.1 简介 2.2 GitLab简单原理图 2.3 Git ...

  7. curl fake ip

    curl --header "X-Forwarded-For: 219.137.148.2" "http://www.x.com"

  8. windows 7 系统进程服务详解

    windows 7已经发布有段时间了,相信很多网友都已经换上了传说中非常完美的win7系统.win7不仅继承而且还超越了vista的美观界面,性能优化方面也下足了功力.还拥有强大的win xp兼容性, ...

  9. 编译boost python模块遇到的错误:../../libraries/boost_1_44_0/boost/python/detail/wrap_python.hpp:75:24: fatal error: patchlevel.h: No such file or directory

    就是遇到类似标题上面的错误. 原因是没有安装对应python的python-dev依赖,不然编译到boost python模块的时候就会出错. 所以解决方案是sudo apt-get install ...

  10. grok 官方文档

    <pre name="code" class="html">grok: 解析任意文本并构造它: Grok 是当前最好的方式在logstash 解析蹩 ...