Little caching/performance proposal

I have a little proposal how to speed up a little codeigniter caching. If I understand caching properly, all the magic starts when I'm calling
But all data generated for showing this view is generated/gathered before this function is called.
Could someone help me writing an extension to check if current site is cached (and not expired) that I could do something like that:
if(cache_expired()) {
//query database            \
//do something with data     > slowing part
//etc...                    /

$this->load->view('some1_view', $data1);
$this->load->view('some2_view', $data2);
$this->load->view('some3_view', $data3);

I think that extending Output class could be an issue. But maybe there is such function (or similar) so I don't want to discover a wheel again Wink

Thanks for help.

Try building something like that on my MP_Cache library (Wiki article).

Easiest option would be to add an expires-key to the cached array and test for that as well as for the cache, like this:

$example = $this->mp_cache->get('example');
if ($example === false OR $example['expires'] < time())
    // parts of code that generate your $example array, like for example below
    $example = $this->my_model->get_pages_from_db();

    $example['expires'] = time() + 7200;
    $this->mp_cache->write($example, 'example');
Which should cache the contents for 2 hours.

EDIT: I have thought about adding something like this to the get() function (like allow a second parameter with the number of seconds it takes to expire). But I didn't really see the upside to this implementation as I tried to keep the library as general-use as possible, and the example above should do the trick just as well with just a little extra code.

I'm using this Output library for caching header info
So I wrote such function extending it:
function _cache_expired() {
      $CI =& get_instance();
        $path = $CI->config->item('cache_path');

        $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
        $uri =  $CI->config->item('base_url').
        $filepath = $cache_path.md5($uri);

        if ( ! @file_exists($filepath)) {
            return TRUE;

        if ( ! $fp = @fopen($filepath, FOPEN_READ)) {
            return TRUE;

        flock($fp, LOCK_SH);

//        $cache = '';
        if (filesize($filepath) > 0) {
            $this->cache = fread($fp, filesize($filepath));

        flock($fp, LOCK_UN);

        // Restore the cache array
        $this->cache = unserialize($this->cache);

        // Validate cache file
        if ( ! isset($this->cache['exp'] ) OR ! isset($this->cache['headers']) OR ! isset($this->cache['output'])) {
            return TRUE;

        // ini_get('max_execution_time') was added in case of
        // accidentally generating page view's from emtpy values
        // and then caching it, what could happen if script is running for a while
        if (time() + ini_get('max_execution_time') >= trim($this->cache['exp']))
//            log_message('debug', "Cache file has expired. File deleted");
            return TRUE;

        return FALSE;
I'm caching result of
from my function in public variable
$this->cache = unserialize($this->cache);
for faster access from _display_cache function.

My tests show that using this function isn't faster than not using it at all Smile I was surprised that fact. (Maybe my site isn't enough complicated...).

I will change this function from unserialize version to checking mtime of cached file - maybe this will be faster. I will post if it helps.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.