Restructure repository
[scuttle] / services / cacheservice.php
1 <?php
2 class CacheService {
3     var $basedir;
4     var $fileextension = '.cache';
5
6     function &getInstance() {
7         static $instance;
8         
9         if (!isset($instance))
10             $instance =& new CacheService();
11
12         return $instance;
13     }
14
15     function CacheService() {
16         $this->basedir = $GLOBALS['dir_cache'];    
17     }
18
19     function Start($hash, $time = 300) {
20         $cachefile = $this->basedir .'/'. $hash . $this->fileextension;
21         if (file_exists($cachefile) && time() < filemtime($cachefile) + $time) {
22             @readfile($cachefile);
23             echo "\n<!-- Cached: ". date('r', filemtime($cachefile)) ." -->\n";
24             unset($cachefile);
25             exit;
26         }
27         ob_start("ob_gzhandler");
28     }
29
30     function End($hash) {
31         $cachefile = $this->basedir .'/'. $hash . $this->fileextension;      
32         $handle = fopen($cachefile, 'w');
33         fwrite($handle, ob_get_contents());
34         fclose($handle);
35         ob_flush();
36     }
37 }
38 ?>

Benjamin Mako Hill || Want to submit a patch?