PageRenderTime 136ms CodeModel.GetById 57ms RepoModel.GetById 2ms app.codeStats 0ms

/cake/lib/Cake/Cache/Engine/MemcacheEngine.php

https://gitlab.com/tixture55/cakeATM
PHP | 292 lines | 145 code | 20 blank | 127 comment | 23 complexity | 34dc0a56bdc2f1894c8f802a0c772934 MD5 | raw file
  1. <?php
  2. /**
  3. * Memcache storage engine for cache
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Cache.Engine
  15. * @since CakePHP(tm) v 1.2.0.4933
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. /**
  19. * Memcache storage engine for cache. Memcache has some limitations in the amount of
  20. * control you have over expire times far in the future. See MemcacheEngine::write() for
  21. * more information.
  22. *
  23. * @package Cake.Cache.Engine
  24. * @deprecated 3.0.0 You should use the Memcached adapter instead.
  25. */
  26. class MemcacheEngine extends CacheEngine {
  27. /**
  28. * Contains the compiled group names
  29. * (prefixed with the global configuration prefix)
  30. *
  31. * @var array
  32. */
  33. protected $_compiledGroupNames = array();
  34. /**
  35. * Memcache wrapper.
  36. *
  37. * @var Memcache
  38. */
  39. protected $_Memcache = null;
  40. /**
  41. * Settings
  42. *
  43. * - servers = string or array of memcache servers, default => 127.0.0.1. If an
  44. * array MemcacheEngine will use them as a pool.
  45. * - compress = boolean, default => false
  46. *
  47. * @var array
  48. */
  49. public $settings = array();
  50. /**
  51. * Initialize the Cache Engine
  52. *
  53. * Called automatically by the cache frontend
  54. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  55. *
  56. * @param array $settings array of setting for the engine
  57. * @return bool True if the engine has been successfully initialized, false if not
  58. */
  59. public function init($settings = array()) {
  60. if (!class_exists('Memcache')) {
  61. return false;
  62. }
  63. if (!isset($settings['prefix'])) {
  64. $settings['prefix'] = Inflector::slug(APP_DIR) . '_';
  65. }
  66. $settings += array(
  67. 'engine' => 'Memcache',
  68. 'servers' => array('127.0.0.1'),
  69. 'compress' => false,
  70. 'persistent' => true
  71. );
  72. parent::init($settings);
  73. if ($this->settings['compress']) {
  74. $this->settings['compress'] = MEMCACHE_COMPRESSED;
  75. }
  76. if (is_string($this->settings['servers'])) {
  77. $this->settings['servers'] = array($this->settings['servers']);
  78. }
  79. if (!isset($this->_Memcache)) {
  80. $return = false;
  81. $this->_Memcache = new Memcache();
  82. foreach ($this->settings['servers'] as $server) {
  83. list($host, $port) = $this->_parseServerString($server);
  84. if ($this->_Memcache->addServer($host, $port, $this->settings['persistent'])) {
  85. $return = true;
  86. }
  87. }
  88. return $return;
  89. }
  90. return true;
  91. }
  92. /**
  93. * Parses the server address into the host/port. Handles both IPv6 and IPv4
  94. * addresses and Unix sockets
  95. *
  96. * @param string $server The server address string.
  97. * @return array Array containing host, port
  98. */
  99. protected function _parseServerString($server) {
  100. if (strpos($server, 'unix://') === 0) {
  101. return array($server, 0);
  102. }
  103. if (substr($server, 0, 1) === '[') {
  104. $position = strpos($server, ']:');
  105. if ($position !== false) {
  106. $position++;
  107. }
  108. } else {
  109. $position = strpos($server, ':');
  110. }
  111. $port = 11211;
  112. $host = $server;
  113. if ($position !== false) {
  114. $host = substr($server, 0, $position);
  115. $port = substr($server, $position + 1);
  116. }
  117. return array($host, $port);
  118. }
  119. /**
  120. * Write data for key into cache. When using memcache as your cache engine
  121. * remember that the Memcache pecl extension does not support cache expiry times greater
  122. * than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
  123. *
  124. * @param string $key Identifier for the data
  125. * @param mixed $value Data to be cached
  126. * @param int $duration How long to cache the data, in seconds
  127. * @return bool True if the data was successfully cached, false on failure
  128. * @see http://php.net/manual/en/memcache.set.php
  129. */
  130. public function write($key, $value, $duration) {
  131. if ($duration > 30 * DAY) {
  132. $duration = 0;
  133. }
  134. return $this->_Memcache->set($key, $value, $this->settings['compress'], $duration);
  135. }
  136. /**
  137. * Read a key from the cache
  138. *
  139. * @param string $key Identifier for the data
  140. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  141. */
  142. public function read($key) {
  143. return $this->_Memcache->get($key);
  144. }
  145. /**
  146. * Increments the value of an integer cached key
  147. *
  148. * @param string $key Identifier for the data
  149. * @param int $offset How much to increment
  150. * @return New incremented value, false otherwise
  151. * @throws CacheException when you try to increment with compress = true
  152. */
  153. public function increment($key, $offset = 1) {
  154. if ($this->settings['compress']) {
  155. throw new CacheException(
  156. __d('cake_dev', 'Method %s not implemented for compressed cache in %s', 'increment()', __CLASS__)
  157. );
  158. }
  159. return $this->_Memcache->increment($key, $offset);
  160. }
  161. /**
  162. * Decrements the value of an integer cached key
  163. *
  164. * @param string $key Identifier for the data
  165. * @param int $offset How much to subtract
  166. * @return New decremented value, false otherwise
  167. * @throws CacheException when you try to decrement with compress = true
  168. */
  169. public function decrement($key, $offset = 1) {
  170. if ($this->settings['compress']) {
  171. throw new CacheException(
  172. __d('cake_dev', 'Method %s not implemented for compressed cache in %s', 'decrement()', __CLASS__)
  173. );
  174. }
  175. return $this->_Memcache->decrement($key, $offset);
  176. }
  177. /**
  178. * Delete a key from the cache
  179. *
  180. * @param string $key Identifier for the data
  181. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  182. */
  183. public function delete($key) {
  184. return $this->_Memcache->delete($key);
  185. }
  186. /**
  187. * Delete all keys from the cache
  188. *
  189. * @param bool $check If true no deletes will occur and instead CakePHP will rely
  190. * on key TTL values.
  191. * @return bool True if the cache was successfully cleared, false otherwise
  192. */
  193. public function clear($check) {
  194. if ($check) {
  195. return true;
  196. }
  197. foreach ($this->_Memcache->getExtendedStats('slabs', 0) as $slabs) {
  198. foreach (array_keys($slabs) as $slabId) {
  199. if (!is_numeric($slabId)) {
  200. continue;
  201. }
  202. foreach ($this->_Memcache->getExtendedStats('cachedump', $slabId, 0) as $stats) {
  203. if (!is_array($stats)) {
  204. continue;
  205. }
  206. foreach (array_keys($stats) as $key) {
  207. if (strpos($key, $this->settings['prefix']) === 0) {
  208. $this->_Memcache->delete($key);
  209. }
  210. }
  211. }
  212. }
  213. }
  214. return true;
  215. }
  216. /**
  217. * Connects to a server in connection pool
  218. *
  219. * @param string $host host ip address or name
  220. * @param int $port Server port
  221. * @return bool True if memcache server was connected
  222. */
  223. public function connect($host, $port = 11211) {
  224. if ($this->_Memcache->getServerStatus($host, $port) === 0) {
  225. if ($this->_Memcache->connect($host, $port)) {
  226. return true;
  227. }
  228. return false;
  229. }
  230. return true;
  231. }
  232. /**
  233. * Returns the `group value` for each of the configured groups
  234. * If the group initial value was not found, then it initializes
  235. * the group accordingly.
  236. *
  237. * @return array
  238. */
  239. public function groups() {
  240. if (empty($this->_compiledGroupNames)) {
  241. foreach ($this->settings['groups'] as $group) {
  242. $this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
  243. }
  244. }
  245. $groups = $this->_Memcache->get($this->_compiledGroupNames);
  246. if (count($groups) !== count($this->settings['groups'])) {
  247. foreach ($this->_compiledGroupNames as $group) {
  248. if (!isset($groups[$group])) {
  249. $this->_Memcache->set($group, 1, false, 0);
  250. $groups[$group] = 1;
  251. }
  252. }
  253. ksort($groups);
  254. }
  255. $result = array();
  256. $groups = array_values($groups);
  257. foreach ($this->settings['groups'] as $i => $group) {
  258. $result[] = $group . $groups[$i];
  259. }
  260. return $result;
  261. }
  262. /**
  263. * Increments the group value to simulate deletion of all keys under a group
  264. * old values will remain in storage until they expire.
  265. *
  266. * @param string $group The group to clear.
  267. * @return bool success
  268. */
  269. public function clearGroup($group) {
  270. return (bool)$this->_Memcache->increment($this->settings['prefix'] . $group);
  271. }
  272. }