00001 <?php 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00023 class KjwCache {
00024         var $_cachePath = null;
00025         var $_cacheFile = null;
00026         var $_enabled = false;
00027         var $_expire = null;
00028         
00029         function KjwCache($cachePath = 'cache') {
00030                 $this->_cachePath = $cachePath;
00031         }
00032 
00033         function expire($seconds) {
00034                 $this->_expire = $seconds;
00035         }
00036 
00037         function start() {
00038                 
00039                 
00040 
00041 
00042 
00043                 if (!isset($_REQUEST[session_name()])) {
00044                 
00045                         $this->_cacheFile = $this->_cachePath . '/' . md5($_SERVER['REQUEST_URI']) . '.cache';
00046 
00047                         
00048                         if (@file_exists($this->_cacheFile)
00049                                         && ($this->_expire === null || filemtime($this->_cacheFile) + $this->_expire > time())) {
00050                                 
00051                                 header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', filemtime($this->_cacheFile)));
00052                                 if ($this->_expire !== null)
00053                                         header('Expires: ' . gmdate('D, d M Y H:i:s T', filemtime($this->_cacheFile) + $this->_expire));
00054                                 
00055                                 
00056                                 if (function_exists('apache_request_headers')) {
00057                                         $headers = apache_request_headers();
00058                                         if (isset($headers['If-Modified-Since'])
00059                                                         && filemtime($this->_cacheFile) <= strtotime($headers['If-Modified-Since'])) {
00060                                                 header("HTTP/1.0 304 Not Modified");
00061                                                 exit;
00062                                         }
00063                                 }
00064 
00065                                 
00066                                 readfile($this->_cacheFile);
00067                                 exit;
00068                         }
00069 
00070                         
00071                         if ($this->_expire !== null)
00072                                 header('Expires: ' . gmdate('D, d M Y H:i:s T', time() + $this->_expire));
00073                                 
00074                         
00075                         ob_start();
00076                         $this->_enabled = true;
00077                         
00078                 } else {
00079 
00080                         
00081                         
00082                         header('Cache-Control: no-cache, must-revalidate'); 
00083                         header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', time()));
00084                         header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
00085 
00086                 }
00087         }
00088 
00093         function setDirty() {
00094                 $n = 0;
00095                 $dp = opendir($this->_cachePath);
00096                 while (($entry = readdir($dp)) !== false)
00097                         if (substr($entry, -6) == '.cache')
00098                                 if (unlink($this->_cachePath . '/' . $entry))
00099                                         ++$n;
00100                 closedir($dp);
00101                 return $n;
00102         }
00103 
00109         function saveCache() {
00110                 $n = 0;
00111                 $dp = opendir($this->_cachePath);
00112                 while (($entry = readdir($dp)) !== false)
00113                         if (substr($entry, -10) == '.cache.bak')
00114                                 unlink($this->_cachePath . '/' . $entry);
00115                 rewinddir($dp);
00116                 while (($entry = readdir($dp)) !== false)
00117                         if (substr($entry, -6) == '.cache')
00118                                 if (copy($this->_cachePath . '/' . $entry, $this->_cachePath . '/' . $entry . '.bak'))
00119                                         ++$n;
00120                 closedir($dp);
00121                 return $n;
00122         }
00123 
00128         function loadCache() {
00129                 $n = 0;
00130                 $dp = opendir($this->_cachePath);
00131                 while (($entry = readdir($dp)) !== false)
00132                         if (substr($entry, -6) == '.cache')
00133                                 unlink($this->_cachePath . '/' . $entry);
00134                 rewinddir($dp);
00135                 while (($entry = readdir($dp)) !== false)
00136                         if (substr($entry, -10) == '.cache.bak')
00137                                 if (copy($this->_cachePath . '/' . $entry, $this->_cachePath . '/' . substr($entry, 0, -4)))
00138                                         ++$n;
00139                 closedir($dp);
00140                 return $n;
00141         }
00142 
00143         function destroy() {
00144                 if ($this->_enabled) {
00145                         echo "<!-- cached file created: " . gmdate("Y-m-d H:i") . " UTC -->";
00146                         if (($fp = @fopen($this->_cacheFile, 'w')) !== false) {
00147                                 $success = @fwrite($fp, ($towrite = ob_get_contents())) === strlen($towrite);
00148                                 @fclose($fp);
00149                                 if ($success === false)
00150                                         unlink($this->_cacheFile);
00151                         }
00152                         ob_end_flush();
00153                 }
00154         }
00155 }
00156 
00157 ?>