sfMemcacheCache.class.php 6.2 KB

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