提供一个PHP缓存方案,大家认为这个的效率如何呢?

XiaoQy 2005-06-21 07:08:09
基本思想是把数据库的内容以数组的形式存放在256x256(两层目录,每层265个目录)的缓存目录中,而不用去查数据库。

但是我发现在windows系统上,当数据量不大的时候,性能甚至还不如直接查询数据库,
<?php

class cache
{
/**
* 缓存目录
*/
var $cache_dir;

/**
* 需要调用的缓存信息
*/
var $_cache;

function cache()
{
$this->_cache = $_CACHE;
}

/**
* 写入缓存信息
*
* 写入缓存信息到缓存文件,存放缓存数据的更新时间以及有效时间
* @param string $cname 缓存名称
* @param array $data 缓存编码
* @param int $ctime 缓存有效期
*/
function setCache($cachename,$data,$ctime=0)
{
$md5str = md5($cachename);
$cfile = $this->buildCacheDir($cachename,$md5str);
$cachestring = var_export($data,true);
$out = "<?php".ENTER;
$out .= "$"."_cache_$cachename = ".$cachestring.ENTER;
$out .= "?".">";

$fp = fopen($cfile,"w");
fputs($fp,$out);
fclose($fp);
$this->register($cachename,$md5str,$ctime);
}

/**
* 注册缓存信息
*
* 注册缓存信息到缓存配置文件,存放缓存数据的更新时间以及有效时间
* @param string $cname 缓存名称
* @param string $md5str 缓存编码
* @param int $ctime 缓存有效期
*/
function register($cname,$md5str,$ctime)
{
$configfile_prefix = $md5str[0].$md5str[1].$md5str[2].$md5str[3];
$configfile_path = $md5str[0].$md5str[1].DIRECTORY_SEPARATOR.$md5str[2].$md5str[3].DIRECTORY_SEPARATOR;
$this->loadCache($configfile_path,$configfile_prefix);
$this->_cache[$configfile_prefix][$cname] = array('register_time'=>time(),'ctime'=>$ctime);
$cstr = var_export($this->_cache[$configfile_prefix],true);
$out .= "<?php".ENTER;
$out .= "//这是缓存配置文件,请不要手动修改".ENTER;
$out .= "\$_CACHE = $cstr";
$out .= "?".">";

$configfile = $configfile_prefix.".config".EXT;
$fp = fopen(CACHE_PATH.$configfile_path.$configfile,"w");
fputs($fp,$out);
fclose($fp);
}

/**
* 载入现有缓存配置信息
*
* @param string $configfile_prefix 缓存存取前缀
* @return boolean
*/
function loadCache($configfile_path,$configfile_prefix)
{
if (file_exists(CACHE_PATH.$configfile_path.$configfile_prefix.".config".EXT))
{
require_once(CACHE_PATH.$configfile_path.$configfile_prefix.".config".EXT);
$this->_cache[$configfile_prefix] = array_merge($this->_cache[$configfile_prefix],$_CACHE);
return true;
}
else
{
return false;
}
}

/**
* 读取缓存
*
* @param string $cachename 缓存名称
* @return array
*/
function getCache($cachename)
{
$md5str = md5($cachename);
$configfile_prefix = $md5str[0].$md5str[1].$md5str[2].$md5str[3];
$configfile_path = $md5str[0].$md5str[1].DIRECTORY_SEPARATOR.$md5str[2].$md5str[3].DIRECTORY_SEPARATOR;
if (!isset($this->_cache[$configfile_prefix]))
{
if (!$this->loadCache($configfile_path,$configfile_prefix))
{
return false;
}
}

if (!array_key_exists($cachename,$this->_cache[$configfile_prefix]))
{
return false;
}

if($this->_cache[$configfile_prefix][$cachename]['ctime'] > 0)
{
if ((time() - $this->_cache[$configfile_prefix][$cachename]['register_time']) > $this->_cache[$configfile_prefix][$cachename]['ctime'])
{
return false;
}
}

$cfile = $this->buildCacheDir($cachename,$md5str);
unset($md5str);
include($cfile);
return ${"_cache_".$cachename};
}

/**
* 初始化缓存对象,保证缓存对象唯一
*
* @param void
* @return void
*/
function &init()
{
static $cache = null;
if (is_null($cache))
{
$cache = new cache();
}
return $cache;
}

/**
* 创建缓存hash目录
*
* @param string $cname 缓存名称
* @param string $md5str 缓存编码
* @return string 缓存文件名
*/
function buildCacheDir($cachename,$md5_cachename)
{
$md5str = $md5_cachename;
$rpath = $md5str[0].$md5str[1].DIRECTORY_SEPARATOR.$md5str[2].$md5str[3];

/*if (!file_exists(CACHE_PATH.$rpath))
{
loadFunction("file");
mkdirs(CACHE_PATH.$rpath);
}
*/
return CACHE_PATH.$rpath.DIRECTORY_SEPARATOR.$cachename.".cache".EXT;
}
}
?>
...全文
510 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangfeng1133 2005-08-16
  • 打赏
  • 举报
回复
asp里到停方便的,
是不是可以把查寻出来的,再放到数据库呢

建立cache_php表,然后把常用的html,保持下来啊,

我2年都没有作php了,asp倒挺方便,asp就这个比php方便
ashchen 2005-06-22
  • 打赏
  • 举报
回复
discuz的缓存用的很好,可以研究一下
xuzuning 2005-06-22
  • 打赏
  • 举报
回复
你没有提供应用示例,所以很难判断优劣。
从你的类定义上看,缓存不是自动进行的。需要使用者编程确定何时使用缓存内容,这样的话这个缓存类是“残废”的

按照你的设想,缓存的只是数据库内容。那么数据库也就不需要了,256*256=65536的规模完全可以满足一般小型应用了
iasky 2005-06-21
  • 打赏
  • 举报
回复
mark
alasika 2005-06-21
  • 打赏
  • 举报
回复
减少数据库(这里是"缓存")的调用才是正道,或者只把一些常用的量存到数组形式
gu1dai 2005-06-21
  • 打赏
  • 举报
回复
是吗
缓存我不怎么用。

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