cari
Rumahphp教程php手册一个PHP缓存类,附三个实例Demo代码

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

Copy to Clipboard引用的内容:[www.bkjia.com]
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 "This is Cache. ";
//以下两种方式,哪种方式好?????
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 '
'.$str.'
';
}
}
?>

以下为三个实例DEMO,请将以下代码按demo.php或其它文件名保存,注意文件路径,别弄错哦。

/*
* 可自由转载使用,请保留版权信息,谢谢使用!
* Class Name : Cache (For Php5)
* Version : 1.0
* Description : 动态缓存类,用于控制页面自动生成缓存、调用缓存、更新缓存、删除缓存.
* Author : jiangjun8528@163.com,Junin
* Author Page : http://blog.csdn.Net/sdts/
* 源码来自:烈火下载 http://www.bkjia.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); //首次运行或缓存过期,生成缓存
*/
?>

Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Alat panas

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Muat turun versi mac editor Atom

Muat turun versi mac editor Atom

Editor sumber terbuka yang paling popular

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Persekitaran pembangunan bersepadu PHP yang berkuasa

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Dreamweaver Mac版

Dreamweaver Mac版

Alat pembangunan web visual