]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/Cache/APC.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / 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         return $this->_fetch($id)
58             ? strlen($this->_data)
59             : false;
60     }
61
62     /**
63      * Does a valid cache entry exist?
64      *
65      * @param string $id cache id
66      *
67      * @param int $srcMtime mtime of the original source file(s)
68      *
69      * @return bool exists
70      */
71     public function isValid($id, $srcMtime)
72     {
73         return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
74     }
75
76     /**
77      * Send the cached content to output
78      *
79      * @param string $id cache id
80      */
81     public function display($id)
82     {
83         echo $this->_fetch($id)
84             ? $this->_data
85             : '';
86     }
87
88     /**
89      * Fetch the cached content
90      *
91      * @param string $id cache id
92      *
93      * @return string
94      */
95     public function fetch($id)
96     {
97         return $this->_fetch($id)
98             ? $this->_data
99             : '';
100     }
101
102     private $_exp = null;
103
104     // cache of most recently fetched id
105     private $_lm = null;
106     private $_data = null;
107     private $_id = null;
108
109     /**
110      * Fetch data and timestamp from apc, store in instance
111      *
112      * @param string $id
113      *
114      * @return bool success
115      */
116     private function _fetch($id)
117     {
118         if ($this->_id === $id) {
119             return true;
120         }
121         $ret = apc_fetch($id);
122         if (false === $ret) {
123             $this->_id = null;
124             return false;
125         }
126         list($this->_lm, $this->_data) = explode('|', $ret, 2);
127         $this->_id = $id;
128         return true;
129     }
130 }