3 * Class Minify_Cache_Memcache
8 * Memcache-based cache class for Minify
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));
20 class Minify_Cache_Memcache {
23 * Create a Minify_Cache_Memcache object, to be passed to
26 * @param Memcache $memcache already-connected instance
28 * @param int $expire seconds until expiration (default = 0
29 * meaning the item will not get an expiration date)
33 public function __construct($memcache, $expire = 0)
35 $this->_mc = $memcache;
36 $this->_exp = $expire;
40 * Write data to cache.
42 * @param string $id cache id
46 * @return bool success
48 public function store($id, $data)
50 return $this->_mc->set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", 0, $this->_exp);
55 * Get the size of a cache entry
57 * @param string $id cache id
59 * @return int size in bytes
61 public function getSize($id)
63 return $this->_fetch($id)
64 ? strlen($this->_data)
69 * Does a valid cache entry exist?
71 * @param string $id cache id
73 * @param int $srcMtime mtime of the original source file(s)
77 public function isValid($id, $srcMtime)
79 return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
83 * Send the cached content to output
85 * @param string $id cache id
87 public function display($id)
89 echo $this->_fetch($id)
95 * Fetch the cached content
97 * @param string $id cache id
101 public function fetch($id)
103 return $this->_fetch($id)
109 private $_exp = null;
111 // cache of most recently fetched id
113 private $_data = null;
117 * Fetch data and timestamp from memcache, store in instance
121 * @return bool success
123 private function _fetch($id)
125 if ($this->_id === $id) {
128 $ret = $this->_mc->get($id);
129 if (false === $ret) {
133 list($this->_lm, $this->_data) = explode('|', $ret, 2);