MemcachedEngine.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.5.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Cache\Engine;
  16. use Cake\Cache\CacheEngine;
  17. use InvalidArgumentException;
  18. use Memcached;
  19. /**
  20. * Memcached storage engine for cache. Memcached has some limitations in the amount of
  21. * control you have over expire times far in the future. See MemcachedEngine::write() for
  22. * more information.
  23. *
  24. * Memcached engine supports binary protocol and igbinary
  25. * serialization (if memcached extension is compiled with --enable-igbinary).
  26. * Compressed keys can also be incremented/decremented.
  27. */
  28. class MemcachedEngine extends CacheEngine
  29. {
  30. /**
  31. * memcached wrapper.
  32. *
  33. * @var \Memcached
  34. */
  35. protected $_Memcached;
  36. /**
  37. * The default config used unless overridden by runtime configuration
  38. *
  39. * - `compress` Whether to compress data
  40. * - `duration` Specify how long items in this cache configuration last.
  41. * - `groups` List of groups or 'tags' associated to every key stored in this config.
  42. * handy for deleting a complete group from cache.
  43. * - `username` Login to access the Memcache server
  44. * - `password` Password to access the Memcache server
  45. * - `persistent` The name of the persistent connection. All configurations using
  46. * the same persistent value will share a single underlying connection.
  47. * - `prefix` Prepended to all entries. Good for when you need to share a keyspace
  48. * with either another cache config or another application.
  49. * - `probability` Probability of hitting a cache gc cleanup. Setting to 0 will disable
  50. * cache::gc from ever being called automatically.
  51. * - `serialize` The serializer engine used to serialize data. Available engines are php,
  52. * igbinary and json. Beside php, the memcached extension must be compiled with the
  53. * appropriate serializer support.
  54. * - `servers` String or array of memcached servers. If an array MemcacheEngine will use
  55. * them as a pool.
  56. * - `options` - Additional options for the memcached client. Should be an array of option => value.
  57. * Use the \Memcached::OPT_* constants as keys.
  58. *
  59. * @var array
  60. */
  61. protected $_defaultConfig = [
  62. 'compress' => false,
  63. 'duration' => 3600,
  64. 'groups' => [],
  65. 'host' => null,
  66. 'username' => null,
  67. 'password' => null,
  68. 'persistent' => false,
  69. 'port' => null,
  70. 'prefix' => 'cake_',
  71. 'probability' => 100,
  72. 'serialize' => 'php',
  73. 'servers' => ['127.0.0.1'],
  74. 'options' => [],
  75. ];
  76. /**
  77. * List of available serializer engines
  78. *
  79. * Memcached must be compiled with json and igbinary support to use these engines
  80. *
  81. * @var array
  82. */
  83. protected $_serializers = [];
  84. /**
  85. * @var string[]
  86. */
  87. protected $_compiledGroupNames = [];
  88. /**
  89. * Initialize the Cache Engine
  90. *
  91. * Called automatically by the cache frontend
  92. *
  93. * @param array $config array of setting for the engine
  94. * @return bool True if the engine has been successfully initialized, false if not
  95. * @throws \InvalidArgumentException When you try use authentication without
  96. * Memcached compiled with SASL support
  97. */
  98. public function init(array $config = [])
  99. {
  100. if (!extension_loaded('memcached')) {
  101. return false;
  102. }
  103. $this->_serializers = [
  104. 'igbinary' => Memcached::SERIALIZER_IGBINARY,
  105. 'json' => Memcached::SERIALIZER_JSON,
  106. 'php' => Memcached::SERIALIZER_PHP
  107. ];
  108. if (defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) {
  109. $this->_serializers['msgpack'] = Memcached::SERIALIZER_MSGPACK;
  110. }
  111. parent::init($config);
  112. if (!empty($config['host'])) {
  113. if (empty($config['port'])) {
  114. $config['servers'] = [$config['host']];
  115. } else {
  116. $config['servers'] = [sprintf('%s:%d', $config['host'], $config['port'])];
  117. }
  118. }
  119. if (isset($config['servers'])) {
  120. $this->setConfig('servers', $config['servers'], false);
  121. }
  122. if (!is_array($this->_config['servers'])) {
  123. $this->_config['servers'] = [$this->_config['servers']];
  124. }
  125. if (isset($this->_Memcached)) {
  126. return true;
  127. }
  128. if ($this->_config['persistent']) {
  129. $this->_Memcached = new Memcached((string)$this->_config['persistent']);
  130. } else {
  131. $this->_Memcached = new Memcached();
  132. }
  133. $this->_setOptions();
  134. if (count($this->_Memcached->getServerList())) {
  135. return true;
  136. }
  137. $servers = [];
  138. foreach ($this->_config['servers'] as $server) {
  139. $servers[] = $this->parseServerString($server);
  140. }
  141. if (!$this->_Memcached->addServers($servers)) {
  142. return false;
  143. }
  144. if (is_array($this->_config['options'])) {
  145. foreach ($this->_config['options'] as $opt => $value) {
  146. $this->_Memcached->setOption($opt, $value);
  147. }
  148. }
  149. if (empty($this->_config['username']) && !empty($this->_config['login'])) {
  150. throw new InvalidArgumentException(
  151. 'Please pass "username" instead of "login" for connecting to Memcached'
  152. );
  153. }
  154. if ($this->_config['username'] !== null && $this->_config['password'] !== null) {
  155. if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
  156. throw new InvalidArgumentException(
  157. 'Memcached extension is not built with SASL support'
  158. );
  159. }
  160. $this->_Memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
  161. $this->_Memcached->setSaslAuthData(
  162. $this->_config['username'],
  163. $this->_config['password']
  164. );
  165. }
  166. return true;
  167. }
  168. /**
  169. * Settings the memcached instance
  170. *
  171. * @return void
  172. * @throws \InvalidArgumentException When the Memcached extension is not built
  173. * with the desired serializer engine.
  174. */
  175. protected function _setOptions()
  176. {
  177. $this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
  178. $serializer = strtolower($this->_config['serialize']);
  179. if (!isset($this->_serializers[$serializer])) {
  180. throw new InvalidArgumentException(
  181. sprintf('%s is not a valid serializer engine for Memcached', $serializer)
  182. );
  183. }
  184. if ($serializer !== 'php' &&
  185. !constant('Memcached::HAVE_' . strtoupper($serializer))
  186. ) {
  187. throw new InvalidArgumentException(
  188. sprintf('Memcached extension is not compiled with %s support', $serializer)
  189. );
  190. }
  191. $this->_Memcached->setOption(
  192. Memcached::OPT_SERIALIZER,
  193. $this->_serializers[$serializer]
  194. );
  195. // Check for Amazon ElastiCache instance
  196. if (defined('Memcached::OPT_CLIENT_MODE') &&
  197. defined('Memcached::DYNAMIC_CLIENT_MODE')
  198. ) {
  199. $this->_Memcached->setOption(
  200. Memcached::OPT_CLIENT_MODE,
  201. Memcached::DYNAMIC_CLIENT_MODE
  202. );
  203. }
  204. $this->_Memcached->setOption(
  205. Memcached::OPT_COMPRESSION,
  206. (bool)$this->_config['compress']
  207. );
  208. }
  209. /**
  210. * Parses the server address into the host/port. Handles both IPv6 and IPv4
  211. * addresses and Unix sockets
  212. *
  213. * @param string $server The server address string.
  214. * @return array Array containing host, port
  215. */
  216. public function parseServerString($server)
  217. {
  218. $socketTransport = 'unix://';
  219. if (strpos($server, $socketTransport) === 0) {
  220. return [substr($server, strlen($socketTransport)), 0];
  221. }
  222. if (substr($server, 0, 1) === '[') {
  223. $position = strpos($server, ']:');
  224. if ($position !== false) {
  225. $position++;
  226. }
  227. } else {
  228. $position = strpos($server, ':');
  229. }
  230. $port = 11211;
  231. $host = $server;
  232. if ($position !== false) {
  233. $host = substr($server, 0, $position);
  234. $port = substr($server, $position + 1);
  235. }
  236. return [$host, (int)$port];
  237. }
  238. /**
  239. * Backwards compatible alias of parseServerString
  240. *
  241. * @param string $server The server address string.
  242. * @return array Array containing host, port
  243. * @deprecated 3.4.13 Will be removed in 4.0.0
  244. */
  245. protected function _parseServerString($server)
  246. {
  247. return $this->parseServerString($server);
  248. }
  249. /**
  250. * Read an option value from the memcached connection.
  251. *
  252. * @param string $name The option name to read.
  253. * @return string|int|null|bool
  254. */
  255. public function getOption($name)
  256. {
  257. return $this->_Memcached->getOption($name);
  258. }
  259. /**
  260. * Write data for key into cache. When using memcached as your cache engine
  261. * remember that the Memcached pecl extension does not support cache expiry
  262. * times greater than 30 days in the future. Any duration greater than 30 days
  263. * will be treated as never expiring.
  264. *
  265. * @param string $key Identifier for the data
  266. * @param mixed $value Data to be cached
  267. * @return bool True if the data was successfully cached, false on failure
  268. * @see https://secure.php.net/manual/en/memcache.set.php
  269. */
  270. public function write($key, $value)
  271. {
  272. $duration = $this->_config['duration'];
  273. if ($duration > 30 * DAY) {
  274. $duration = 0;
  275. }
  276. $key = $this->_key($key);
  277. return $this->_Memcached->set($key, $value, $duration);
  278. }
  279. /**
  280. * Write many cache entries to the cache at once
  281. *
  282. * @param array $data An array of data to be stored in the cache
  283. * @return array of bools for each key provided, true if the data was
  284. * successfully cached, false on failure
  285. */
  286. public function writeMany($data)
  287. {
  288. $cacheData = [];
  289. foreach ($data as $key => $value) {
  290. $cacheData[$this->_key($key)] = $value;
  291. }
  292. $success = $this->_Memcached->setMulti($cacheData);
  293. $return = [];
  294. foreach (array_keys($data) as $key) {
  295. $return[$key] = $success;
  296. }
  297. return $return;
  298. }
  299. /**
  300. * Read a key from the cache
  301. *
  302. * @param string $key Identifier for the data
  303. * @return mixed The cached data, or false if the data doesn't exist, has
  304. * expired, or if there was an error fetching it.
  305. */
  306. public function read($key)
  307. {
  308. $key = $this->_key($key);
  309. return $this->_Memcached->get($key);
  310. }
  311. /**
  312. * Read many keys from the cache at once
  313. *
  314. * @param array $keys An array of identifiers for the data
  315. * @return array An array containing, for each of the given $keys, the cached data or
  316. * false if cached data could not be retrieved.
  317. */
  318. public function readMany($keys)
  319. {
  320. $cacheKeys = [];
  321. foreach ($keys as $key) {
  322. $cacheKeys[] = $this->_key($key);
  323. }
  324. $values = $this->_Memcached->getMulti($cacheKeys);
  325. $return = [];
  326. foreach ($keys as &$key) {
  327. $return[$key] = array_key_exists($this->_key($key), $values) ?
  328. $values[$this->_key($key)] : false;
  329. }
  330. return $return;
  331. }
  332. /**
  333. * Increments the value of an integer cached key
  334. *
  335. * @param string $key Identifier for the data
  336. * @param int $offset How much to increment
  337. * @return bool|int New incremented value, false otherwise
  338. */
  339. public function increment($key, $offset = 1)
  340. {
  341. $key = $this->_key($key);
  342. return $this->_Memcached->increment($key, $offset);
  343. }
  344. /**
  345. * Decrements the value of an integer cached key
  346. *
  347. * @param string $key Identifier for the data
  348. * @param int $offset How much to subtract
  349. * @return bool|int New decremented value, false otherwise
  350. */
  351. public function decrement($key, $offset = 1)
  352. {
  353. $key = $this->_key($key);
  354. return $this->_Memcached->decrement($key, $offset);
  355. }
  356. /**
  357. * Delete a key from the cache
  358. *
  359. * @param string $key Identifier for the data
  360. * @return bool True if the value was successfully deleted, false if it didn't
  361. * exist or couldn't be removed.
  362. */
  363. public function delete($key)
  364. {
  365. $key = $this->_key($key);
  366. return $this->_Memcached->delete($key);
  367. }
  368. /**
  369. * Delete many keys from the cache at once
  370. *
  371. * @param array $keys An array of identifiers for the data
  372. * @return array of boolean values that are true if the key was successfully
  373. * deleted, false if it didn't exist or couldn't be removed.
  374. */
  375. public function deleteMany($keys)
  376. {
  377. $cacheKeys = [];
  378. foreach ($keys as $key) {
  379. $cacheKeys[] = $this->_key($key);
  380. }
  381. $success = $this->_Memcached->deleteMulti($cacheKeys);
  382. $return = [];
  383. foreach ($keys as $key) {
  384. $return[$key] = $success;
  385. }
  386. return $return;
  387. }
  388. /**
  389. * Delete all keys from the cache
  390. *
  391. * @param bool $check If true will check expiration, otherwise delete all.
  392. * @return bool True if the cache was successfully cleared, false otherwise
  393. */
  394. public function clear($check)
  395. {
  396. if ($check) {
  397. return true;
  398. }
  399. $keys = $this->_Memcached->getAllKeys();
  400. if ($keys === false) {
  401. return false;
  402. }
  403. foreach ($keys as $key) {
  404. if (strpos($key, $this->_config['prefix']) === 0) {
  405. $this->_Memcached->delete($key);
  406. }
  407. }
  408. return true;
  409. }
  410. /**
  411. * Add a key to the cache if it does not already exist.
  412. *
  413. * @param string $key Identifier for the data.
  414. * @param mixed $value Data to be cached.
  415. * @return bool True if the data was successfully cached, false on failure.
  416. */
  417. public function add($key, $value)
  418. {
  419. $duration = $this->_config['duration'];
  420. if ($duration > 30 * DAY) {
  421. $duration = 0;
  422. }
  423. $key = $this->_key($key);
  424. return $this->_Memcached->add($key, $value, $duration);
  425. }
  426. /**
  427. * Returns the `group value` for each of the configured groups
  428. * If the group initial value was not found, then it initializes
  429. * the group accordingly.
  430. *
  431. * @return array
  432. */
  433. public function groups()
  434. {
  435. if (empty($this->_compiledGroupNames)) {
  436. foreach ($this->_config['groups'] as $group) {
  437. $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
  438. }
  439. }
  440. $groups = $this->_Memcached->getMulti($this->_compiledGroupNames) ?: [];
  441. if (count($groups) !== count($this->_config['groups'])) {
  442. foreach ($this->_compiledGroupNames as $group) {
  443. if (!isset($groups[$group])) {
  444. $this->_Memcached->set($group, 1, 0);
  445. $groups[$group] = 1;
  446. }
  447. }
  448. ksort($groups);
  449. }
  450. $result = [];
  451. $groups = array_values($groups);
  452. foreach ($this->_config['groups'] as $i => $group) {
  453. $result[] = $group . $groups[$i];
  454. }
  455. return $result;
  456. }
  457. /**
  458. * Increments the group value to simulate deletion of all keys under a group
  459. * old values will remain in storage until they expire.
  460. *
  461. * @param string $group name of the group to be cleared
  462. * @return bool success
  463. */
  464. public function clearGroup($group)
  465. {
  466. return (bool)$this->_Memcached->increment($this->_config['prefix'] . $group);
  467. }
  468. }