Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by George Petsagourakis )

Get Cache File Contents

Category:Library -> Cache | Category:Library -> Community

Here is an extension for the Output Library that aims to retrieve cached file contents and let you do fragment caching.

You just have to create a file called MY_Output.php in the system/application/libraries folder and put this code in the file :

<?

class MY_Output extends CI_Output {

    
function My_Output()
    
{
        parent
::CI_Output();
    
}

    
function get_cached_file($url) {
            $CI 
=& get_instance();

            
/* defines cache path */
            
$cache_path "system/cache/";
            if(
strlen($CI->config->item('cache_path')) > 0{
                $cache_path 
$CI->config->item('cache_path');
            
}

            
/* retreives cache file */
            
$cached_url base_url().$CI->config->item('index_page')."/".$url;
            
$cached_file $cache_path.md5($cached_url);

            
/* checks if cached file exists */
            
            
if(file_exists($cached_file)) {
        
                
/* gets cached content and removes timestamp */
                
                
$handle fopen($cached_file"r")
                $contents 
fread($handlefilesize($cached_file));
                
fclose($handle);
                
$timestamp_pos strpos($contents"--->");
                
$contents substr($contents, ($timestamp_pos 4));
                return 
$contents;
        
            
}
        
            
return FALSE;
    
}
}

?> 

Then the usage is pretty simple. Imagine you’ve called the $this->output->cache(); function from the sample_controller, within the sample_method, then you would access this fragment this way:

http://www.yoursite.com/sample_controller/sample_method/ 

Then you can use the new get_cached_file function to get the cached contents of this page. And you can do it this way:

$this->output->get_cached_file('sample_controller/sample_method'

Categories: