Welcome Guest, Not a member yet? Register   Sign In
Caching and headers
#97

[eluser]vrencianz[/eluser]
This thread is old but the MY_Output class is still useful. For Codeigniter 2.1.0 this override neds only few mods:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Output extends CI_Output
{
/**
  * Write a Cache File
  *
  * @access    public
  * @return    void
  */
function _write_cache($output)
{
  $CI =& get_instance();
  $path = $CI->config->item('cache_path');

  $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

  if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
  {
   log_message('error', "Unable to write cache file: ".$cache_path);
   return;
  }

  $uri = $CI->config->item('base_url').
  $CI->config->item('index_page').
  $CI->uri->uri_string();

  $cache_path .= md5($uri);

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

  $expire = time() + ($this->cache_expiration * 60);
  $headers = array();

  foreach($this->headers as $header)
  {
   $headers[] = $header[0].(int)(boolean)$header[1];
  }

  $headers = implode("\n", $headers);

  if (flock($fp, LOCK_EX))
  {
   fwrite($fp, $expire .'TS--->'. $headers .'H--->'. $output);
   flock($fp, LOCK_UN);
  }
  else
  {
   log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
   return;
  }
  fclose($fp);
  @chmod($cache_path, FILE_WRITE_MODE);

  log_message('debug', "Cache file written: ".$cache_path);
}

/**
  * Update/serve a cached file
  *
  * @access    public
  * @return    void
  */
function _display_cache(&$CFG, &$URI)
{
  $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');

  // 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;

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

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

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

  flock($fp, LOCK_SH);

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

  flock($fp, LOCK_UN);
  fclose($fp);

   // Strip out the embedded timestamp and headers
  $ts = strpos($cache, 'TS--->');
  $h = strpos($cache, 'H--->');
  if ( ! $ts || ! $h ) {
   return FALSE;
  }
  $match = array();
  $match['1'] = substr($cache, 0, $ts);
  $match['2'] = substr($cache, $ts+6, $h-$ts-6);
  $match['0'] = $match['1'].'TS--->'.$match['2'].'H--->';

  // Has the file expired? If so we'll delete it.
  if (time() >= trim(str_replace('TS--->', '', $match['1'])))
  {
   @unlink($filepath);
   log_message('debug', "Cache file has expired. File deleted");
   return FALSE;
  }

  // Extract the headers
  $headers = explode("\n", $match['2']);
  foreach($headers as $header)
  {
   $this->headers[] = array(substr($header, 0, -1), substr($header, -1));
  }
  
  // Display the cache
  $this->_display(str_replace($match['0'], '', $cache));
  log_message('debug', "Cache file is current. Sending it to browser.");
  return TRUE;
}
}


