一个PHP缓存类,附三个实例Demo代码,本文由小编收集于网络,首先我们看到的是cache.inc.php文件,请大家将以下代码保存为: cache.inc.php。

class Cache {
/**
* $dir : 缓存文件存放目录
* $lifetime : 缓存文件有效期,单位为秒
* $cacheid : 缓存文件路径,包含文件名
* $ext : 缓存文件扩展名(可以不用),这里使用是为了查看文件方便
*/
private $dir;
private $lifetime;
private $cacheid;
private $ext;
/**
* 析构函数,检查缓存目录是否有效,默认赋值
*/
function __construct($dir=”,$lifetime=1800) {
if ($this->dir_isvalid($dir)) {
$this->dir = $dir;
$this->lifetime = $lifetime;
$this->ext = ‘.Php’;
$this->cacheid = $this->getcacheid();
}
}
/**
* 检查缓存是否有效
*/
private function isvalid() {
if (!file_exists($this->cacheid)) return false;
if (!(@$mtime = filemtime($this->cacheid))) return false;
if (mktime() – $mtime > $this->lifetime) return false;
return true;
}
/**
* 写入缓存
* $mode == 0 , 以浏览器缓存的方式取得页面内容
* $mode == 1 , 以直接赋值(通过$content参数接收)的方式取得页面内容
* $mode == 2 , 以本地读取(fopen ile_get_contents)的方式取得页面内容(似乎这种方式没什么必要)
*/
public function write($mode=0,$content=”) {
switch ($mode) {
case 0:
$content = ob_get_contents();
break;
default:
break;
}
ob_end_flush();
try {
file_put_contents($this->cacheid,$content);
}
catch (Exception $e) {
$this->error(‘写入缓存失败!请检查目录权限!’);
}
}
/**
* 加载缓存
* exit() 载入缓存后终止原页面程序的执行,缓存无效则运行原页面程序生成缓存
* ob_start() 开启浏览器缓存用于在页面结尾处取得页面内容
*/
public function load() {
if ($this->isvalid()) {
echo "<span style=’display:none;’>This is Cache.</span> ";
//以下两种方式,哪种方式好?????
require_once($this->cacheid);
//echo file_get_contents($this->cacheid);
exit();
}
else {
ob_start();
}
}
/**
* 清除缓存
*/
public function clean() {
try {
unlink($this->cacheid);
}
catch (Exception $e) {
$this->error(‘清除缓存文件失败!请检查目录权限!’);
}
}
/**
* 取得缓存文件路径
*/
private function getcacheid() {
return $this->dir.md5($this->geturl()).$this->ext;
}
/**
* 检查目录是否存在或是否可创建
*/
private function dir_isvalid($dir) {
if (is_dir($dir)) return true;
try {
mkdir($dir,0777);
}
catch (Exception $e) {
$this->error(‘所设定缓存目录不存在并且创建失败!请检查目录权限!’);
return false;
}
return true;
}
/**
* 取得当前页面完整url
*/
private function geturl() {
$url = ”;
if (isset($_SERVER[‘REQUEST_URI’])) {
$url = $_SERVER[‘REQUEST_URI’];
}
else {
$url = $_SERVER[‘Php_SELF’];
$url .= empty($_SERVER[‘QUERY_STRING’])?”:’?’.$_SERVER[‘QUERY_STRING’];
}
return $url;
}
/**
* 输出错误信息
*/
private function error($str) {
echo ‘<div style="color:red;">’.$str.'</div>’;
}
}
?>
以下为三个实例DEMO,请将以下代码按demo.php或其它文件名保存,注意文件路径,别弄错哦。
<?php
/*
* 可自由转载使用,请保留版权信息,谢谢使用!
* Class Name : Cache (For Php5)
* Version : 1.0
* Description : 动态缓存类,用于控制页面自动生成缓存、调用缓存、更新缓存、删除缓存.
* Author : jiangjun8528@163.com,Junin
* Author Page : http://blog.csdn.Net/sdts/
* 源码来自:下载 http://www.veryhuo.com/down
* Last Modify : 2007-8-22
* Remark :
1.此版本为Php5版本,本人暂没有写Php4的版本,如需要请自行参考修改(比较容易啦,不要那么懒嘛,呵呵!).
2.此版本为utf-8编码,如果网站采用其它编码请自行转换,Windows系统用记事本打开另存为,选择相应编码即可(一般ANSI),Linux下请使用相应编辑软件或iconv命令行.
3.拷贝粘贴的就不用管上面第2条了.
* 关于缓存的一点感想:
* 动态缓存和静态缓存的根本差别在于其是自动的,用户访问页面过程就是生成缓存、浏览缓存、更新缓存的过程,无需人工操作干预.
* 静态缓存指的就是生成静态页面,相关操作一般是在网站后台完成,需人工操作(也就是手动生成).
*/
/*
* 使用方法举例
————————————Demo1——————————————-
require_once(‘cache.inc.php’);
$cachedir = ‘./Cache/’; //设定缓存目录
$cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
if ($_GET[‘cacheact’] != ‘rewrite’) //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
$cache->load(); //装载缓存,缓存有效则不执行以下页面代码
//页面代码开始
echo date(‘H:i:s jS F’);
//页面代码结束
$cache->write(); //首次运行或缓存过期,生成缓存
————————————Demo2——————————————-
require_once(‘cache.inc.php’);
$cachedir = ‘./Cache/’; //设定缓存目录
$cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
if ($_GET[‘cacheact’] != ‘rewrite’) //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
$cache->load(); //装载缓存,缓存有效则不执行以下页面代码
//页面代码开始
$content = date(‘H:i:s jS F’);
echo $content;
//页面代码结束
$cache->write(1,$content); //首次运行或缓存过期,生成缓存
————————————Demo3——————————————-
require_once(‘cache.inc.php’);
define(‘CACHEENABLE’,true);
if (CACHEENABLE) {
$cachedir = ‘./Cache/’; //设定缓存目录
$cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
if ($_GET[‘cacheact’] != ‘rewrite’) //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
$cache->load(); //装载缓存,缓存有效则不执行以下页面代码
}
//页面代码开始
$content = date(‘H:i:s jS F’);
echo $content;
//页面代码结束
if (CACHEENABLE)
$cache->write(1,$content); //首次运行或缓存过期,生成缓存
*/
?>
波比源码 » 一个PHP缓存类,附三个实例Demo代码
levaquin 250mg price levaquin 250mg oral
purchase dutasteride pills tamsulosin oral cost zofran 8mg
generic aldactone 25mg order valacyclovir 1000mg generic buy diflucan 200mg sale
buy ampicillin 500mg pill order cephalexin 500mg for sale erythromycin 500mg pills
fildena 100mg cost order nolvadex 20mg generic methocarbamol 500mg generic
buy suhagra without prescription buy sildalis for sale estrace 1mg cost
buy lamictal 50mg pills vermox over the counter cheap tretinoin
brand tadalafil buy avanafil 200mg for sale buy voltaren 100mg sale
buy isotretinoin 20mg online azithromycin online buy zithromax 250mg sale
purchase indocin sale trimox 500mg pills order trimox 500mg without prescription
buy generic accutane 40mg order zithromax generic ivermectin 6
provigil 100mg pills diamox over the counter generic diamox 250mg
doxycycline 200mg drug furosemide online buy order generic furosemide
clonidine cost meclizine 25 mg ca spiriva 9 mcg sale
buspirone 10mg oral order phenytoin 100mg online order ditropan 2.5mg pill
order terazosin 5mg sale arava 10mg tablet sulfasalazine 500 mg us
buy prograf 1mg brand prograf buy ursodiol 150mg pill
buy imdur 40mg pills lanoxin sale micardis us
buy zyban 150 mg purchase cetirizine generic buy quetiapine 100mg online
buy molnupiravir 200 mg generic buy generic cefdinir 300mg lansoprazole 30mg tablet
sertraline 50mg cheap zoloft 50mg oral sildenafil 100mg for sale
buy salbutamol for sale viagra without prescription purchase sildenafil pill
naltrexone 50 mg tablet albendazole over the counter aripiprazole 20mg brand
buy medroxyprogesterone 5mg online cheap buy medroxyprogesterone 5mg without prescription oral periactin
purchase modafinil without prescription provigil ca ivermectin lotion for scabies
accutane 40mg for sale order amoxicillin online cheap buy deltasone 5mg pill
order azithromycin without prescription buy prednisolone 5mg generic cheap neurontin generic
furosemide brand oral doxycycline 100mg hydroxychloroquine medication
chloroquine 250mg tablet oral cenforce 50mg baricitinib 2mg price
itraconazole for sale online order prometrium buy tindamax 500mg without prescription
order glycomet 500mg generic atorvastatin 10mg price buy cialis 10mg generic
prilosec 10mg usa prilosec for sale online free slot
get assignments done online how to write an essay about my life play poker online free no sign up
vardenafil 10mg drug methylprednisolone 4 mg for sale cost methylprednisolone
order triamcinolone 4mg online claritin uk buy clarinex 5mg sale
tadalafil 5mg cheap us pharmacy viagra sildenafil 50mg cost
cheap cialis tablets order inderal 20mg generic plavix buy online
order zyloprim 100mg oral rosuvastatin 20mg ezetimibe usa
methotrexate generic buy methotrexate online metoclopramide 20mg over the counter
order domperidone for sale purchase domperidone sale cost cyclobenzaprine 15mg
order baclofen 10mg without prescription lioresal without prescription order generic toradol 10mg
imitrex 25mg over the counter order generic sumatriptan avodart 0.5mg sale
cost colchicine online blackjack for real money usa play casino slots
zantac 150mg brand ranitidine sale purchase celecoxib for sale
buy tamsulosin pills cost flomax order aldactone 25mg online cheap
order tadalafil 5mg without prescription cipro price order ciprofloxacin 500mg without prescription
purchase metronidazole for sale order augmentin 375mg order bactrim 480mg pill
order diflucan online cheap fluconazole 200mg purchase viagra for sale
cialis super active purchase viagra sale viagra 50 mg
order cefuroxime generic bimatoprost oral methocarbamol price
sildalis ca sildenafil 50mg for sale buy generic lamotrigine 200mg
online gambling games best ed pills online order tadalafil 10mg online cheap
zithromax 500mg price brand gabapentin buy neurontin 100mg pills
cheap furosemide 40mg lasix brand order generic plaquenil 400mg
fildena 100mg canada rhinocort online buy purchase rhinocort online cheap
purchase prednisone deltasone 10mg drug mebendazole pills
retin without prescription buy tretinoin online buy avana 100mg online cheap
tadalafil 10mg price tadalafil 20mg oral cost indocin 75mg
purchase naprosyn pill omnicef 300mg usa prevacid 15mg pills
order tiotropium bromide pill minocycline over the counter order hytrin generic
proventil 100 mcg brand proventil 100 mcg sale purchase ciprofloxacin
montelukast price buy sildenafil 50mg sale sildenafil overnight
order cialis 10mg online buy tadalafil 5mg pills cialis 10mg brand
adalat pills aceon medication buy fexofenadine 120mg generic
mesalamine 400mg pill order avapro 150mg online avapro price
order diamox 250 mg pill acetazolamide brand imuran 25mg generic
buy temovate cheap temovate buy generic amiodarone
lanoxin 250mg cost micardis order online order molnunat without prescription
buy priligy 30mg pill avanafil oral order motilium online
alendronate for sale order furadantin online motrin 400mg generic
indocin 50mg oral buy indocin 50mg generic brand cenforce
buy nortriptyline sale order paroxetine online cheap paxil usa
order doxycycline for sale order medrol sale buy medrol for sale
famotidine pills prograf order online buy remeron online
order generic tadalafil tadacip 20mg us trimox 250mg drug
generic fenofibrate 160mg viagra 100mg cheap viagra online
cialis 5mg brand cheap erectile dysfunction pill cheap ed pills
order glucophage pills glucophage 1000mg uk order tamoxifen 10mg pills
order modafinil 200mg pill stromectol 6mg canada buy promethazine generic
prednisone online cheap generic amoxicillin amoxil 1000mg usa
buy fildena 50mg for sale cost lyrica 150mg cost propecia
zofran price purchase amoxicillin for sale buy bactrim 480mg sale
order ventolin online cheap ventolin 4mg pill order augmentin 1000mg online
buy generic provigil 100mg metoprolol uk buy metoprolol 50mg online cheap
order avodart for sale order keflex 250mg pill order generic xenical 60mg
oxybutynin cheap oxcarbazepine pills order trileptal 600mg sale
rhetorical analysis thesis thesis layout phd thesis example
great thesis statement examples persuasive thesis statement what’s a thesis statement
examples of a good thesis statement thesis example which of the following is the most effective thesis statement?
argument thesis examples hawking thesis thesis statement\