| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfMemcacheCache extends sfCache |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$memcache = null; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
* Initializes this sfCache instance. |
|---|
| 26 |
* |
|---|
| 27 |
* Available options: |
|---|
| 28 |
* |
|---|
| 29 |
* * memcache: A memcache object (optional) |
|---|
| 30 |
* |
|---|
| 31 |
* * host: The default host (default to localhost) |
|---|
| 32 |
* * port: The port for the default server (default to 11211) |
|---|
| 33 |
* * persistent: true if the connection must be persistent, false otherwise (true by default) |
|---|
| 34 |
* |
|---|
| 35 |
* * servers: An array of additional servers (keys: host, port, persistent) |
|---|
| 36 |
* |
|---|
| 37 |
* * see sfCache for options available for all drivers |
|---|
| 38 |
* |
|---|
| 39 |
* @see sfCache |
|---|
| 40 |
*/ |
|---|
| 41 |
public function initialize($options = array()) |
|---|
| 42 |
{ |
|---|
| 43 |
parent::initialize($options); |
|---|
| 44 |
|
|---|
| 45 |
if (!class_exists('Memcache')) |
|---|
| 46 |
{ |
|---|
| 47 |
throw new sfInitializationException('You must have memcache installed and enabled to use sfMemcacheCache class.'); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
if ($this->getOption('memcache')) |
|---|
| 51 |
{ |
|---|
| 52 |
$this->memcache = $this->getOption('memcache'); |
|---|
| 53 |
} |
|---|
| 54 |
else |
|---|
| 55 |
{ |
|---|
| 56 |
$this->memcache = new Memcache(); |
|---|
| 57 |
|
|---|
| 58 |
if ($this->getOption('servers')) |
|---|
| 59 |
{ |
|---|
| 60 |
foreach ($this->getOption('servers') as $server) |
|---|
| 61 |
{ |
|---|
| 62 |
$port = isset($server['port']) ? $server['port'] : 11211; |
|---|
| 63 |
if (!$this->memcache->addServer($server['host'], $port, isset($server['persistent']) ? $server['persistent'] : true)) |
|---|
| 64 |
{ |
|---|
| 65 |
throw new sfInitializationException(sprintf('Unable to connect to the memcache server (%s:%s).', $server['host'], $port)); |
|---|
| 66 |
} |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
else |
|---|
| 70 |
{ |
|---|
| 71 |
$method = $this->getOption('persistent', true) ? 'pconnect' : 'connect'; |
|---|
| 72 |
if (!$this->memcache->$method($this->getOption('host', 'localhost'), $this->getOption('port', 11211), $this->getOption('timeout', 1))) |
|---|
| 73 |
{ |
|---|
| 74 |
throw new sfInitializationException(sprintf('Unable to connect to the memcache server (%s:%s).', $this->getOption('host', 'localhost'), $this->getOption('port', 11211))); |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
* @see sfCache |
|---|
| 82 |
*/ |
|---|
| 83 |
public function getBackend() |
|---|
| 84 |
{ |
|---|
| 85 |
return $this->memcache; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* @see sfCache |
|---|
| 90 |
*/ |
|---|
| 91 |
public function get($key, $default = null) |
|---|
| 92 |
{ |
|---|
| 93 |
$value = $this->memcache->get($this->getOption('prefix').$key); |
|---|
| 94 |
|
|---|
| 95 |
return false === $value ? $default : $value; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
* @see sfCache |
|---|
| 100 |
*/ |
|---|
| 101 |
public function has($key) |
|---|
| 102 |
{ |
|---|
| 103 |
return !(false === $this->memcache->get($this->getOption('prefix').$key)); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
* @see sfCache |
|---|
| 108 |
*/ |
|---|
| 109 |
public function set($key, $data, $lifetime = null) |
|---|
| 110 |
{ |
|---|
| 111 |
$lifetime = is_null($lifetime) ? $this->getOption('lifetime') : $lifetime; |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
$this->setMetadata($key, $lifetime); |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
if ($this->getOption('storeCacheInfo', false)) |
|---|
| 118 |
{ |
|---|
| 119 |
$this->setCacheInfo($key); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
return $this->memcache->set($this->getOption('prefix').$key, $data, false, $lifetime); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* @see sfCache |
|---|
| 127 |
*/ |
|---|
| 128 |
public function remove($key) |
|---|
| 129 |
{ |
|---|
| 130 |
$this->memcache->delete($this->getOption('prefix').'_metadata'.self::SEPARATOR.$key); |
|---|
| 131 |
|
|---|
| 132 |
return $this->memcache->delete($this->getOption('prefix').$key); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
* @see sfCache |
|---|
| 137 |
*/ |
|---|
| 138 |
public function clean($mode = sfCache::ALL) |
|---|
| 139 |
{ |
|---|
| 140 |
if (sfCache::ALL === $mode) |
|---|
| 141 |
{ |
|---|
| 142 |
return $this->memcache->flush(); |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
* @see sfCache |
|---|
| 148 |
*/ |
|---|
| 149 |
public function getLastModified($key) |
|---|
| 150 |
{ |
|---|
| 151 |
if (false === ($retval = $this->getMetadata($key))) |
|---|
| 152 |
{ |
|---|
| 153 |
return 0; |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
return $retval['lastModified']; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
* @see sfCache |
|---|
| 161 |
*/ |
|---|
| 162 |
public function getTimeout($key) |
|---|
| 163 |
{ |
|---|
| 164 |
if (false === ($retval = $this->getMetadata($key))) |
|---|
| 165 |
{ |
|---|
| 166 |
return 0; |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
return $retval['timeout']; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
* @see sfCache |
|---|
| 174 |
*/ |
|---|
| 175 |
public function removePattern($pattern) |
|---|
| 176 |
{ |
|---|
| 177 |
if (!$this->getOption('storeCacheInfo', false)) |
|---|
| 178 |
{ |
|---|
| 179 |
throw new sfCacheException('To use the "removePattern" method, you must set the "storeCacheInfo" option to "true".'); |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
$regexp = self::patternToRegexp($this->getOption('prefix').$pattern); |
|---|
| 183 |
|
|---|
| 184 |
foreach ($this->getCacheInfo() as $key) |
|---|
| 185 |
{ |
|---|
| 186 |
if (preg_match($regexp, $key)) |
|---|
| 187 |
{ |
|---|
| 188 |
$this->memcache->delete($key); |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
* @see sfCache |
|---|
| 195 |
*/ |
|---|
| 196 |
public function getMany($keys) |
|---|
| 197 |
{ |
|---|
| 198 |
$values = array(); |
|---|
| 199 |
foreach ($this->memcache->get(array_map(create_function('$k', 'return "'.$this->getOption('prefix').'".$k;'), $keys)) as $key => $value) |
|---|
| 200 |
{ |
|---|
| 201 |
$values[str_replace($this->getOption('prefix'), '', $key)] = $value; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
return $values; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
* Gets metadata about a key in the cache. |
|---|
| 209 |
* |
|---|
| 210 |
* @param string $key A cache key |
|---|
| 211 |
* |
|---|
| 212 |
* @return array An array of metadata information |
|---|
| 213 |
*/ |
|---|
| 214 |
protected function getMetadata($key) |
|---|
| 215 |
{ |
|---|
| 216 |
return $this->memcache->get($this->getOption('prefix').'_metadata'.self::SEPARATOR.$key); |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
* Stores metadata about a key in the cache. |
|---|
| 221 |
* |
|---|
| 222 |
* @param string $key A cache key |
|---|
| 223 |
* @param string $key The lifetime |
|---|
| 224 |
*/ |
|---|
| 225 |
protected function setMetadata($key, $lifetime) |
|---|
| 226 |
{ |
|---|
| 227 |
$this->memcache->set($this->getOption('prefix').'_metadata'.self::SEPARATOR.$key, array('lastModified' => time(), 'timeout' => time() + $lifetime), false, $lifetime); |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
* Updates the cache information for the given cache key. |
|---|
| 232 |
* |
|---|
| 233 |
* @param string $key The cache key |
|---|
| 234 |
*/ |
|---|
| 235 |
protected function setCacheInfo($key) |
|---|
| 236 |
{ |
|---|
| 237 |
$keys = $this->memcache->get($this->getOption('prefix').'_metadata'); |
|---|
| 238 |
if (!is_array($keys)) |
|---|
| 239 |
{ |
|---|
| 240 |
$keys = array(); |
|---|
| 241 |
} |
|---|
| 242 |
$keys[] = $this->getOption('prefix').$key; |
|---|
| 243 |
$this->memcache->set($this->getOption('prefix').'_metadata', $keys, 0); |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
* Gets cache information. |
|---|
| 248 |
*/ |
|---|
| 249 |
protected function getCacheInfo() |
|---|
| 250 |
{ |
|---|
| 251 |
$keys = $this->memcache->get($this->getOption('prefix').'_metadata'); |
|---|
| 252 |
if (!is_array($keys)) |
|---|
| 253 |
{ |
|---|
| 254 |
return array(); |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
return $keys; |
|---|
| 258 |
} |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|