initial commit
[namibia] / public / min / lib / Minify / Cache / XCache.php
1 <?php
2 /**
3  * Class Minify_Cache_XCache
4  *
5  * @link http://xcache.lighttpd.net/
6  * @package Minify
7  */
8
9 /**
10  * XCache-based cache class for Minify
11  * {@see http://xcache.lighttpd.net/wiki/XcacheApi XCache API}
12  *
13  * <code>
14  * Minify::setCache(new Minify_Cache_XCache());
15  * </code>
16  *
17  * @package Minify
18  * @author Elan Ruusamäe <glen@delfi.ee>
19  **/
20 class Minify_Cache_XCache {
21
22     /**
23      * Create a Minify_Cache_XCache object, to be passed to
24      * Minify::setCache().
25      *
26      * @param int $expire seconds until expiration (default = 0
27      * meaning the item will not get an expiration date)
28      */
29     public function __construct($expire = 0)
30     {
31         $this->_exp = $expire;
32     }
33
34     /**
35      * Write data to cache.
36      *
37      * @param string $id cache id
38      * @param string $data
39      * @return bool success
40      */
41     public function store($id, $data)
42     {
43         return xcache_set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp);
44     }
45
46     /**
47      * Get the size of a cache entry
48      *
49      * @param string $id cache id
50      * @return int size in bytes
51      */
52     public function getSize($id)
53     {
54         if (! $this->_fetch($id)) {
55             return false;
56         }
57         return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
58             ? mb_strlen($this->_data, '8bit')
59             : strlen($this->_data);
60     }
61
62     /**
63      * Does a valid cache entry exist?
64      *
65      * @param string $id cache id
66      * @param int $srcMtime mtime of the original source file(s)
67      * @return bool exists
68      */
69     public function isValid($id, $srcMtime)
70     {
71         return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
72     }
73
74     /**
75      * Send the cached content to output
76      *
77      * @param string $id cache id
78      */
79     public function display($id)
80     {
81         echo $this->_fetch($id)
82             ? $this->_data
83             : '';
84     }
85
86     /**
87      * Fetch the cached content
88      *
89      * @param string $id cache id
90      * @return string
91      */
92     public function fetch($id)
93     {
94         return $this->_fetch($id)
95             ? $this->_data
96             : '';
97     }
98
99     private $_exp = null;
100
101     // cache of most recently fetched id
102     private $_lm = null;
103     private $_data = null;
104     private $_id = null;
105
106     /**
107      * Fetch data and timestamp from xcache, store in instance
108      *
109      * @param string $id
110      * @return bool success
111      */
112     private function _fetch($id)
113     {
114         if ($this->_id === $id) {
115             return true;
116         }
117         $ret = xcache_get($id);
118         if (false === $ret) {
119             $this->_id = null;
120             return false;
121         }
122         list($this->_lm, $this->_data) = explode('|', $ret, 2);
123         $this->_id = $id;
124         return true;
125     }
126 }