aboutsummaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/Cache.php
blob: 5ca284355ef64771973b3d0d69b5ea03d5314f91 (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
39
40
41
42
43
44
45
46
47
<?php
class SemanticScuttle_Service_Cache extends SemanticScuttle_Service
{
    var $basedir;
    var $fileextension = '.cache';

    /**
     * Returns the single service instance
     *
     * @param DB $db Database object
     *
     * @return SemanticScuttle_Service
     */
	public static function getInstance($db)
    {
		static $instance;
		if (!isset($instance)) {
            $instance = new self($db);
        }
		return $instance;
	}

    protected function __construct()
    {
        $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();
    }
}
?>