|
发表于 2009-11-12 11:48:25
|
显示全部楼层
原帖:http://codeigniter.com/forums/viewthread/53238/
这个帖子是在 CI 英文官方找到的,提供了外置的缓存清理类库。原文如下:
The following Cache vulnerability
was not addressed by CI, so I developed this simple and (hopefully) effective cache wrapper library
翻译:以下CI缓存弱点并未得到CI官方的解决,所以我自己写了一个外置的简单而有效的类库
Its main purpose is relieving the developer from worrying aboutcleaning the cache directory (or writing extra code to decide whatshould or shouldn’t go to cache).
翻译:该类库的主要目的是免除开发者清理缓存文件的工作(写额外的代码来决定什么该缓存什么不该缓存【ichun注:这个其实才是这个争论贴的关键之处。个人认为】)
Also it enables turning on/off the cache for the entire site using the config file
翻译:同时,你也可以用config文件来打开、关闭全站的缓存功能
The random clean factor idea was borrowed from the excellentCache_lite PEAR library. It helps keeping the avarage number of cachefiles under control, without traversing the cache directory andcounting the actual number of cache files (and hence, it is very fast).
翻译:这个随机清理的主意借鉴了出色的cache_lite PEAR库。它可以保持一个平均的可控的缓存文件数,而不用进入缓存目录并统计真实的缓存文件数字(另外,它的速度相当快。)【ichun注:代码我还没空看,事实上也不太懂。希望大牛们解读一下】
PHP复制代码 <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Cache Library Class
*/
class Cacher
{
var $_ci;
var $_useCache;
var $_cacheExpire;
var $_cleanFactor;
/**
* Constructor
*
* @access public
*/
function Cacher ()
{
$this->_ci =& get_instance ();
$this->_useCache = $this->_ci -> config -> item("use_cache");
$this->_cacheExpire = (int )$this->_ci -> config -> item("cache_expiration");
$this->_cleanFactor = (int )$this->_ci -> config -> item("cache_clean_factor");
}
// --------------------------------------------------------------------
/**
* Cache the current page using CI cache
* Clean the cache directory on every 'cache_clean_factor' writes on avarage
* Warning: When the time comes, the cleaner will delete all files in the cache directory
* @access public
* @return void
*/
function cachePage ($expiration = 0)
{
if($this->_useCache != 'false')
{
if($expiration > 0)
$this->_ci ->output->cache($expiration);
else
$this->_ci ->output->cache($this->_cacheExpire );
if($this->_cleanFactor > 0)
{
if(mt_rand(1, $this->_cleanFactor ) == 1)
$this->_clean ();
}
}
}
// --------------------------------------------------------------------
/**
* Clean all cache in the cache dir
*
* @access private
* @return number of cleaned files or false on error
*/
function _clean ()
{
$dir = $this->_ci ->config->item('cache_path');
if(!@is_dir($dir))
return false;
if( ! $fp = @opendir($dir))
return false;
$count = 0;
$retVal = true;
while ($file = @readdir($fp))
{
$fullPath = $dir.$file;
if(!@is_file($fullPath))
continue;
//Cache file names are 32 chars in size (because md5). Just extra caution
if(@strlen($file) != 32)
continue;
if(@unlink($fullPath))
$count++;
else
$retVal = false;
}
closedir($fp)
if($retVal)
return $count;
else
return false;
}
}
?> 复制代码
请各位大牛看看,讨论一下用这个解决问题如何?当然,对于高手来说,可能会执着于研究问题的根源,但对新手来说,可能解决的方案会更好。既然vison提出的解决方案并没有得到一致的认可,那上面贴的国外高手的“以清理缓存的方式”来解决问题的手段是否可行? |
评分
-
查看全部评分
|