加速你的应用程序(缓存类)
时间:2008-10-09
来源:互联网
[php]
<?php
/**
* cache class
*/
class Cache {
/**
* 缓存保存的路径
*
* @var string
*/
var $cache_path;
/**
* 过期时间
*
* @var integer
*/
var $time = 60;
/**
*
* 缓存文件的扩展名
*
* @var String
*/
var $ext = 'cc';
/**
* 构造函数
*
* @param string $cache_path 缓存保存的路径,默认为cache文件夹
* @return Cache
*/
function Cache($cache_path = 'cache') {
if(is_dir($cache_path)) {//如果存在这个文件夹
$this->cache_path = rtrim($cache_path,'/').'/';
} else {
die('cache dir is not exists.');
}
}
/**
* 设置过期时间
*
* @param integer $time 过期时间
* @return boolean
*/
function setTime($time) {
if(isset($time) && is_integer($time)) {//检查$time是否为整数
$this->time = $time;
return true;
} else {
return false;
}
}
/**
* 设置缓存的扩展名
*
* @return Boolean
*/
function setExt($ext) {
if(preg_match('/[a-zA-Z0-9.]{1,}/',$ext)) {
//扩展名只能由字母,数字和.组成
$this->ext = $ext;
return true;
} else {
return false;
}
}
/**
* 读取缓存
*
* @param string $cache_id 要读取缓存的ID
* @return mixed
*/
function read($cache_id) {
$cache_file = $this->cache_path.$cache_id.'.'.$this->ext;//取得缓存的路径
if(!file_exists($cache_file)) {//如果缓存不存在,返回false
return false;
}
$mtime = filemtime($cache_file);//取得缓存的生成时间
if((time() - $mtime) > $this->time) {//判断缓存是否过期
return false;
} else {//读取缓存
$fp = fopen($cache_file,'r');//打开缓存文件
$content = fread($fp,filesize($cache_file));//读取缓存内容
fclose($fp);
unset($fp);
if($content) {
return unserialize($content);//返回反序列化的缓存内容
} else {
return false;
}
}
}
/**
* 写入缓存
*
* @param string $content 缓存内容
* @param string $cache_id 缓存ID
* @return boolean
*/
function write($content,$cache_id) {
$cache_file = $this->cache_path.$cache_id.'.'.$this->ext;//获得缓存文件名
if(file_exists($cache_file)) {//如果存在相同ID的缓存,就把它删除
@unlink($cache_file);
}
$fp = fopen($cache_file,'w');//创建缓存文件
$yon = fwrite($fp,serialize($content));//写入序列化后的缓存内容
fclose($fp);
unset($fp);
$yon?return true:return false;
}
/**
* 清除所有的缓存
*
* @param string $path
* @return boolean
*/
function cleanCache($path = 'cache') {
if(!is_dir($path)) {
$path = $this->cache_path;
}
$path = rtrim($path,'/').'/';
$handler = opendir($path);//打开缓存目录
while (($f = readdir($handler)) !== false) {//循环缓存目录
if(!is_dir($f)) {
if($f != '.' && $f != '..') {
@unlink($path.$f);
}
} else {
$this->cleanCache($f);//递归删除所有的缓存文件
}
}
return true;
}
}
?>
[/php]
使用方法:
<?php
requrie_once('cache.class.php');
requrie_once('db.class.php');
$cache = new Cache('/cache/');
if(!($result = $cache->read('content'))) {
$db = new DB();
$db->query('select * form my_table where id=1');
$result = $db->fetch();
$cache->write($result, 'content');
}
print_r($result);
?>
作者: liexusong 发布时间: 2008-10-09
if(fwrite($fp,serialize($content))) {//写入序列化后的缓存内容
fclose($fp);
unset($fp);
return true;
} else {
fclose($fp);
unset($fp);
return false;
}
这一句有些重复的说,可以这样写
$result = fwrite($fp,serialize($content))
fclose($fp);
unset($fp);
return $result ? true : false;
作者: 飞翔de希望 发布时间: 2008-10-13
* 写入缓存
*
* @param string $content 缓存内容
* @param string $cache_id 缓存ID
* @return boolean
*/
function write($content,$cache_id) {
$cache_file = $this->cache_path.$cache_id.'.'.$this->ext;//获得缓存文件名
if(file_exists($cache_file)) {//如果存在过期的缓存,就把它删除
@unlink($cache_file);
}
$fp = fopen($cache_file,'w');//创建缓存文件
if(fwrite($fp,serialize($content))) {//写入序列化后的缓存内容
fclose($fp);
unset($fp);
return true;
} else {
fclose($fp);
unset($fp);
return false;
}
}[/code]这里的
if(file_exists($cache_file)) {//如果存在过期的缓存,就把它删除
@unlink($cache_file);
}
有判断是否过期吗?
作者: ylcz 发布时间: 2008-10-13
作者: liexusong 发布时间: 2008-10-13
作者: luzhou 发布时间: 2008-10-13
作者: liexusong 发布时间: 2008-10-13
作者: ylcz 发布时间: 2008-10-15
可lz有没有想过。在高并发的情况下。是否能保证正确运行!
作者: superpower 发布时间: 2008-11-25
希望楼主给个例子,我没搞过缓存
作者: 1987 发布时间: 2008-12-08
高并发再说
作者: wodoe 发布时间: 2009-01-13
--------------------------------------------------------------------------------------------------------------------------------------
海泡石电缆管 维纶电缆管 电缆保护管 风水预测 网站推广软件 上海湖南特产 供求信息群发软件
作者: fgca08 发布时间: 2009-09-09

作者: 邂逅米莱 发布时间: 2009-09-16
作者: ainiaa 发布时间: 2009-09-16
作者: oik550 发布时间: 2009-09-24
斗破苍穹 http://www.shucheng.com/book/5/5575/
作者: 智没平 发布时间: 2009-09-29
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28