CodeIgniter數(shù)據(jù)庫緩存自動過期的處理

2013-06-08 16:45:06來源:techweb作者:

CodeIgniter框架是一個非常小巧的PHP框架,最近幾個項(xiàng)目中一直用。CI自帶數(shù)據(jù)庫文件緩存,但按官方的說法,緩存設(shè)置后永不過期,出發(fā)你調(diào)用方法主動刪除。

CodeIgniter框架是一個非常小巧的PHP框架,最近幾個項(xiàng)目中一直用。CI自帶數(shù)據(jù)庫文件緩存,但按官方的說法,緩存設(shè)置后永不過期,出發(fā)你調(diào)用方法主動刪除。

Cache files DO NOT expire. Any queries that have been cached will remain cached until you delete them.

感覺太弱智了,非常不方便。 我修改了一下db類,在開啟緩存時設(shè)置一個過期時間,到期自動緩存自動失效。

1:CI database/DB_dirver.php 中 1021行 cache_on 函數(shù)替換為
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 函數(shù) if (FALSE === ($cachedata = read_file($filepath))) 一行前面加上
//判斷是否過期 // 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(); 改為
$this→db→cache_on($SEC);
$SEC 為緩存過期時間,以秒為單位。 如 $this→db→cache_on(60);表示緩存60秒后過期。

關(guān)鍵詞:CodeIgniter

贊助商鏈接: