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

Benjamin Mako Hill || Want to submit a patch?