initial commit
[namibia] / public / 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         if (! $this->_fetch($id)) {
64             return false;
65         }
66         return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
67             ? mb_strlen($this->_data, '8bit')
68             : strlen($this->_data);
69     }
70     
71     /**
72      * Does a valid cache entry exist?
73      *
74      * @param string $id cache id
75      * 
76      * @param int $srcMtime mtime of the original source file(s)
77      * 
78      * @return bool exists
79      */
80     public function isValid($id, $srcMtime)
81     {
82         return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
83     }
84     
85     /**
86      * Send the cached content to output
87      *
88      * @param string $id cache id
89      */
90     public function display($id)
91     {
92         echo $this->_fetch($id)
93             ? $this->_data
94             : '';
95     }
96     
97         /**
98      * Fetch the cached content
99      *
100      * @param string $id cache id
101      * 
102      * @return string
103      */
104     public function fetch($id)
105     {
106         return $this->_fetch($id)
107             ? $this->_data
108             : '';
109     }
110     
111     private $_mc = null;
112     private $_exp = null;
113     
114     // cache of most recently fetched id
115     private $_lm = null;
116     private $_data = null;
117     private $_id = null;
118     
119         /**
120      * Fetch data and timestamp from memcache, store in instance
121      * 
122      * @param string $id
123      * 
124      * @return bool success
125      */
126     private function _fetch($id)
127     {
128         if ($this->_id === $id) {
129             return true;
130         }
131         $ret = $this->_mc->get($id);
132         if (false === $ret) {
133             $this->_id = null;
134             return false;
135         }
136         list($this->_lm, $this->_data) = explode('|', $ret, 2);
137         $this->_id = $id;
138         return true;
139     }
140 }