RedisTrait.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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\Traits;
  11. use Predis\Connection\Aggregate\ClusterInterface;
  12. use Predis\Connection\Aggregate\RedisCluster;
  13. use Predis\Response\Status;
  14. use Symfony\Component\Cache\Exception\CacheException;
  15. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  16. use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
  17. use Symfony\Component\Cache\Marshaller\MarshallerInterface;
  18. /**
  19. * @author Aurimas Niekis <aurimas@niekis.lt>
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. *
  22. * @internal
  23. */
  24. trait RedisTrait
  25. {
  26. private static $defaultConnectionOptions = [
  27. 'class' => null,
  28. 'persistent' => 0,
  29. 'persistent_id' => null,
  30. 'timeout' => 30,
  31. 'read_timeout' => 0,
  32. 'retry_interval' => 0,
  33. 'tcp_keepalive' => 0,
  34. 'lazy' => null,
  35. 'redis_cluster' => false,
  36. 'redis_sentinel' => null,
  37. 'dbindex' => 0,
  38. 'failover' => 'none',
  39. ];
  40. private $redis;
  41. private $marshaller;
  42. /**
  43. * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient
  44. */
  45. private function init($redisClient, string $namespace, int $defaultLifetime, ?MarshallerInterface $marshaller)
  46. {
  47. parent::__construct($namespace, $defaultLifetime);
  48. if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
  49. throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
  50. }
  51. if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\ClientInterface && !$redisClient instanceof RedisProxy && !$redisClient instanceof RedisClusterProxy) {
  52. throw new InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, \is_object($redisClient) ? \get_class($redisClient) : \gettype($redisClient)));
  53. }
  54. if ($redisClient instanceof \Predis\ClientInterface && $redisClient->getOptions()->exceptions) {
  55. $options = clone $redisClient->getOptions();
  56. \Closure::bind(function () { $this->options['exceptions'] = false; }, $options, $options)();
  57. $redisClient = new $redisClient($redisClient->getConnection(), $options);
  58. }
  59. $this->redis = $redisClient;
  60. $this->marshaller = $marshaller ?? new DefaultMarshaller();
  61. }
  62. /**
  63. * Creates a Redis connection using a DSN configuration.
  64. *
  65. * Example DSN:
  66. * - redis://localhost
  67. * - redis://example.com:1234
  68. * - redis://secret@example.com/13
  69. * - redis:///var/run/redis.sock
  70. * - redis://secret@/var/run/redis.sock/13
  71. *
  72. * @param string $dsn
  73. * @param array $options See self::$defaultConnectionOptions
  74. *
  75. * @throws InvalidArgumentException when the DSN is invalid
  76. *
  77. * @return \Redis|\RedisCluster|\Predis\ClientInterface According to the "class" option
  78. */
  79. public static function createConnection($dsn, array $options = [])
  80. {
  81. if (0 === strpos($dsn, 'redis:')) {
  82. $scheme = 'redis';
  83. } elseif (0 === strpos($dsn, 'rediss:')) {
  84. $scheme = 'rediss';
  85. } else {
  86. throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s" does not start with "redis:" or "rediss".', $dsn));
  87. }
  88. if (!\extension_loaded('redis') && !class_exists(\Predis\Client::class)) {
  89. throw new CacheException(sprintf('Cannot find the "redis" extension nor the "predis/predis" package: "%s".', $dsn));
  90. }
  91. $params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) {
  92. if (isset($m[2])) {
  93. $auth = $m[2];
  94. }
  95. return 'file:'.($m[1] ?? '');
  96. }, $dsn);
  97. if (false === $params = parse_url($params)) {
  98. throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
  99. }
  100. $query = $hosts = [];
  101. if (isset($params['query'])) {
  102. parse_str($params['query'], $query);
  103. if (isset($query['host'])) {
  104. if (!\is_array($hosts = $query['host'])) {
  105. throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
  106. }
  107. foreach ($hosts as $host => $parameters) {
  108. if (\is_string($parameters)) {
  109. parse_str($parameters, $parameters);
  110. }
  111. if (false === $i = strrpos($host, ':')) {
  112. $hosts[$host] = ['scheme' => 'tcp', 'host' => $host, 'port' => 6379] + $parameters;
  113. } elseif ($port = (int) substr($host, 1 + $i)) {
  114. $hosts[$host] = ['scheme' => 'tcp', 'host' => substr($host, 0, $i), 'port' => $port] + $parameters;
  115. } else {
  116. $hosts[$host] = ['scheme' => 'unix', 'path' => substr($host, 0, $i)] + $parameters;
  117. }
  118. }
  119. $hosts = array_values($hosts);
  120. }
  121. }
  122. if (isset($params['host']) || isset($params['path'])) {
  123. if (!isset($params['dbindex']) && isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
  124. $params['dbindex'] = $m[1];
  125. $params['path'] = substr($params['path'], 0, -\strlen($m[0]));
  126. }
  127. if (isset($params['host'])) {
  128. array_unshift($hosts, ['scheme' => 'tcp', 'host' => $params['host'], 'port' => $params['port'] ?? 6379]);
  129. } else {
  130. array_unshift($hosts, ['scheme' => 'unix', 'path' => $params['path']]);
  131. }
  132. }
  133. if (!$hosts) {
  134. throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
  135. }
  136. if (isset($params['redis_sentinel']) && !class_exists(\Predis\Client::class)) {
  137. throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package: "%s".', $dsn));
  138. }
  139. $params += $query + $options + self::$defaultConnectionOptions;
  140. if (null === $params['class'] && !isset($params['redis_sentinel']) && \extension_loaded('redis')) {
  141. $class = $params['redis_cluster'] ? \RedisCluster::class : (1 < \count($hosts) ? \RedisArray::class : \Redis::class);
  142. } else {
  143. $class = null === $params['class'] ? \Predis\Client::class : $params['class'];
  144. }
  145. if (is_a($class, \Redis::class, true)) {
  146. $connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
  147. $redis = new $class();
  148. $initializer = function ($redis) use ($connect, $params, $dsn, $auth, $hosts) {
  149. try {
  150. @$redis->{$connect}($hosts[0]['host'] ?? $hosts[0]['path'], $hosts[0]['port'] ?? null, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval']);
  151. } catch (\RedisException $e) {
  152. throw new InvalidArgumentException(sprintf('Redis connection failed (%s): "%s".', $e->getMessage(), $dsn));
  153. }
  154. set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
  155. $isConnected = $redis->isConnected();
  156. restore_error_handler();
  157. if (!$isConnected) {
  158. $error = preg_match('/^Redis::p?connect\(\): (.*)/', $error, $error) ? sprintf(' (%s)', $error[1]) : '';
  159. throw new InvalidArgumentException(sprintf('Redis connection failed%s: "%s".', $error, $dsn));
  160. }
  161. if ((null !== $auth && !$redis->auth($auth))
  162. || ($params['dbindex'] && !$redis->select($params['dbindex']))
  163. || ($params['read_timeout'] && !$redis->setOption(\Redis::OPT_READ_TIMEOUT, $params['read_timeout']))
  164. ) {
  165. $e = preg_replace('/^ERR /', '', $redis->getLastError());
  166. throw new InvalidArgumentException(sprintf('Redis connection failed (%s): "%s".', $e, $dsn));
  167. }
  168. if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
  169. $redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
  170. }
  171. return true;
  172. };
  173. if ($params['lazy']) {
  174. $redis = new RedisProxy($redis, $initializer);
  175. } else {
  176. $initializer($redis);
  177. }
  178. } elseif (is_a($class, \RedisArray::class, true)) {
  179. foreach ($hosts as $i => $host) {
  180. $hosts[$i] = 'tcp' === $host['scheme'] ? $host['host'].':'.$host['port'] : $host['path'];
  181. }
  182. $params['lazy_connect'] = $params['lazy'] ?? true;
  183. $params['connect_timeout'] = $params['timeout'];
  184. try {
  185. $redis = new $class($hosts, $params);
  186. } catch (\RedisClusterException $e) {
  187. throw new InvalidArgumentException(sprintf('Redis connection failed (%s): "%s".', $e->getMessage(), $dsn));
  188. }
  189. if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
  190. $redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
  191. }
  192. } elseif (is_a($class, \RedisCluster::class, true)) {
  193. $initializer = function () use ($class, $params, $dsn, $hosts) {
  194. foreach ($hosts as $i => $host) {
  195. $hosts[$i] = 'tcp' === $host['scheme'] ? $host['host'].':'.$host['port'] : $host['path'];
  196. }
  197. try {
  198. $redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent']);
  199. } catch (\RedisClusterException $e) {
  200. throw new InvalidArgumentException(sprintf('Redis connection failed (%s): "%s".', $e->getMessage(), $dsn));
  201. }
  202. if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
  203. $redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
  204. }
  205. switch ($params['failover']) {
  206. case 'error': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_ERROR); break;
  207. case 'distribute': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE); break;
  208. case 'slaves': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE_SLAVES); break;
  209. }
  210. return $redis;
  211. };
  212. $redis = $params['lazy'] ? new RedisClusterProxy($initializer) : $initializer();
  213. } elseif (is_a($class, \Predis\ClientInterface::class, true)) {
  214. if ($params['redis_cluster']) {
  215. $params['cluster'] = 'redis';
  216. if (isset($params['redis_sentinel'])) {
  217. throw new InvalidArgumentException(sprintf('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: "%s".', $dsn));
  218. }
  219. } elseif (isset($params['redis_sentinel'])) {
  220. $params['replication'] = 'sentinel';
  221. $params['service'] = $params['redis_sentinel'];
  222. }
  223. $params += ['parameters' => []];
  224. $params['parameters'] += [
  225. 'persistent' => $params['persistent'],
  226. 'timeout' => $params['timeout'],
  227. 'read_write_timeout' => $params['read_timeout'],
  228. 'tcp_nodelay' => true,
  229. ];
  230. if ($params['dbindex']) {
  231. $params['parameters']['database'] = $params['dbindex'];
  232. }
  233. if (null !== $auth) {
  234. $params['parameters']['password'] = $auth;
  235. }
  236. if (1 === \count($hosts) && !($params['redis_cluster'] || $params['redis_sentinel'])) {
  237. $hosts = $hosts[0];
  238. } elseif (\in_array($params['failover'], ['slaves', 'distribute'], true) && !isset($params['replication'])) {
  239. $params['replication'] = true;
  240. $hosts[0] += ['alias' => 'master'];
  241. }
  242. $params['exceptions'] = false;
  243. $redis = new $class($hosts, array_diff_key($params, self::$defaultConnectionOptions));
  244. if (isset($params['redis_sentinel'])) {
  245. $redis->getConnection()->setSentinelTimeout($params['timeout']);
  246. }
  247. } elseif (class_exists($class, false)) {
  248. throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis", "RedisArray", "RedisCluster" nor "Predis\ClientInterface".', $class));
  249. } else {
  250. throw new InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  251. }
  252. return $redis;
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. protected function doFetch(array $ids)
  258. {
  259. if (!$ids) {
  260. return [];
  261. }
  262. $result = [];
  263. if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) {
  264. $values = $this->pipeline(function () use ($ids) {
  265. foreach ($ids as $id) {
  266. yield 'get' => [$id];
  267. }
  268. });
  269. } else {
  270. $values = $this->redis->mget($ids);
  271. if (!\is_array($values) || \count($values) !== \count($ids)) {
  272. return [];
  273. }
  274. $values = array_combine($ids, $values);
  275. }
  276. foreach ($values as $id => $v) {
  277. if ($v) {
  278. $result[$id] = $this->marshaller->unmarshall($v);
  279. }
  280. }
  281. return $result;
  282. }
  283. /**
  284. * {@inheritdoc}
  285. */
  286. protected function doHave($id)
  287. {
  288. return (bool) $this->redis->exists($id);
  289. }
  290. /**
  291. * {@inheritdoc}
  292. */
  293. protected function doClear($namespace)
  294. {
  295. $cleared = true;
  296. if ($this->redis instanceof \Predis\ClientInterface) {
  297. $evalArgs = [0, $namespace];
  298. } else {
  299. $evalArgs = [[$namespace], 0];
  300. }
  301. foreach ($this->getHosts() as $host) {
  302. if (!isset($namespace[0])) {
  303. $cleared = $host->flushDb() && $cleared;
  304. continue;
  305. }
  306. $info = $host->info('Server');
  307. $info = isset($info['Server']) ? $info['Server'] : $info;
  308. if (!version_compare($info['redis_version'], '2.8', '>=')) {
  309. // As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
  310. // can hang your server when it is executed against large databases (millions of items).
  311. // Whenever you hit this scale, you should really consider upgrading to Redis 2.8 or above.
  312. $cleared = $host->eval("local keys=redis.call('KEYS',ARGV[1]..'*') for i=1,#keys,5000 do redis.call('DEL',unpack(keys,i,math.min(i+4999,#keys))) end return 1", $evalArgs[0], $evalArgs[1]) && $cleared;
  313. continue;
  314. }
  315. $cursor = null;
  316. do {
  317. $keys = $host instanceof \Predis\ClientInterface ? $host->scan($cursor, 'MATCH', $namespace.'*', 'COUNT', 1000) : $host->scan($cursor, $namespace.'*', 1000);
  318. if (isset($keys[1]) && \is_array($keys[1])) {
  319. $cursor = $keys[0];
  320. $keys = $keys[1];
  321. }
  322. if ($keys) {
  323. $this->doDelete($keys);
  324. }
  325. } while ($cursor = (int) $cursor);
  326. }
  327. return $cleared;
  328. }
  329. /**
  330. * {@inheritdoc}
  331. */
  332. protected function doDelete(array $ids)
  333. {
  334. if (!$ids) {
  335. return true;
  336. }
  337. if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) {
  338. $this->pipeline(function () use ($ids) {
  339. foreach ($ids as $id) {
  340. yield 'del' => [$id];
  341. }
  342. })->rewind();
  343. } else {
  344. $this->redis->del($ids);
  345. }
  346. return true;
  347. }
  348. /**
  349. * {@inheritdoc}
  350. */
  351. protected function doSave(array $values, $lifetime)
  352. {
  353. if (!$values = $this->marshaller->marshall($values, $failed)) {
  354. return $failed;
  355. }
  356. $results = $this->pipeline(function () use ($values, $lifetime) {
  357. foreach ($values as $id => $value) {
  358. if (0 >= $lifetime) {
  359. yield 'set' => [$id, $value];
  360. } else {
  361. yield 'setEx' => [$id, $lifetime, $value];
  362. }
  363. }
  364. });
  365. foreach ($results as $id => $result) {
  366. if (true !== $result && (!$result instanceof Status || Status::get('OK') !== $result)) {
  367. $failed[] = $id;
  368. }
  369. }
  370. return $failed;
  371. }
  372. private function pipeline(\Closure $generator, $redis = null): \Generator
  373. {
  374. $ids = [];
  375. $redis = $redis ?? $this->redis;
  376. if ($redis instanceof RedisClusterProxy || $redis instanceof \RedisCluster || ($redis instanceof \Predis\ClientInterface && $redis->getConnection() instanceof RedisCluster)) {
  377. // phpredis & predis don't support pipelining with RedisCluster
  378. // see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#pipelining
  379. // see https://github.com/nrk/predis/issues/267#issuecomment-123781423
  380. $results = [];
  381. foreach ($generator() as $command => $args) {
  382. $results[] = $redis->{$command}(...$args);
  383. $ids[] = 'eval' === $command ? ($redis instanceof \Predis\ClientInterface ? $args[2] : $args[1][0]) : $args[0];
  384. }
  385. } elseif ($redis instanceof \Predis\ClientInterface) {
  386. $results = $redis->pipeline(static function ($redis) use ($generator, &$ids) {
  387. foreach ($generator() as $command => $args) {
  388. $redis->{$command}(...$args);
  389. $ids[] = 'eval' === $command ? $args[2] : $args[0];
  390. }
  391. });
  392. } elseif ($redis instanceof \RedisArray) {
  393. $connections = $results = $ids = [];
  394. foreach ($generator() as $command => $args) {
  395. $id = 'eval' === $command ? $args[1][0] : $args[0];
  396. if (!isset($connections[$h = $redis->_target($id)])) {
  397. $connections[$h] = [$redis->_instance($h), -1];
  398. $connections[$h][0]->multi(\Redis::PIPELINE);
  399. }
  400. $connections[$h][0]->{$command}(...$args);
  401. $results[] = [$h, ++$connections[$h][1]];
  402. $ids[] = $id;
  403. }
  404. foreach ($connections as $h => $c) {
  405. $connections[$h] = $c[0]->exec();
  406. }
  407. foreach ($results as $k => list($h, $c)) {
  408. $results[$k] = $connections[$h][$c];
  409. }
  410. } else {
  411. $redis->multi(\Redis::PIPELINE);
  412. foreach ($generator() as $command => $args) {
  413. $redis->{$command}(...$args);
  414. $ids[] = 'eval' === $command ? $args[1][0] : $args[0];
  415. }
  416. $results = $redis->exec();
  417. }
  418. foreach ($ids as $k => $id) {
  419. yield $id => $results[$k];
  420. }
  421. }
  422. private function getHosts(): array
  423. {
  424. $hosts = [$this->redis];
  425. if ($this->redis instanceof \Predis\ClientInterface) {
  426. $connection = $this->redis->getConnection();
  427. if ($connection instanceof ClusterInterface && $connection instanceof \Traversable) {
  428. $hosts = [];
  429. foreach ($connection as $c) {
  430. $hosts[] = new \Predis\Client($c);
  431. }
  432. }
  433. } elseif ($this->redis instanceof \RedisArray) {
  434. $hosts = [];
  435. foreach ($this->redis->_hosts() as $host) {
  436. $hosts[] = $this->redis->_instance($host);
  437. }
  438. } elseif ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster) {
  439. $hosts = [];
  440. foreach ($this->redis->_masters() as $host) {
  441. $hosts[] = $h = new \Redis();
  442. $h->connect($host[0], $host[1]);
  443. }
  444. }
  445. return $hosts;
  446. }
  447. }