一个简单的cache管理类
时间:2007-07-08
来源:互联网
第一次在phpchina上面发贴, 希望各位朋友多多支持, 如果各位用上了, 还请在此吼一声。 也欢迎在此建议改进。 谢谢
复制内容到剪贴板
/*
Class: Simple cache management class.
Version: 0.0.2
Last update: 2007-07-07
Author: wimax
URL: [url=http://www.embedworld.com]http://www.embedworld.com[/url]
*/
define("ERR_FOPEN", -1);
define("ERR_READABLE", -2);
define("ERR_FWRITE", -3);
define("ERR_NULL", -4);
define("INFO_OKAY", 0);
class cacheMgr {
var $_cache_dir;
var $_cacheFileName;
var $_content;
var $_cache_timespan;
function cacheMgr() {
$this->_cache_dir="./cache";
$this->set_cache_timespan();
}
function set_cache_dir($cache_dir="") {
$this->_cache_dir=preg_replace("#[\\\/]$#", "", $cache_dir);
}
function get_cache_dir($cache_dir="") {
return $this->_cache_dir;
}
function set_cache_timespan($seconds=2592000) { // 缺省cache生效的时间是一个月 (one month)
$this->_cache_timespan=$seconds;
}
function get_cache_timespan() {
return $this->_cache_timespan;
}
function set_cache_file_name($params=array()) {
$cacheFileName="";
foreach ($params as $key => $value) {
$cacheFileName .= "_".$key."_".$value;
}
//调试的时候用这个
$this->_cacheFileName= $this->_cache_dir."/".$cacheFileName.".tmp";
// 实际发布的时候用这个
// $this->_cacheFileName= $this->_cache_dir."/".md5($cacheFileName).".tmp";
}
function read_cache_content($params=array()) {
$this->_c;
$this->set_cache_file_name($params);
if (is_readable($this->_cacheFileName)) {
$this->_content = file_get_contents($this->_cacheFileName);
} else {
return ERR_READABLE;
}
return $this->_content;
}
/* Recommended params list:
id
module
script_name
block_name
url_remaining_part
*/
function write_cache_content($c, $params=array()) {
$this->set_cache_file_name($params);
/* dump cache content to local disk */
if ($fp=fopen($this->_cacheFileName, "wb+")) {
$this->_content=$content;
if(fwrite($fp, $content)) {
fclose($fp);
return INFO_OKAY;
} else {
fclose($fp);
return ERR_FWRITE;
}
} else {
return ERR_FOPEN;
}
}
function is_cache_exist($params=array()) {
$this->set_cache_file_name($params);
return file_exists($this->_cacheFileName);
}
function get_cache_update_time($params=array()) {
if (!empty($params)) {
$this->set_cache_file_name($params);
}
$fStat=stat($this->_cacheFileName);
$fTime=$fStat["mtime"] < $fStat["ctime"] ? $fStat["mtime"] : $fStat["ctime"];
return $fTime;
}
function is_cache_not_expired($params=array()) {
$now=time();
$cache_file_update_time=$this->get_cache_update_time();
if (!empty($params)) {
$this->set_cache_file_name($params);
}
return ( ($cache_file_update_time + $this->_cache_timespan) > $now); // one month
}
function is_cache_health($params=array()) {
return $this->is_cache_exist($params) && $this->is_cache_not_expired();
}
}
/* 使用示例 */
if (0) {
$cacheMgr=new cacheMgr;
$cacheMgr->set_cache_dir("cache/");
print "cache_dir: ". $cacheMgr->get_cache_dir()."\n";
$pas=array("module"=>"test1", "block" => "block1");
if ($cacheMgr->is_cache_health($pas)) {
$content=$cacheMgr->read_cache_content($pas);
print "haha: $content";
} else {
ob_start();
print "hello world\n";
$output=ob_get_contents();
ob_end_flush();
$cacheMgr->write_cache_content($output, $pas);
}
}
?>
代码:
<?/*
Class: Simple cache management class.
Version: 0.0.2
Last update: 2007-07-07
Author: wimax
URL: [url=http://www.embedworld.com]http://www.embedworld.com[/url]
*/
define("ERR_FOPEN", -1);
define("ERR_READABLE", -2);
define("ERR_FWRITE", -3);
define("ERR_NULL", -4);
define("INFO_OKAY", 0);
class cacheMgr {
var $_cache_dir;
var $_cacheFileName;
var $_content;
var $_cache_timespan;
function cacheMgr() {
$this->_cache_dir="./cache";
$this->set_cache_timespan();
}
function set_cache_dir($cache_dir="") {
$this->_cache_dir=preg_replace("#[\\\/]$#", "", $cache_dir);
}
function get_cache_dir($cache_dir="") {
return $this->_cache_dir;
}
function set_cache_timespan($seconds=2592000) { // 缺省cache生效的时间是一个月 (one month)
$this->_cache_timespan=$seconds;
}
function get_cache_timespan() {
return $this->_cache_timespan;
}
function set_cache_file_name($params=array()) {
$cacheFileName="";
foreach ($params as $key => $value) {
$cacheFileName .= "_".$key."_".$value;
}
//调试的时候用这个
$this->_cacheFileName= $this->_cache_dir."/".$cacheFileName.".tmp";
// 实际发布的时候用这个
// $this->_cacheFileName= $this->_cache_dir."/".md5($cacheFileName).".tmp";
}
function read_cache_content($params=array()) {
$this->_c;
$this->set_cache_file_name($params);
if (is_readable($this->_cacheFileName)) {
$this->_content = file_get_contents($this->_cacheFileName);
} else {
return ERR_READABLE;
}
return $this->_content;
}
/* Recommended params list:
id
module
script_name
block_name
url_remaining_part
*/
function write_cache_content($c, $params=array()) {
$this->set_cache_file_name($params);
/* dump cache content to local disk */
if ($fp=fopen($this->_cacheFileName, "wb+")) {
$this->_content=$content;
if(fwrite($fp, $content)) {
fclose($fp);
return INFO_OKAY;
} else {
fclose($fp);
return ERR_FWRITE;
}
} else {
return ERR_FOPEN;
}
}
function is_cache_exist($params=array()) {
$this->set_cache_file_name($params);
return file_exists($this->_cacheFileName);
}
function get_cache_update_time($params=array()) {
if (!empty($params)) {
$this->set_cache_file_name($params);
}
$fStat=stat($this->_cacheFileName);
$fTime=$fStat["mtime"] < $fStat["ctime"] ? $fStat["mtime"] : $fStat["ctime"];
return $fTime;
}
function is_cache_not_expired($params=array()) {
$now=time();
$cache_file_update_time=$this->get_cache_update_time();
if (!empty($params)) {
$this->set_cache_file_name($params);
}
return ( ($cache_file_update_time + $this->_cache_timespan) > $now); // one month
}
function is_cache_health($params=array()) {
return $this->is_cache_exist($params) && $this->is_cache_not_expired();
}
}
/* 使用示例 */
if (0) {
$cacheMgr=new cacheMgr;
$cacheMgr->set_cache_dir("cache/");
print "cache_dir: ". $cacheMgr->get_cache_dir()."\n";
$pas=array("module"=>"test1", "block" => "block1");
if ($cacheMgr->is_cache_health($pas)) {
$content=$cacheMgr->read_cache_content($pas);
print "haha: $content";
} else {
ob_start();
print "hello world\n";
$output=ob_get_contents();
ob_end_flush();
$cacheMgr->write_cache_content($output, $pas);
}
}
?>
作者: wimax 发布时间: 2007-07-08

作者: wukeyuan 发布时间: 2007-07-08
不错
作者: fengyun 发布时间: 2007-07-09
把注释写上哈
作者: FLASH百强 发布时间: 2007-07-09
发现Discuz论坛显示的时候替换了一些字段。 譬如write_cache_content函数的第一个参数是$content, 结果替换成了$c。 read_cache_content函数的第一行应该是 $this->_content =“”;
代码中各个函数的命名已经比较清除了, 注释也就免了。
PS: 不知道怎么用Discuz的PHP语法加亮, 请知道的朋友赐教一下。 :)
代码中各个函数的命名已经比较清除了, 注释也就免了。
PS: 不知道怎么用Discuz的PHP语法加亮, 请知道的朋友赐教一下。 :)
作者: wimax 发布时间: 2007-07-09
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28