]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/Cache/Memcache.php
Issue #166 - we test exif data below, no need for error output
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / min / lib / Minify / Cache / Memcache.php
1 <?php
2 /**
3  * Class Minify_Cache_Memcache
4  * @package Minify
5  */
6
7 /**
8  * Memcache-based cache class for Minify
9  * 
10  * <code>
11  * // fall back to disk caching if memcache can't connect
12  * $memcache = new Memcache;
13  * if ($memcache->connect('localhost', 11211)) {
14  *     Minify::setCache(new Minify_Cache_Memcache($memcache));
15  * } else {
16  *     Minify::setCache();
17  * }
18  * </code>
19  **/
20 class Minify_Cache_Memcache {
21     
22     /**
23      * Create a Minify_Cache_Memcache object, to be passed to 
24      * Minify::setCache().
25      *
26      * @param Memcache $memcache already-connected instance
27      * 
28      * @param int $expire seconds until expiration (default = 0
29      * meaning the item will not get an expiration date)
30      * 
31      * @return null
32      */
33     public function __construct($memcache, $expire = 0)
34     {
35         $this->_mc = $memcache;
36         $this->_exp = $expire;
37     }
38     
39     /**
40      * Write data to cache.
41      *
42      * @param string $id cache id
43      * 
44      * @param string $data
45      * 
46      * @return bool success
47      */
48     public function store($id, $data)
49     {
50         return $this->_mc->set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", 0, $this->_exp);
51     }
52     
53     
54     /**
55      * Get the size of a cache entry
56      *
57      * @param string $id cache id
58      * 
59      * @return int size in bytes
60      */
61     public function getSize($id)
62     {
63         return $this->_fetch($id)
64             ? strlen($this->_data)
65             : false;
66     }
67     
68     /**
69      * Does a valid cache entry exist?
70      *
71      * @param string $id cache id
72      * 
73      * @param int $srcMtime mtime of the original source file(s)
74      * 
75      * @return bool exists
76      */
77     public function isValid($id, $srcMtime)
78     {
79         return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
80     }
81     
82     /**
83      * Send the cached content to output
84      *
85      * @param string $id cache id
86      */
87     public function display($id)
88     {
89         echo $this->_fetch($id)
90             ? $this->_data
91             : '';
92     }
93     
94         /**
95      * Fetch the cached content
96      *
97      * @param string $id cache id
98      * 
99      * @return string
100      */
101     public function fetch($id)
102     {
103         return $this->_fetch($id)
104             ? $this->_data
105             : '';
106     }
107     
108     private $_mc = null;
109     private $_exp = null;
110     
111     // cache of most recently fetched id
112     private $_lm = null;
113     private $_data = null;
114     private $_id = null;
115     
116         /**
117      * Fetch data and timestamp from memcache, store in instance
118      * 
119      * @param string $id
120      * 
121      * @return bool success
122      */
123     private function _fetch($id)
124     {
125         if ($this->_id === $id) {
126             return true;
127         }
128         $ret = $this->_mc->get($id);
129         if (false === $ret) {
130             $this->_id = null;
131             return false;
132         }
133         list($this->_lm, $this->_data) = explode('|', $ret, 2);
134         $this->_id = $id;
135         return true;
136     }
137 }