Messages In This Thread
Caching and headers - by El Forum - 07-10-2008, 11:38 PM
Caching and headers - by El Forum - 07-11-2008, 05:16 AM
Caching and headers - by El Forum - 07-11-2008, 09:07 AM
Caching and headers - by El Forum - 07-11-2008, 11:34 AM
Caching and headers - by El Forum - 07-11-2008, 12:58 PM
Caching and headers - by El Forum - 07-11-2008, 03:08 PM
Caching and headers - by El Forum - 07-11-2008, 10:38 PM
Caching and headers - by El Forum - 07-11-2008, 10:44 PM
Caching and headers - by El Forum - 07-12-2008, 12:17 AM
Caching and headers - by El Forum - 07-12-2008, 12:21 AM
Caching and headers - by El Forum - 07-12-2008, 12:44 PM
Caching and headers - by El Forum - 07-20-2008, 10:00 PM
Caching and headers - by El Forum - 07-20-2008, 10:30 PM
Caching and headers - by El Forum - 07-20-2008, 10:31 PM
Caching and headers - by El Forum - 07-20-2008, 11:52 PM
Caching and headers - by El Forum - 07-20-2008, 11:54 PM
Caching and headers - by El Forum - 07-21-2008, 05:25 AM
Caching and headers - by El Forum - 07-21-2008, 10:07 AM
Caching and headers - by El Forum - 07-21-2008, 11:47 AM
Caching and headers - by El Forum - 07-21-2008, 03:19 PM
Caching and headers - by El Forum - 07-21-2008, 03:58 PM
Caching and headers - by El Forum - 07-21-2008, 04:21 PM
Caching and headers - by El Forum - 07-21-2008, 05:31 PM
Caching and headers - by El Forum - 07-21-2008, 06:46 PM
Caching and headers - by El Forum - 07-21-2008, 07:02 PM
Caching and headers - by El Forum - 07-21-2008, 07:11 PM
Caching and headers - by El Forum - 07-21-2008, 07:29 PM
Caching and headers - by El Forum - 07-21-2008, 07:39 PM
Caching and headers - by El Forum - 07-21-2008, 08:49 PM
Caching and headers - by El Forum - 07-21-2008, 08:55 PM
Caching and headers - by El Forum - 07-21-2008, 10:01 PM
Caching and headers - by El Forum - 07-21-2008, 10:11 PM
Caching and headers - by El Forum - 07-21-2008, 10:15 PM
Caching and headers - by El Forum - 07-21-2008, 10:22 PM
Caching and headers - by El Forum - 07-21-2008, 10:28 PM
Caching and headers - by El Forum - 07-21-2008, 10:36 PM
Caching and headers - by El Forum - 07-21-2008, 10:37 PM
Caching and headers - by El Forum - 07-21-2008, 10:40 PM
Caching and headers - by El Forum - 07-21-2008, 11:26 PM
Caching and headers - by El Forum - 07-22-2008, 10:12 AM
Caching and headers - by El Forum - 07-22-2008, 10:26 AM
Caching and headers - by El Forum - 07-22-2008, 12:21 PM
Caching and headers - by El Forum - 07-22-2008, 01:01 PM
Caching and headers - by El Forum - 07-22-2008, 01:09 PM
Caching and headers - by El Forum - 07-22-2008, 01:14 PM
Caching and headers - by El Forum - 07-22-2008, 01:37 PM
Caching and headers - by El Forum - 07-22-2008, 01:44 PM
Caching and headers - by El Forum - 07-22-2008, 03:39 PM
Caching and headers - by El Forum - 07-22-2008, 03:42 PM
Caching and headers - by El Forum - 07-22-2008, 03:43 PM
Caching and headers - by El Forum - 07-22-2008, 03:49 PM
Caching and headers - by El Forum - 07-22-2008, 04:08 PM
Caching and headers - by El Forum - 07-22-2008, 04:31 PM
Caching and headers - by El Forum - 07-22-2008, 04:45 PM
Caching and headers - by El Forum - 07-22-2008, 04:47 PM
Caching and headers - by El Forum - 07-22-2008, 04:59 PM
Caching and headers - by El Forum - 07-22-2008, 05:49 PM
Caching and headers - by El Forum - 07-22-2008, 06:58 PM
Caching and headers - by El Forum - 07-22-2008, 06:59 PM
Caching and headers - by El Forum - 07-22-2008, 07:30 PM
Caching and headers - by El Forum - 07-22-2008, 08:08 PM
Caching and headers - by El Forum - 07-22-2008, 08:58 PM
Caching and headers - by El Forum - 07-22-2008, 08:59 PM
Caching and headers - by El Forum - 10-27-2008, 03:55 AM
Caching and headers - by El Forum - 10-27-2008, 10:33 AM
Caching and headers - by El Forum - 10-27-2008, 11:55 AM
Caching and headers - by El Forum - 11-11-2008, 03:59 AM
Caching and headers - by El Forum - 11-11-2008, 05:29 AM
Caching and headers - by El Forum - 11-11-2008, 06:08 AM
Caching and headers - by El Forum - 11-11-2008, 01:12 PM
Caching and headers - by El Forum - 11-12-2008, 12:11 AM
Caching and headers - by El Forum - 11-12-2008, 03:25 AM
Caching and headers - by El Forum - 11-12-2008, 04:28 AM
Caching and headers - by El Forum - 11-12-2008, 04:45 AM
Caching and headers - by El Forum - 11-12-2008, 06:48 AM
Caching and headers - by El Forum - 11-12-2008, 07:22 AM
Caching and headers - by El Forum - 11-12-2008, 07:55 AM
Caching and headers - by El Forum - 11-12-2008, 10:44 AM
Caching and headers - by El Forum - 11-13-2008, 03:23 AM
Caching and headers - by El Forum - 11-13-2008, 04:48 AM
Caching and headers - by El Forum - 11-13-2008, 05:17 AM
Caching and headers - by El Forum - 11-13-2008, 05:41 AM
Caching and headers - by El Forum - 11-13-2008, 12:17 PM
Caching and headers - by El Forum - 11-13-2008, 12:27 PM
Caching and headers - by El Forum - 11-13-2008, 12:34 PM
Caching and headers - by El Forum - 11-17-2008, 03:12 AM
Caching and headers - by El Forum - 11-17-2008, 05:01 AM
Caching and headers - by El Forum - 11-17-2008, 07:32 AM
Caching and headers - by El Forum - 11-17-2008, 07:57 AM
Caching and headers - by El Forum - 11-17-2008, 03:26 PM
Caching and headers - by El Forum - 11-17-2008, 03:45 PM
Caching and headers - by El Forum - 11-17-2008, 10:53 PM
Caching and headers - by El Forum - 11-18-2008, 02:47 AM
Caching and headers - by El Forum - 11-18-2008, 03:18 AM
Caching and headers - by El Forum - 06-23-2009, 03:40 AM
Caching and headers - by El Forum - 01-22-2010, 08:22 AM
Caching and headers - by El Forum - 06-04-2012, 10:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB