首页 > php教程 > php手册 > 正文

PHP一个小巧的缓存类

php中文网
发布: 2016-06-13 10:36:37
原创
1044人浏览过

功能很简单,就是缓存整个页面,可以设定缓存时间,可以缓存特定的url,例如:test.php?id=12,当目标文件更新时,如test.php,缓存文件也会更新,即使仍处于缓存期内。

Koobi Pro
Koobi Pro

主要功能: 无限级分类,商品可在各类别间自由转移; 组合商品概念,可以用于组配商品销售(比如服装鞋帽的颜色、尺码大小等),组合销售等销售方式; 商品的自定义属性功能,商品类别扩展属性,满足商品多属性需求(比如某一笔记本电脑,可以有cpu、内存、显示屏、硬盘等等扩展属性); 按照商品类别查看热卖、特价,允许按每个类别增加当前类别的热卖、特价等商品; 会员分级功能,会员积分功能。可根据会

Koobi Pro 0
查看详情 Koobi Pro

class cache
{
var $cache_dir = ./cache/;//This is the directory where the cache files will be stored;
var $cache_time = 120;//How much time will keep the cache files in seconds.

var $caching = false;
var $file = ;

function cache()
{
//Constructor of the class
$this->file = $this->cache_dir . urlencode( $_SERVER[REQUEST_URI] );
if(file_exists($this->file)) $expired = $this->check_expire();
else $expired = false;
if ( file_exists ( $this->file ) && ( filemtime ( $this->file ) + $this->cache_time ) > time() && !$expired )
{
//Grab the cache:
$handle = fopen( $this->file , "r");
do {
$data = fread($handle, 8192);
if (strlen($data) == 0) {
break;
}
echo $data;
} while (true);
fclose($handle);
exit();
}
else
{
//create cache :
$this->caching = true;
ob_start();
$now = time();
echo " ";
}
}

function close()
{
//You should have this at the end of each page
if ( $this->caching )
{
//You were caching the contents so display them, and write the cache file
$data = ob_get_clean();
echo $data;
$fp = fopen( $this->file , w );
fwrite ( $fp , $data );
fclose ( $fp );
}
}
function check_expire(){
$fp = fopen($this->file,"r");
preg_match("/:([d]+)-/",fread($fp,200),$time);
$modify_time = $time[1];
if($modify_time return true;
}
else{
return false;
}

}
}


用法:

//Example :
$ch = new cache();
echo date("D M j G:i:s T Y");
$ch->close();

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号