Skip to content

Centralized Cache URI

World Wide Web Server edited this page Jul 4, 2012 · 4 revisions

Adjusting the storages paths of the CI cache can be labor-intensive, so I decided to make it a little less expensive. Make this mod to MY_Output and you should be able to plug in Suffix Cache, Slightly Better Caching, or make any other adjustments to the CI caching conventions -- all from one spot.

I've included snippets for the two Cache libraries mentioned above. [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**

  • One Cache URI to Rule Them All

  • Cache URI building is coded in duplicate as of 1.6.2 trunk, so I

  • refactored it to make it a little more manageable. If you're using

  • Slightly Better Caching or Suffix Cache, the codebases can be merged

  • to give you more reign over cache filing practices.

  • @package CodeIgniter

  • @subpackage Libraries

  • @hacked-by Bradford Mar

  • @copyright Copyright (c) 2008, Bradford Mar

  • @license http://codeigniter.com/wiki/Copyrights_and_Content_License/ */ class MY_Output extends CI_Output {

    /**

    • Get the uri of a given cached page
    • @access public
    • @return void */

    function get_cache_URI($set_uri = null, $append_suffix = true){ $CFG =& load_class('Config'); $URI =& load_class('URI');

     $set_uri = (isset($set_uri)) ? $set_uri : $URI->uri_string;
    
     $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
             
     if ( ! is_dir($cache_path) OR ! is_writable($cache_path))
     {
          return FALSE;
      }
     
     /*
      * Build the file path.  The file name is an MD5 hash of the full URI
      * If we make the suffix optional in _write_cache() be sure to do
      * so here too. <[email protected]>
      */
     $uri =    $CFG->item('base_url').
             $CFG->item('index_page').
             $set_uri . '.';
     if ($append_suffix)
     {
         $uri.=$URI->suffix;
     } else
     {
         // Get type type
         if ( FALSE !== ($type = $this->_get_from_session($CFG->item('cache_type_source'))) )
         {
             $uri.= 'sess:'.$type;
         }
     }
     return array('path'=>$cache_path, 'uri'=>$uri);
    

    }

    // --------------------------------------------------------------------

    /**

    • Manually clear a cached file
    • @access public
    • @return void */

    function clear_page_cache($set_uri = null, $append_suffix = true){ $cacheuri = $this->get_cache_URI($set_uri, $append_suffix); $filepath = $cacheuri['path']; $filepath .= md5($cacheuri['uri']);
    if(file_exists($filepath)) { touch($filepath); unlink($filepath); log_message('debug', "Cache file has expired. File deleted for: ".$cacheuri['uri']); } else { return FALSE; } }

} // END MY_Output Class

/* End of file MY_Output.php / / Location: ./system/application/libraries/MY_Output.php */ [/code]

Make edits to _display_cache() and _write_cache like so: [code] // --------------------------------------------------------------------

/**
 * Update/serve a cached file
 *
 * @access    public
 * @return    void
 */    

function _display_cache(&$CFG, &$RTR)
{

// moved to function get_cache_URI()
/* $URI =& load_class('URI');

    $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
        
    if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
    {
        return FALSE;
    }
    
    // Build the file path.  The file name is an MD5 hash of the full URI
    $uri =    $CFG->item('base_url').
            $CFG->item('index_page').
            $URI->uri_string;

*/ $cacheuri = $this->get_cache_URI(); $uri = $cacheuri['uri']; $cache_path = $cacheuri['path'];

    $filepath = $cache_path.md5($uri);

    if ( ! @file_exists($filepath))
    {
        return FALSE;
    }

// etc... [/code] [code] /** * Write a Cache File * * @access public * @return void */
function _write_cache($output) { $CI =& get_instance();

// moved to function get_cache_URI()
/* $path = $CI->config->item('cache_path');

    $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
    
    if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
    {
        return;
    }
    
    $uri =    $CI->config->item('base_url').
            $CI->config->item('index_page').
            $CI->uri->uri_string();

*/
$cacheuri = $this->get_cache_URI(); $uri = $cacheuri['uri']; $cache_path = $cacheuri['path'];

    $cache_path .= md5($uri);

    if ( ! $fp = @fopen&#40;$cache_path, 'wb'&#41;)
    {
        log_message('error', "Unable to write cache file: ".$cache_path);
        return;
    }

// etc... [/code]

Clone this wiki locally