Welcome Guest, Not a member yet? Register   Sign In
Little caching/performance proposal
#3

[eluser]Unknown[/eluser]
I'm using this Output library for caching header info http://codeigniter.com/wiki/Cache_headers
So I wrote such function extending it:
Code:
function _cache_expired() {
      $CI =& get_instance();
        $path = $CI->config->item('cache_path');

        $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
        $uri =  $CI->config->item('base_url').
                $CI->config->item('index_page').
                $CI->uri->uri_string();
        $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);
        fclose($fp);

        // 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']))
        {
            @unlink($filepath);
//            log_message('debug', "Cache file has expired. File deleted");
            return TRUE;
        }

        return FALSE;
    }
I'm caching result of
Code:
unserialize($cache);
from my function in public variable
Code:
$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.


Messages In This Thread
Little caching/performance proposal - by El Forum - 03-24-2009, 01:07 PM
Little caching/performance proposal - by El Forum - 03-24-2009, 06:22 PM
Little caching/performance proposal - by El Forum - 03-25-2009, 05:25 AM



Theme © iAndrew 2016 - Forum software by © MyBB