Home  >  Article  >  Backend Development  >  Codeigniter database cache automatic expiration processing method

Codeigniter database cache automatic expiration processing method

WBOY
WBOYOriginal
2016-07-25 08:52:511151browse
This article introduces how to handle the automatic expiration of the codeigniter database cache. It modifies the db class and sets an expiration time when the cache is enabled. The automatic cache will automatically expire when it expires.

codeigniter database cache automatically expires

The codeigniter framework is a very small php framework that has been used in several recent projects. CI comes with a database file cache, but according to the official statement, the cache will never expire after being set, so you can call the method to delete it automatically.

cache files do not expire. any queries that have been cached will remain cached until you delete them.

Modified the db class, set an expiration time when caching is enabled, and the cache will automatically expire when it expires.

1. Replace the cache_on function in line 1021 of CI database/DB_dirver.php with

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. In line 90 of CI database/DB_cache.php, add the read function if (FALSE === ($cachedata = read_file($filepath))) in front of the line

//判断是否过期 // cache_expire_time
if ( !file_exists($filepath) ) {
return false;
} // bbs.it-home.org
if ( $this->db->cache_expire_time > 0 && filemtime($filepath) db->cache_expire_time) {
return false;
}

In this way, where the cache needs to be enabled, the previous $this→db→cache_on(); is modified to: $this→db→cache_on($SEC); $SEC is the cache expiration time in seconds. For example, $this→db→cache_on(60); means that the cache will expire after 60 seconds.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn