Processing method for automatically expiring CodeIgniter database cache

  • 2021-06-29 10:28:44
  • OfStack

The CodeIgniter framework is a very compact PHP framework.CI comes with its own database file cache, but officially, the cache settings never expire unless you invoke a method to delete them actively.

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

It feels too weak and inconvenient.Modify the db class under 1 to set an expiration time when the cache is opened, and the expiration auto-cache will automatically expire.

1:CI database/DB_1021 lines cache_in dirver.phpon function replaced by

function cache_on($expire_time=0) //add parm expire time -  Cache expiration time 
{
$this->cache_expire_time = $expire_time; //add by kenvin
$this->cache_on = TRUE;
return TRUE;
}


2:CI database/DB_cache.php 90 lines read function if (FALSE == ($cachedata = read_file ($filepath)) 1 line preceded by

// Determine whether it is overdue  // 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 way, where the cache needs to be opened, it will be $this_db_cache_on();Change to
$this _ db _ cache_on($SEC); 

$SEC is the cache expiration time in seconds.For example, $this_db_cache_on (60);Indicates that the cache expires after 60 seconds.

Related articles: