| 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 implements Minify_CacheInterface |
| 21 |
{ |
| 22 |
|
| 23 |
/** |
| 24 |
* Create a Minify_Cache_Memcache object, to be passed to |
| 25 |
* Minify::setCache(). |
| 26 |
* |
| 27 |
* @param Memcache $memcache already-connected instance |
| 28 |
* |
| 29 |
* @param int $expire seconds until expiration (default = 0 |
| 30 |
* meaning the item will not get an expiration date) |
| 31 |
*/ |
| 32 |
public function __construct($memcache, $expire = 0) |
| 33 |
{ |
| 34 |
$this->_mc = $memcache; |
| 35 |
$this->_exp = $expire; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Write data to cache. |
| 40 |
* |
| 41 |
* @param string $id cache id |
| 42 |
* |
| 43 |
* @param string $data |
| 44 |
* |
| 45 |
* @return bool success |
| 46 |
*/ |
| 47 |
public function store($id, $data) |
| 48 |
{ |
| 49 |
return $this->_mc->set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", 0, $this->_exp); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get the size of a cache entry |
| 54 |
* |
| 55 |
* @param string $id cache id |
| 56 |
* |
| 57 |
* @return int size in bytes |
| 58 |
*/ |
| 59 |
public function getSize($id) |
| 60 |
{ |
| 61 |
if (! $this->_fetch($id)) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { |
| 66 |
return mb_strlen($this->_data, '8bit'); |
| 67 |
} else { |
| 68 |
return strlen($this->_data); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Does a valid cache entry exist? |
| 74 |
* |
| 75 |
* @param string $id cache id |
| 76 |
* |
| 77 |
* @param int $srcMtime mtime of the original source file(s) |
| 78 |
* |
| 79 |
* @return bool exists |
| 80 |
*/ |
| 81 |
public function isValid($id, $srcMtime) |
| 82 |
{ |
| 83 |
return ($this->_fetch($id) && ($this->_lm >= $srcMtime)); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Send the cached content to output |
| 88 |
* |
| 89 |
* @param string $id cache id |
| 90 |
*/ |
| 91 |
public function display($id) |
| 92 |
{ |
| 93 |
echo $this->_fetch($id) ? $this->_data : ''; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Fetch the cached content |
| 98 |
* |
| 99 |
* @param string $id cache id |
| 100 |
* |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
public function fetch($id) |
| 104 |
{ |
| 105 |
return $this->_fetch($id) ? $this->_data : ''; |
| 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 |
|
| 129 |
$ret = $this->_mc->get($id); |
| 130 |
if (false === $ret) { |
| 131 |
$this->_id = null; |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
list($this->_lm, $this->_data) = explode('|', $ret, 2); |
| 137 |
$this->_id = $id; |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
} |
| 142 |
|