aboutsummaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/Cache.php
blob: fa719d3bd17d842a525d6c735ae322fe8e61bb88 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * SemanticScuttle caching service
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
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();
    }
}
?>