summaryrefslogtreecommitdiff
path: root/services/cacheservice.php
blob: fe66d38e995d23e38804267f0cffa75ec8480d6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
class CacheService {
    var $basedir;
    var $fileextension = '.cache';

    function &getInstance() {
        static $instance;
        
        if (!isset($instance))
            $instance =& new CacheService();

        return $instance;
    }

    function CacheService() {
        $this->basedir = $GLOBALS['dir_cache'];    
    }

    function Start($hash, $time = 300) {
        $cachefile = $this->basedir .'/'. $hash . $this->fileextension;
        if (file_exists($cachefile) && time() < filemtime($cachefile) + $time) {
            @readfile($cachefile);
            echo "\n<!-- Cached: ". date('r', filemtime($cachefile)) ." -->\n";
            unset($cachefile);
            exit;
        }
        ob_start("ob_gzhandler");
    }

    function End($hash) {
        $cachefile = $this->basedir .'/'. $hash . $this->fileextension;      
        $handle = fopen($cachefile, 'w');
        fwrite($handle, ob_get_contents());
        fclose($handle);
        ob_flush();
    }
}
?>