MemcachedAdapter.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. use Symfony\Component\Cache\Exception\CacheException;
  12. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  13. use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
  14. use Symfony\Component\Cache\Marshaller\MarshallerInterface;
  15. /**
  16. * @author Rob Frawley 2nd <rmf@src.run>
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class MemcachedAdapter extends AbstractAdapter
  20. {
  21. /**
  22. * We are replacing characters that are illegal in Memcached keys with reserved characters from
  23. * {@see \Symfony\Contracts\Cache\ItemInterface::RESERVED_CHARACTERS} that are legal in Memcached.
  24. * Note: don’t use {@see \Symfony\Component\Cache\Adapter\AbstractAdapter::NS_SEPARATOR}.
  25. */
  26. private const RESERVED_MEMCACHED = " \n\r\t\v\f\0";
  27. private const RESERVED_PSR6 = '@()\{}/';
  28. protected $maxIdLength = 250;
  29. private const DEFAULT_CLIENT_OPTIONS = [
  30. 'persistent_id' => null,
  31. 'username' => null,
  32. 'password' => null,
  33. \Memcached::OPT_SERIALIZER => \Memcached::SERIALIZER_PHP,
  34. ];
  35. private $marshaller;
  36. private $client;
  37. private $lazyClient;
  38. /**
  39. * Using a MemcachedAdapter with a TagAwareAdapter for storing tags is discouraged.
  40. * Using a RedisAdapter is recommended instead. If you cannot do otherwise, be aware that:
  41. * - the Memcached::OPT_BINARY_PROTOCOL must be enabled
  42. * (that's the default when using MemcachedAdapter::createConnection());
  43. * - tags eviction by Memcached's LRU algorithm will break by-tags invalidation;
  44. * your Memcached memory should be large enough to never trigger LRU.
  45. *
  46. * Using a MemcachedAdapter as a pure items store is fine.
  47. */
  48. public function __construct(\Memcached $client, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
  49. {
  50. if (!static::isSupported()) {
  51. throw new CacheException('Memcached '.(\PHP_VERSION_ID >= 80100 ? '> 3.1.5' : '>= 2.2.0').' is required.');
  52. }
  53. if ('Memcached' === \get_class($client)) {
  54. $opt = $client->getOption(\Memcached::OPT_SERIALIZER);
  55. if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
  56. throw new CacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
  57. }
  58. $this->maxIdLength -= \strlen($client->getOption(\Memcached::OPT_PREFIX_KEY));
  59. $this->client = $client;
  60. } else {
  61. $this->lazyClient = $client;
  62. }
  63. parent::__construct($namespace, $defaultLifetime);
  64. $this->enableVersioning();
  65. $this->marshaller = $marshaller ?? new DefaultMarshaller();
  66. }
  67. public static function isSupported()
  68. {
  69. return \extension_loaded('memcached') && version_compare(phpversion('memcached'), \PHP_VERSION_ID >= 80100 ? '3.1.6' : '2.2.0', '>=');
  70. }
  71. /**
  72. * Creates a Memcached instance.
  73. *
  74. * By default, the binary protocol, no block, and libketama compatible options are enabled.
  75. *
  76. * Examples for servers:
  77. * - 'memcached://user:pass@localhost?weight=33'
  78. * - [['localhost', 11211, 33]]
  79. *
  80. * @param array[]|string|string[] $servers An array of servers, a DSN, or an array of DSNs
  81. *
  82. * @throws \ErrorException When invalid options or servers are provided
  83. */
  84. public static function createConnection(array|string $servers, array $options = []): \Memcached
  85. {
  86. if (\is_string($servers)) {
  87. $servers = [$servers];
  88. } elseif (!\is_array($servers)) {
  89. throw new InvalidArgumentException(sprintf('MemcachedAdapter::createClient() expects array or string as first argument, "%s" given.', get_debug_type($servers)));
  90. }
  91. if (!static::isSupported()) {
  92. throw new CacheException('Memcached '.(\PHP_VERSION_ID >= 80100 ? '> 3.1.5' : '>= 2.2.0').' is required.');
  93. }
  94. set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); });
  95. try {
  96. $options += static::DEFAULT_CLIENT_OPTIONS;
  97. $client = new \Memcached($options['persistent_id']);
  98. $username = $options['username'];
  99. $password = $options['password'];
  100. // parse any DSN in $servers
  101. foreach ($servers as $i => $dsn) {
  102. if (\is_array($dsn)) {
  103. continue;
  104. }
  105. if (!str_starts_with($dsn, 'memcached:')) {
  106. throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s" does not start with "memcached:".', $dsn));
  107. }
  108. $params = preg_replace_callback('#^memcached:(//)?(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
  109. if (!empty($m[2])) {
  110. [$username, $password] = explode(':', $m[2], 2) + [1 => null];
  111. }
  112. return 'file:'.($m[1] ?? '');
  113. }, $dsn);
  114. if (false === $params = parse_url($params)) {
  115. throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
  116. }
  117. $query = $hosts = [];
  118. if (isset($params['query'])) {
  119. parse_str($params['query'], $query);
  120. if (isset($query['host'])) {
  121. if (!\is_array($hosts = $query['host'])) {
  122. throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
  123. }
  124. foreach ($hosts as $host => $weight) {
  125. if (false === $port = strrpos($host, ':')) {
  126. $hosts[$host] = [$host, 11211, (int) $weight];
  127. } else {
  128. $hosts[$host] = [substr($host, 0, $port), (int) substr($host, 1 + $port), (int) $weight];
  129. }
  130. }
  131. $hosts = array_values($hosts);
  132. unset($query['host']);
  133. }
  134. if ($hosts && !isset($params['host']) && !isset($params['path'])) {
  135. unset($servers[$i]);
  136. $servers = array_merge($servers, $hosts);
  137. continue;
  138. }
  139. }
  140. if (!isset($params['host']) && !isset($params['path'])) {
  141. throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
  142. }
  143. if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
  144. $params['weight'] = $m[1];
  145. $params['path'] = substr($params['path'], 0, -\strlen($m[0]));
  146. }
  147. $params += [
  148. 'host' => $params['host'] ?? $params['path'],
  149. 'port' => isset($params['host']) ? 11211 : null,
  150. 'weight' => 0,
  151. ];
  152. if ($query) {
  153. $params += $query;
  154. $options = $query + $options;
  155. }
  156. $servers[$i] = [$params['host'], $params['port'], $params['weight']];
  157. if ($hosts) {
  158. $servers = array_merge($servers, $hosts);
  159. }
  160. }
  161. // set client's options
  162. unset($options['persistent_id'], $options['username'], $options['password'], $options['weight'], $options['lazy']);
  163. $options = array_change_key_case($options, \CASE_UPPER);
  164. $client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
  165. $client->setOption(\Memcached::OPT_NO_BLOCK, true);
  166. $client->setOption(\Memcached::OPT_TCP_NODELAY, true);
  167. if (!\array_key_exists('LIBKETAMA_COMPATIBLE', $options) && !\array_key_exists(\Memcached::OPT_LIBKETAMA_COMPATIBLE, $options)) {
  168. $client->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
  169. }
  170. foreach ($options as $name => $value) {
  171. if (\is_int($name)) {
  172. continue;
  173. }
  174. if ('HASH' === $name || 'SERIALIZER' === $name || 'DISTRIBUTION' === $name) {
  175. $value = \constant('Memcached::'.$name.'_'.strtoupper($value));
  176. }
  177. unset($options[$name]);
  178. if (\defined('Memcached::OPT_'.$name)) {
  179. $options[\constant('Memcached::OPT_'.$name)] = $value;
  180. }
  181. }
  182. $client->setOptions($options);
  183. // set client's servers, taking care of persistent connections
  184. if (!$client->isPristine()) {
  185. $oldServers = [];
  186. foreach ($client->getServerList() as $server) {
  187. $oldServers[] = [$server['host'], $server['port']];
  188. }
  189. $newServers = [];
  190. foreach ($servers as $server) {
  191. if (1 < \count($server)) {
  192. $server = array_values($server);
  193. unset($server[2]);
  194. $server[1] = (int) $server[1];
  195. }
  196. $newServers[] = $server;
  197. }
  198. if ($oldServers !== $newServers) {
  199. $client->resetServerList();
  200. $client->addServers($servers);
  201. }
  202. } else {
  203. $client->addServers($servers);
  204. }
  205. if (null !== $username || null !== $password) {
  206. if (!method_exists($client, 'setSaslAuthData')) {
  207. trigger_error('Missing SASL support: the memcached extension must be compiled with --enable-memcached-sasl.');
  208. }
  209. $client->setSaslAuthData($username, $password);
  210. }
  211. return $client;
  212. } finally {
  213. restore_error_handler();
  214. }
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. protected function doSave(array $values, int $lifetime): array|bool
  220. {
  221. if (!$values = $this->marshaller->marshall($values, $failed)) {
  222. return $failed;
  223. }
  224. if ($lifetime && $lifetime > 30 * 86400) {
  225. $lifetime += time();
  226. }
  227. $encodedValues = [];
  228. foreach ($values as $key => $value) {
  229. $encodedValues[self::encodeKey($key)] = $value;
  230. }
  231. return $this->checkResultCode($this->getClient()->setMulti($encodedValues, $lifetime)) ? $failed : false;
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. protected function doFetch(array $ids): iterable
  237. {
  238. try {
  239. $encodedIds = array_map([__CLASS__, 'encodeKey'], $ids);
  240. $encodedResult = $this->checkResultCode($this->getClient()->getMulti($encodedIds));
  241. $result = [];
  242. foreach ($encodedResult as $key => $value) {
  243. $result[self::decodeKey($key)] = $this->marshaller->unmarshall($value);
  244. }
  245. return $result;
  246. } catch (\Error $e) {
  247. throw new \ErrorException($e->getMessage(), $e->getCode(), \E_ERROR, $e->getFile(), $e->getLine());
  248. }
  249. }
  250. /**
  251. * {@inheritdoc}
  252. */
  253. protected function doHave(string $id): bool
  254. {
  255. return false !== $this->getClient()->get(self::encodeKey($id)) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
  256. }
  257. /**
  258. * {@inheritdoc}
  259. */
  260. protected function doDelete(array $ids): bool
  261. {
  262. $ok = true;
  263. $encodedIds = array_map([__CLASS__, 'encodeKey'], $ids);
  264. foreach ($this->checkResultCode($this->getClient()->deleteMulti($encodedIds)) as $result) {
  265. if (\Memcached::RES_SUCCESS !== $result && \Memcached::RES_NOTFOUND !== $result) {
  266. $ok = false;
  267. }
  268. }
  269. return $ok;
  270. }
  271. /**
  272. * {@inheritdoc}
  273. */
  274. protected function doClear(string $namespace): bool
  275. {
  276. return '' === $namespace && $this->getClient()->flush();
  277. }
  278. private function checkResultCode(mixed $result)
  279. {
  280. $code = $this->client->getResultCode();
  281. if (\Memcached::RES_SUCCESS === $code || \Memcached::RES_NOTFOUND === $code) {
  282. return $result;
  283. }
  284. throw new CacheException('MemcachedAdapter client error: '.strtolower($this->client->getResultMessage()));
  285. }
  286. private function getClient(): \Memcached
  287. {
  288. if (isset($this->client)) {
  289. return $this->client;
  290. }
  291. $opt = $this->lazyClient->getOption(\Memcached::OPT_SERIALIZER);
  292. if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
  293. throw new CacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
  294. }
  295. if ('' !== $prefix = (string) $this->lazyClient->getOption(\Memcached::OPT_PREFIX_KEY)) {
  296. throw new CacheException(sprintf('MemcachedAdapter: "prefix_key" option must be empty when using proxified connections, "%s" given.', $prefix));
  297. }
  298. return $this->client = $this->lazyClient;
  299. }
  300. private static function encodeKey(string $key): string
  301. {
  302. return strtr($key, self::RESERVED_MEMCACHED, self::RESERVED_PSR6);
  303. }
  304. private static function decodeKey(string $key): string
  305. {
  306. return strtr($key, self::RESERVED_PSR6, self::RESERVED_MEMCACHED);
  307. }
  308. }