Reputation: 1548
I was wondering if there is any workaround to delete database cache
in codeigniter, say after every 5 hours. by default database query cache
is persistent and we have to delete it manually.
Can we make db cache
work like page level cache
in CI, where we can specify time for cache!
Upvotes: 3
Views: 4384
Reputation: 17828
I don't really like the cron job solution since one might forget executing it when installing new server or after maintenance.
I decided to use a little helper to support deletion or old cache file:
/application/helpers/cache_expire_helper.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
define("CACHE_DIR", APPPATH.'cache/');
if ( ! function_exists('is_cache_valid')) {
function is_cache_valid($cache_name,$lifespan) {
if (file_exists(CACHE_DIR.$cache_name)) {
$last_date = file_get_contents(CACHE_DIR.$cache_name);
if (abs($last_date - time()) < $lifespan) {
return true;
} else {
file_put_contents(CACHE_DIR.$cache_name,time());
return false;
}
} else {
file_put_contents(CACHE_DIR.$cache_name,time());
return true;
}
}
}
Then, to use it, from the Model:
$this->load->helper('cache_expire');
if (!is_cache_valid('servers',60 /* seconds */)){
$this->db->cache_delete('controller', 'comments');
}
.....
Upvotes: 2
Reputation: 429
You could make a cron script to call $this->db->cache_delete_all(); and run it every 5h.
Upvotes: 3