加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-12 20:13:36 所属栏目:PHP教程 来源:网络整理
导读:CodeIgniter框架是一个非常小巧的PHP框架。CI自带数据库文件缓存,但按官方的说法,缓存设置后永不过期,除非你调用方法主动删除。Cache files DO NOT expire. Any queries that have been cached will remain cached until you delete them.感觉太弱智了,

CodeIgniter框架是一个非常小巧的PHP框架。CI自带数据库文件缓存,但按官方的说法,缓存设置后永不过期,除非你调用方法主动删除。Cache files DO NOT expire. Any queries that have been cached will remain cached until you delete them.感觉太弱智了,非常不方便。 修改一下db类,在开启缓存时设置一个过期时间,到期自动缓存自动失效。1:CI database/DB_dirver.php 中 1021行 cache_on 函数替换为<div class="codetitle"><a style="CURSOR: pointer" data="82574" class="copybut" id="copybut82574" onclick="doCopy('code82574')"> 代码如下:<div class="codebody" id="code82574">function cache_on($expire_time=0) //add parm expire time - 缓存过期时间
{
$this->cache_expire_time = $expire_time; //add by kenvin
$this->cache_on = TRUE;
return TRUE;
}2:CI database/DB_cache.php 中 90行 read 函数 if (FALSE === ($cachedata = read_file($filepath))) 一行前面加上<div class="codetitle"><a style="CURSOR: pointer" data="74288" class="copybut" id="copybut74288" onclick="doCopy('code74288')"> 代码如下:<div class="codebody" id="code74288">//判断是否过期 // cache_expire_time
if ( !file_exists($filepath) ) {
return false;
}
if ( $this->db->cache_expire_time > 0 && filemtime($filepath) db->cache_expire_time) {
return false;
}这样,在需要开启缓存的地方,由以前的 $this→db→cache_on(); 改为
<div class="codetitle"><a style="CURSOR: pointer" data="52261" class="copybut" id="copybut52261" onclick="doCopy('code52261')"> 代码如下:<div class="codebody" id="code52261">$this→db→cache_on($SEC);
$SEC 为缓存过期时间,以秒为单位。 如 $this→db→cache_on(60);表示缓存60秒后过期。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读