-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Clear Cache by POST
If you want use form->open() without setting the action, but still want to cache the page, this modification will be sure to clear the page's cache once any form values are submitted. Useful for global login forms, especially in conjunction with [url=http://codeigniter.com/wiki/Slightly_better_caching/]Slightly Better Caching[/url]
Get_cache_URI can be refactored a step more, but I like to log the un-MD5ed URI as well, so I left it a little less optimized.
[size=3]Output Modification Without Slightly Better Caching[/size]
[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
-
For caching global logins
-
Responsible for sending final output to browser
-
@package CodeIgniter
-
@subpackage Libraries
-
@author Bradford Mar
-
@adapted-from nmweb(http://codeigniter.com/forums/viewthread/62951/) */ class MY_Output extends CI_Output {
/**
- Get the uri of a given cached page
- @access public
- @return void */
function get_cache_URI(){ $CFG =& load_class('Config'); $RTR =& load_class('Router');
$cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path'); if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) { return FALSE; } $uri = $CFG->item('base_url'). $CFG->item('index_page'). $RTR->uri->uri_string; return array($cache_path,$uri);}
// --------------------------------------------------------------------
/**
- Manually clear a cached file
- @access public
- @return void */
function clear_page_cache(){ $cacheuri = $this->get_cache_URI(); $uri = $cacheuri[1]; $cache_path = $cacheuri[0]; $cache_path .= md5($uri); if(file_exists($cache_path)) { touch($cache_path); unlink($cache_path); } else { return false; } }
// --------------------------------------------------------------------
/**
-
Update/serve a cached file
-
@access public
-
@return void */
function _display_cache(&$CFG, &$RTR) { if ($_POST != NULL) { $this->clear_page_cache(); }$cacheuri = $this->get_cache_URI(); $uri = $cacheuri[1]; $cache_path = $cacheuri[0]; $filepath = $cache_path.md5($uri);
if ( ! @file_exists($filepath)) { return FALSE; }
if ( ! $fp = @fopen($filepath, 'rb')) { 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
if ( ! preg_match("/(\d+TS--->)/", $cache, $match)) { return FALSE; }// 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; }// Display the cache $this->_display(str_replace($match['0'], '', $cache)); log_message('debug', "Cache file is current. Sending it to browser.");
return TRUE; }
} // END MY_Output Class
/* End of file MY_Output.php / / Location: ./system/application/libraries/MY_Output.php */ [/code]
[size=3]Output Modification for Slightly Better Caching[/size]
Add this above "return array($cache_path,$uri);" in get_cache_URI() [code] // Get type type if ( FALSE !== ($type = $this->_get_from_session($CFG->item('cache_type_source'))) ) { $uri.= 'sess:'.$type; } [/code]
And add this to the beginning of _display_cache() [code] // Start session session_start(); [/code]