File: cacheservice.php

package info (click to toggle)
scuttle 0.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,508 kB
  • ctags: 1,956
  • sloc: php: 8,036; sh: 127; makefile: 48; sql: 43
file content (38 lines) | stat: -rw-r--r-- 999 bytes parent folder | download | duplicates (3)
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();
    }
}
?>