public
Description: Some PHP libs
Homepage: http://mihasya.com
Clone URL: git://github.com/mihasya/mihasya_libs.git
mihasya_libs / mihasya_cache.php
100644 101 lines (95 sloc) 3.134 kb
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
abstract class CacheNamespace {
    public static $handlers;
    public static $conf = array();
}
 
function cache($hdlName) {
    if (!CacheNamespace::$handlers[$hdlName]) {
        if (!CacheNamespace::$conf[$hdlName]) return false; //init hasnt been run
        //go on and create the connection
        $conf = CacheNamespace::$conf[$hdlName];
        $className = $conf['type'].'CacheHdl';
        $hdl = new $className($conf['initArgs']);
        CacheNamespace::$handlers[$hdlName] = $hdl;
    }
    return CacheNamespace::$handlers[$hdlName];
}
 
function cache_init($hdlName, $type='file', $initArgs = array()) {
    $conf = array(
        'type'=>$type,
        'initArgs'=>$initArgs
    );
    CacheNamespace::$conf[$hdlName] = $conf;
}
 
abstract class cacheHdl {
    abstract function __construct($initArgs);
    abstract function get($key);
    abstract function set($key, $value, $ttl=null);
    abstract function delete($key, $timeout=null);
    protected function _getRealKey($key) {
        //TODO: make this modifiable so user can define own hash/salt
        return md5($key);
    }
}
 
class apcCacheHdl extends cacheHdl{
    function __construct($initArgs) {
        if (!function_exists('apc_fetch')) {
            throw new Exception ("APC does not appear to be enabled");
        }
    }
    function get($key) {
        return apc_fetch($this->_getRealKey($key));
    }
    function set($key, $value, $ttl=null) {
        return apc_store($this->_getRealKey($key), $value, $ttl);
    }
    function delete($key, $timeout=null) {
        if (!$timeout) {
            return apc_delete($this->_getRealKey($key));
        } else {
            return apc_store($this->_getRealKey($key), $value, $timeout);
        }
    }
}
class fileCacheHdl extends cacheHdl{
    private $_path;
 
    function __construct($initArgs) {
        $this->_path = $initArgs['path'] ? $initArgs['path'] : '.';
    }
    function get($key) {
        $realKey = $this->_getRealKey($key);
        $keyPath = $this->_path.'/'.$realKey;
        try {
            $fcontent = @fread(@fopen($keyPath, 'r'), filesize($keyPath));
        } catch (Exception $e) {
            return false;
        }
        if (!$fcontent) { return false; }
        $content = unserialize($fcontent);
        if ($content->ttl < time() && $content->ttl != 0) {
            unlink($keyPath);
            return false;
        }
        return $content->value;
    }
    function set($key, $value, $ttl=null) {
        $realKey = $this->_getRealKey($key);
        $ttl = $ttl ? time()+$ttl : 0;
        $content = new stdClass();
        $content->ttl = $ttl;
        $content->value = $value;
        $f = fopen($this->_path.'/'.$realKey, 'w+');
        return @fwrite(@fopen($this->_path.'/'.$realKey, 'w+'), serialize($content));
    }
    function delete($key, $timeout=null) {
        if (!$timeout) {
            return unlink($this->_path.'/'.$this->_getRealKey($key));
        } else {
            if (!$value = $this->get($key)) {
                return false;
            }
            return $this->set($key, $value, $timeout);
        }
    }
}
?>