zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

让CodeIgniter数据库缓存自动过期的处理的方法

数据库方法缓存自动 处理 过期 CodeIgniter
2023-06-13 09:15:28 时间
CodeIgniter框架是一个非常小巧的PHP框架。CI自带数据库文件缓存,但按官方的说法,缓存设置后永不过期,除非你调用方法主动删除。

CachefilesDONOTexpire.Anyqueriesthathavebeencachedwillremaincacheduntilyoudeletethem.

感觉太弱智了,非常不方便。修改一下db类,在开启缓存时设置一个过期时间,到期自动缓存自动失效。

1:CIdatabase/DB_dirver.php中1021行cache_on函数替换为

复制代码代码如下:
functioncache_on($expire_time=0)//addparmexpiretime-缓存过期时间
{
$this->cache_expire_time=$expire_time;//addbykenvin
$this->cache_on=TRUE;
returnTRUE;
}


2:CIdatabase/DB_cache.php中90行read函数if(FALSE===($cachedata=read_file($filepath)))一行前面加上

复制代码代码如下:
//判断是否过期//cache_expire_time
if(!file_exists($filepath)){
returnfalse;
}
if($this->db->cache_expire_time>0&&filemtime($filepath)db->cache_expire_time){
returnfalse;
}


这样,在需要开启缓存的地方,由以前的$this→db→cache_on();改为
复制代码代码如下:$this→db→cache_on($SEC);
$SEC为缓存过期时间,以秒为单位。如$this→db→cache_on(60);表示缓存60秒后过期。