MemcachedEngine.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 (
  185. $serializer !== 'php' &&
  186. !constant('Memcached::HAVE_' . strtoupper($serializer))
  187. ) {
  188. throw new InvalidArgumentException(
  189. sprintf('Memcached extension is not compiled with %s support', $serializer)
  190. );
  191. }
  192. $this->_Memcached->setOption(
  193. Memcached::OPT_SERIALIZER,
  194. $this->_serializers[$serializer]
  195. );
  196. // Check for Amazon ElastiCache instance
  197. if (
  198. defined('Memcached::OPT_CLIENT_MODE') &&
  199. defined('Memcached::DYNAMIC_CLIENT_MODE')
  200. ) {
  201. $this->_Memcached->setOption(
  202. Memcached::OPT_CLIENT_MODE,
  203. Memcached::DYNAMIC_CLIENT_MODE
  204. );
  205. }
  206. $this->_Memcached->setOption(
  207. Memcached::OPT_COMPRESSION,
  208. (bool)$this->_config['compress']
  209. );
  210. }
  211. /**
  212. * Parses the server address into the host/port. Handles both IPv6 and IPv4
  213. * addresses and Unix sockets
  214. *
  215. * @param string $server The server address string.
  216. * @return array Array containing host, port
  217. */
  218. public function parseServerString($server)
  219. {
  220. $socketTransport = 'unix://';
  221. if (strpos($server, $socketTransport) === 0) {
  222. return [substr($server, strlen($socketTransport)), 0];
  223. }
  224. if (substr($server, 0, 1) === '[') {
  225. $position = strpos($server, ']:');
  226. if ($position !== false) {
  227. $position++;
  228. }
  229. } else {
  230. $position = strpos($server, ':');
  231. }
  232. $port = 11211;
  233. $host = $server;
  234. if ($position !== false) {
  235. $host = substr($server, 0, $position);
  236. $port = substr($server, $position + 1);
  237. }
  238. return [$host, (int)$port];
  239. }
  240. /**
  241. * Backwards compatible alias of parseServerString
  242. *
  243. * @param string $server The server address string.
  244. * @return array Array containing host, port
  245. * @deprecated 3.4.13 Will be removed in 4.0.0
  246. */
  247. protected function _parseServerString($server)
  248. {
  249. return $this->parseServerString($server);
  250. }
  251. /**
  252. * Read an option value from the memcached connection.
  253. *
  254. * @param string $name The option name to read.
  255. * @return string|int|bool|null
  256. */
  257. public function getOption($name)
  258. {
  259. return $this->_Memcached->getOption($name);
  260. }
  261. /**
  262. * Write data for key into cache. When using memcached as your cache engine
  263. * remember that the Memcached pecl extension does not support cache expiry
  264. * times greater than 30 days in the future. Any duration greater than 30 days
  265. * will be treated as real Unix time value rather than an offset from current time.
  266. *
  267. * @param string $key Identifier for the data
  268. * @param mixed $value Data to be cached
  269. * @return bool True if the data was successfully cached, false on failure
  270. * @see https://www.php.net/manual/en/memcached.set.php
  271. */
  272. public function write($key, $value)
  273. {
  274. $duration = $this->_config['duration'];
  275. $key = $this->_key($key);
  276. return $this->_Memcached->set($key, $value, $duration);
  277. }
  278. /**
  279. * Write many cache entries to the cache at once
  280. *
  281. * @param array $data An array of data to be stored in the cache
  282. * @return array of bools for each key provided, true if the data was
  283. * successfully cached, false on failure
  284. */
  285. public function writeMany($data)
  286. {
  287. $cacheData = [];
  288. foreach ($data as $key => $value) {
  289. $cacheData[$this->_key($key)] = $value;
  290. }
  291. $success = $this->_Memcached->setMulti($cacheData);
  292. $return = [];
  293. foreach (array_keys($data) as $key) {
  294. $return[$key] = $success;
  295. }
  296. return $return;
  297. }
  298. /**
  299. * Read a key from the cache
  300. *
  301. * @param string $key Identifier for the data
  302. * @return mixed The cached data, or false if the data doesn't exist, has
  303. * expired, or if there was an error fetching it.
  304. */
  305. public function read($key)
  306. {
  307. $key = $this->_key($key);
  308. return $this->_Memcached->get($key);
  309. }
  310. /**
  311. * Read many keys from the cache at once
  312. *
  313. * @param array $keys An array of identifiers for the data
  314. * @return array An array containing, for each of the given $keys, the cached data or
  315. * false if cached data could not be retrieved.
  316. */
  317. public function readMany($keys)
  318. {
  319. $cacheKeys = [];
  320. foreach ($keys as $key) {
  321. $cacheKeys[] = $this->_key($key);
  322. }
  323. $values = $this->_Memcached->getMulti($cacheKeys);
  324. $return = [];
  325. foreach ($keys as &$key) {
  326. $return[$key] = array_key_exists($this->_key($key), $values) ?
  327. $values[$this->_key($key)] : false;
  328. }
  329. return $return;
  330. }
  331. /**
  332. * Increments the value of an integer cached key
  333. *
  334. * @param string $key Identifier for the data
  335. * @param int $offset How much to increment
  336. * @return int|false New incremented value, false otherwise
  337. */
  338. public function increment($key, $offset = 1)
  339. {
  340. $key = $this->_key($key);
  341. return $this->_Memcached->increment($key, $offset);
  342. }
  343. /**
  344. * Decrements the value of an integer cached key
  345. *
  346. * @param string $key Identifier for the data
  347. * @param int $offset How much to subtract
  348. * @return int|false New decremented value, false otherwise
  349. */
  350. public function decrement($key, $offset = 1)
  351. {
  352. $key = $this->_key($key);
  353. return $this->_Memcached->decrement($key, $offset);
  354. }
  355. /**
  356. * Delete a key from the cache
  357. *
  358. * @param string $key Identifier for the data
  359. * @return bool True if the value was successfully deleted, false if it didn't
  360. * exist or couldn't be removed.
  361. */
  362. public function delete($key)
  363. {
  364. $key = $this->_key($key);
  365. return $this->_Memcached->delete($key);
  366. }
  367. /**
  368. * Delete many keys from the cache at once
  369. *
  370. * @param array $keys An array of identifiers for the data
  371. * @return array of boolean values that are true if the key was successfully
  372. * deleted, false if it didn't exist or couldn't be removed.
  373. */
  374. public function deleteMany($keys)
  375. {
  376. $cacheKeys = [];
  377. foreach ($keys as $key) {
  378. $cacheKeys[] = $this->_key($key);
  379. }
  380. $success = $this->_Memcached->deleteMulti($cacheKeys);
  381. $return = [];
  382. foreach ($keys as $key) {
  383. $return[$key] = $success;
  384. }
  385. return $return;
  386. }
  387. /**
  388. * Delete all keys from the cache
  389. *
  390. * @param bool $check If true will check expiration, otherwise delete all.
  391. * @return bool True if the cache was successfully cleared, false otherwise
  392. */
  393. public function clear($check)
  394. {
  395. if ($check) {
  396. return true;
  397. }
  398. $keys = $this->_Memcached->getAllKeys();
  399. if ($keys === false) {
  400. return false;
  401. }
  402. foreach ($keys as $key) {
  403. if (strpos($key, $this->_config['prefix']) === 0) {
  404. $this->_Memcached->delete($key);
  405. }
  406. }
  407. return true;
  408. }
  409. /**
  410. * Add a key to the cache if it does not already exist.
  411. *
  412. * @param string $key Identifier for the data.
  413. * @param mixed $value Data to be cached.
  414. * @return bool True if the data was successfully cached, false on failure.
  415. */
  416. public function add($key, $value)
  417. {
  418. $duration = $this->_config['duration'];
  419. $key = $this->_key($key);
  420. return $this->_Memcached->add($key, $value, $duration);
  421. }
  422. /**
  423. * Returns the `group value` for each of the configured groups
  424. * If the group initial value was not found, then it initializes
  425. * the group accordingly.
  426. *
  427. * @return string[]
  428. */
  429. public function groups()
  430. {
  431. if (empty($this->_compiledGroupNames)) {
  432. foreach ($this->_config['groups'] as $group) {
  433. $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
  434. }
  435. }
  436. $groups = $this->_Memcached->getMulti($this->_compiledGroupNames) ?: [];
  437. if (count($groups) !== count($this->_config['groups'])) {
  438. foreach ($this->_compiledGroupNames as $group) {
  439. if (!isset($groups[$group])) {
  440. $this->_Memcached->set($group, 1, 0);
  441. $groups[$group] = 1;
  442. }
  443. }
  444. ksort($groups);
  445. }
  446. $result = [];
  447. $groups = array_values($groups);
  448. foreach ($this->_config['groups'] as $i => $group) {
  449. $result[] = $group . $groups[$i];
  450. }
  451. return $result;
  452. }
  453. /**
  454. * Increments the group value to simulate deletion of all keys under a group
  455. * old values will remain in storage until they expire.
  456. *
  457. * @param string $group name of the group to be cleared
  458. * @return bool success
  459. */
  460. public function clearGroup($group)
  461. {
  462. return (bool)$this->_Memcached->increment($this->_config['prefix'] . $group);
  463. }
  464. }