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 if (! $this->_fetch($id)) {
66 return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
67 ? mb_strlen($this->_data, '8bit')
68 : strlen($this->_data);
72 * Does a valid cache entry exist?
74 * @param string $id cache id
76 * @param int $srcMtime mtime of the original source file(s)
80 public function isValid($id, $srcMtime)
82 return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
86 * Send the cached content to output
88 * @param string $id cache id
90 public function display($id)
92 echo $this->_fetch($id)
98 * Fetch the cached content
100 * @param string $id cache id
104 public function fetch($id)
106 return $this->_fetch($id)
112 private $_exp = null;
114 // cache of most recently fetched id
116 private $_data = null;
120 * Fetch data and timestamp from memcache, store in instance
124 * @return bool success
126 private function _fetch($id)
128 if ($this->_id === $id) {
131 $ret = $this->_mc->get($id);
132 if (false === $ret) {
136 list($this->_lm, $this->_data) = explode('|', $ret, 2);