MemcacheEngine.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Memcache storage engine for cache
  4. *
  5. *
  6. * PHP 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package cake.libs.cache
  17. * @since CakePHP(tm) v 1.2.0.4933
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Memcache storage engine for cache. Memcache has some limitations in the amount of
  22. * control you have over expire times far in the future. See MemcacheEngine::write() for
  23. * more information.
  24. *
  25. * @package cake.libs.cache
  26. */
  27. class MemcacheEngine extends CacheEngine {
  28. /**
  29. * Memcache wrapper.
  30. *
  31. * @var Memcache
  32. * @access private
  33. */
  34. protected $_Memcache = null;
  35. /**
  36. * Settings
  37. *
  38. * - servers = string or array of memcache servers, default => 127.0.0.1. If an
  39. * array MemcacheEngine will use them as a pool.
  40. * - compress = boolean, default => false
  41. *
  42. * @var array
  43. * @access public
  44. */
  45. public $settings = array();
  46. /**
  47. * Initialize the Cache Engine
  48. *
  49. * Called automatically by the cache frontend
  50. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  51. *
  52. * @param array $settings array of setting for the engine
  53. * @return boolean True if the engine has been successfully initialized, false if not
  54. */
  55. public function init($settings = array()) {
  56. if (!class_exists('Memcache')) {
  57. return false;
  58. }
  59. parent::init(array_merge(array(
  60. 'engine'=> 'Memcache',
  61. 'prefix' => Inflector::slug(APP_DIR) . '_',
  62. 'servers' => array('127.0.0.1'),
  63. 'compress'=> false
  64. ), $settings)
  65. );
  66. if ($this->settings['compress']) {
  67. $this->settings['compress'] = MEMCACHE_COMPRESSED;
  68. }
  69. if (!is_array($this->settings['servers'])) {
  70. $this->settings['servers'] = array($this->settings['servers']);
  71. }
  72. if (!isset($this->_Memcache)) {
  73. $return = false;
  74. $this->_Memcache = new Memcache();
  75. foreach ($this->settings['servers'] as $server) {
  76. list($host, $port) = $this->_parseServerString($server);
  77. if ($this->_Memcache->addServer($host, $port)) {
  78. $return = true;
  79. }
  80. }
  81. return $return;
  82. }
  83. return true;
  84. }
  85. /**
  86. * Parses the server address into the host/port. Handles both IPv6 and IPv4
  87. * addresses
  88. *
  89. * @param string $server The server address string.
  90. * @return array Array containing host, port
  91. */
  92. function _parseServerString($server) {
  93. if (substr($server, 0, 1) == '[') {
  94. $position = strpos($server, ']:');
  95. if ($position !== false) {
  96. $position++;
  97. }
  98. } else {
  99. $position = strpos($server, ':');
  100. }
  101. $port = 11211;
  102. $host = $server;
  103. if ($position !== false) {
  104. $host = substr($server, 0, $position);
  105. $port = substr($server, $position + 1);
  106. }
  107. return array($host, $port);
  108. }
  109. /**
  110. * Write data for key into cache. When using memcache as your cache engine
  111. * remember that the Memcache pecl extension does not support cache expiry times greater
  112. * than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
  113. *
  114. * @param string $key Identifier for the data
  115. * @param mixed $value Data to be cached
  116. * @param integer $duration How long to cache the data, in seconds
  117. * @return boolean True if the data was successfully cached, false on failure
  118. * @see http://php.net/manual/en/memcache.set.php
  119. */
  120. public function write($key, $value, $duration) {
  121. if ($duration > 30 * DAY) {
  122. $duration = 0;
  123. }
  124. return $this->_Memcache->set($key, $value, $this->settings['compress'], $duration);
  125. }
  126. /**
  127. * Read a key from the cache
  128. *
  129. * @param string $key Identifier for the data
  130. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  131. */
  132. public function read($key) {
  133. return $this->_Memcache->get($key);
  134. }
  135. /**
  136. * Increments the value of an integer cached key
  137. *
  138. * @param string $key Identifier for the data
  139. * @param integer $offset How much to increment
  140. * @return New incremented value, false otherwise
  141. * @throws CacheException when you try to increment with compress = true
  142. */
  143. public function increment($key, $offset = 1) {
  144. if ($this->settings['compress']) {
  145. throw new CacheException(
  146. __d('cake_dev', 'Method increment() not implemented for compressed cache in %s', __CLASS__)
  147. );
  148. }
  149. return $this->_Memcache->increment($key, $offset);
  150. }
  151. /**
  152. * Decrements the value of an integer cached key
  153. *
  154. * @param string $key Identifier for the data
  155. * @param integer $offset How much to subtract
  156. * @return New decremented value, false otherwise
  157. * @throws CacheException when you try to decrement with compress = true
  158. */
  159. public function decrement($key, $offset = 1) {
  160. if ($this->settings['compress']) {
  161. throw new CacheException(
  162. __d('cake_dev', 'Method decrement() not implemented for compressed cache in %s', __CLASS__)
  163. );
  164. }
  165. return $this->_Memcache->decrement($key, $offset);
  166. }
  167. /**
  168. * Delete a key from the cache
  169. *
  170. * @param string $key Identifier for the data
  171. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  172. */
  173. public function delete($key) {
  174. return $this->_Memcache->delete($key);
  175. }
  176. /**
  177. * Delete all keys from the cache
  178. *
  179. * @return boolean True if the cache was successfully cleared, false otherwise
  180. */
  181. public function clear($check) {
  182. return $this->_Memcache->flush();
  183. }
  184. /**
  185. * Connects to a server in connection pool
  186. *
  187. * @param string $host host ip address or name
  188. * @param integer $port Server port
  189. * @return boolean True if memcache server was connected
  190. */
  191. public function connect($host, $port = 11211) {
  192. if ($this->_Memcache->getServerStatus($host, $port) === 0) {
  193. if ($this->_Memcache->connect($host, $port)) {
  194. return true;
  195. }
  196. return false;
  197. }
  198. return true;
  199. }
  200. }