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

[eluser]narkaT[/eluser]
[quote author="Arjen van Bochoven" date="1226978818"]I've spoken too soon, I forgot Call-time pass-by-reference" is deprecated, so to make this work we have to change the function definition of _display()[/quote]
you're right.

I wonder why my tests not triggered any warnings, I used a "recent php version"
for that.
the manual says that a warning message should have been generated.

[quote author="Arjen van Bochoven" date="1226978818"]I don’t know if adding the ref is breaking something (php4, anybody?)[/quote]
I've found some interesting comment in the manual.

taking that into account the following would not work for php4
Code:
function _display(&$output = '')

tricky... :roll:
#92

[eluser]Henry Weismann[/eluser]
This is exactly what I was looking for! I am building my applications to load views in a css view folder and output it as css after minimizing it and dynamically setting expire headers. In the end it makes one nice cached, minimized css file and a single http request. I did the same for JavaScript. Does anybody know if I can set it to be gzipped as well and how that will interact with caching?
#93

[eluser]Arjen van Bochoven[/eluser]
[quote author="hcamelion" date="1227005591"]Does anybody know if I can set it to be gzipped as well and how that will interact with caching?[/quote]

If you set
Code:
$config['compress_output'] = TRUE;
in config.php, output will be gzipped on demand. Caching happens before this, so the cache files don't get gzipped.
#94

[eluser]Aquillyne[/eluser]
[quote author="hcamelion" date="1227005591"]This is exactly what I was looking for! I am building my applications to load views in a css view folder and output it as css after minimizing it and dynamically setting expire headers. In the end it makes one nice cached, minimized css file and a single http request. I did the same for JavaScript. Does anybody know if I can set it to be gzipped as well and how that will interact with caching?[/quote]

Would you mind sharing your code for the CSS minimization, and your javascript view??
#95

[eluser]Phil Sturgeon[/eluser]
Time for a little resurrection. I posted here pointing out a few of my concerns with this header caching method.

It is clearly an improvement over the core Output library, but it still has a little way to go until it is perfectly usable in all situations.

Perhaps storing the Content type header as part of the md5 hash would be an idea?
#96

[eluser]pacifika[/eluser]
Thanks this is very useful to me, I am parsing an external page then retrieving content of an image on that page, now I can cache the image content and serve it without issues.
#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;
}
}




Theme © iAndrew 2016 - Forum software by © MyBB