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