提供一个PHP缓存方案,大家认为这个的效率如何呢?
基本思想是把数据库的内容以数组的形式存放在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;
}
}
?>