RedisTrait.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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\Connection\Factory;
  14. use Predis\Response\Status;
  15. use Symfony\Component\Cache\Exception\CacheException;
  16. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  17. /**
  18. * @author Aurimas Niekis <aurimas@niekis.lt>
  19. * @author Nicolas Grekas <p@tchwork.com>
  20. *
  21. * @internal
  22. */
  23. trait RedisTrait
  24. {
  25. private static $defaultConnectionOptions = [
  26. 'class' => null,
  27. 'persistent' => 0,
  28. 'persistent_id' => null,
  29. 'timeout' => 30,
  30. 'read_timeout' => 0,
  31. 'retry_interval' => 0,
  32. 'lazy' => false,
  33. ];
  34. private $redis;
  35. /**
  36. * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
  37. */
  38. private function init($redisClient, $namespace = '', $defaultLifetime = 0)
  39. {
  40. parent::__construct($namespace, $defaultLifetime);
  41. if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
  42. throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
  43. }
  44. if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client && !$redisClient instanceof RedisProxy) {
  45. throw new InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, "%s" given.', __METHOD__, \is_object($redisClient) ? \get_class($redisClient) : \gettype($redisClient)));
  46. }
  47. $this->redis = $redisClient;
  48. }
  49. /**
  50. * Creates a Redis connection using a DSN configuration.
  51. *
  52. * Example DSN:
  53. * - redis://localhost
  54. * - redis://example.com:1234
  55. * - redis://secret@example.com/13
  56. * - redis:///var/run/redis.sock
  57. * - redis://secret@/var/run/redis.sock/13
  58. *
  59. * @param string $dsn
  60. * @param array $options See self::$defaultConnectionOptions
  61. *
  62. * @throws InvalidArgumentException when the DSN is invalid
  63. *
  64. * @return \Redis|\Predis\Client According to the "class" option
  65. */
  66. public static function createConnection($dsn, array $options = [])
  67. {
  68. if (0 !== strpos($dsn, 'redis://')) {
  69. throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s" does not start with "redis://".', $dsn));
  70. }
  71. $params = preg_replace_callback('#^redis://(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) {
  72. if (isset($m[1])) {
  73. $auth = $m[1];
  74. }
  75. return 'file://';
  76. }, $dsn);
  77. if (false === $params = parse_url($params)) {
  78. throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
  79. }
  80. if (!isset($params['host']) && !isset($params['path'])) {
  81. throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
  82. }
  83. if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
  84. $params['dbindex'] = $m[1];
  85. $params['path'] = substr($params['path'], 0, -\strlen($m[0]));
  86. }
  87. if (isset($params['host'])) {
  88. $scheme = 'tcp';
  89. } else {
  90. $scheme = 'unix';
  91. }
  92. $params += [
  93. 'host' => isset($params['host']) ? $params['host'] : $params['path'],
  94. 'port' => isset($params['host']) ? 6379 : null,
  95. 'dbindex' => 0,
  96. ];
  97. if (isset($params['query'])) {
  98. parse_str($params['query'], $query);
  99. $params += $query;
  100. }
  101. $params += $options + self::$defaultConnectionOptions;
  102. if (null === $params['class'] && !\extension_loaded('redis') && !class_exists(\Predis\Client::class)) {
  103. throw new CacheException(sprintf('Cannot find the "redis" extension, and "predis/predis" is not installed: "%s".', $dsn));
  104. }
  105. $class = null === $params['class'] ? (\extension_loaded('redis') ? \Redis::class : \Predis\Client::class) : $params['class'];
  106. if (is_a($class, \Redis::class, true)) {
  107. $connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
  108. $redis = new $class();
  109. $initializer = function ($redis) use ($connect, $params, $dsn, $auth) {
  110. try {
  111. @$redis->{$connect}($params['host'], $params['port'], $params['timeout'], $params['persistent_id'], $params['retry_interval']);
  112. set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
  113. $isConnected = $redis->isConnected();
  114. restore_error_handler();
  115. if (!$isConnected) {
  116. $error = preg_match('/^Redis::p?connect\(\): (.*)/', $error, $error) ? sprintf(' (%s)', $error[1]) : '';
  117. throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$error.'.');
  118. }
  119. if ((null !== $auth && !$redis->auth($auth))
  120. || ($params['dbindex'] && !$redis->select($params['dbindex']))
  121. || ($params['read_timeout'] && !$redis->setOption(\Redis::OPT_READ_TIMEOUT, $params['read_timeout']))
  122. ) {
  123. $e = preg_replace('/^ERR /', '', $redis->getLastError());
  124. throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e.'.');
  125. }
  126. } catch (\RedisException $e) {
  127. throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
  128. }
  129. return true;
  130. };
  131. if ($params['lazy']) {
  132. $redis = new RedisProxy($redis, $initializer);
  133. } else {
  134. $initializer($redis);
  135. }
  136. } elseif (is_a($class, \Predis\Client::class, true)) {
  137. $params['scheme'] = $scheme;
  138. $params['database'] = $params['dbindex'] ?: null;
  139. $params['password'] = $auth;
  140. $redis = new $class((new Factory())->create($params));
  141. } elseif (class_exists($class, false)) {
  142. throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis" or "Predis\Client".', $class));
  143. } else {
  144. throw new InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  145. }
  146. return $redis;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. protected function doFetch(array $ids)
  152. {
  153. if (!$ids) {
  154. return [];
  155. }
  156. $result = [];
  157. if ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof ClusterInterface) {
  158. $values = $this->pipeline(function () use ($ids) {
  159. foreach ($ids as $id) {
  160. yield 'get' => [$id];
  161. }
  162. });
  163. } else {
  164. $values = $this->redis->mget($ids);
  165. if (!\is_array($values) || \count($values) !== \count($ids)) {
  166. return [];
  167. }
  168. $values = array_combine($ids, $values);
  169. }
  170. foreach ($values as $id => $v) {
  171. if ($v) {
  172. $result[$id] = parent::unserialize($v);
  173. }
  174. }
  175. return $result;
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. protected function doHave($id)
  181. {
  182. return (bool) $this->redis->exists($id);
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. protected function doClear($namespace)
  188. {
  189. $cleared = true;
  190. $hosts = [$this->redis];
  191. $evalArgs = [[$namespace], 0];
  192. if ($this->redis instanceof \Predis\Client) {
  193. $evalArgs = [0, $namespace];
  194. $connection = $this->redis->getConnection();
  195. if ($connection instanceof ClusterInterface && $connection instanceof \Traversable) {
  196. $hosts = [];
  197. foreach ($connection as $c) {
  198. $hosts[] = new \Predis\Client($c);
  199. }
  200. }
  201. } elseif ($this->redis instanceof \RedisArray) {
  202. $hosts = [];
  203. foreach ($this->redis->_hosts() as $host) {
  204. $hosts[] = $this->redis->_instance($host);
  205. }
  206. } elseif ($this->redis instanceof \RedisCluster) {
  207. $hosts = [];
  208. foreach ($this->redis->_masters() as $host) {
  209. $hosts[] = $h = new \Redis();
  210. $h->connect($host[0], $host[1]);
  211. }
  212. }
  213. foreach ($hosts as $host) {
  214. if (!isset($namespace[0])) {
  215. $cleared = $host->flushDb() && $cleared;
  216. continue;
  217. }
  218. $info = $host->info('Server');
  219. $info = isset($info['Server']) ? $info['Server'] : $info;
  220. if (!version_compare($info['redis_version'], '2.8', '>=')) {
  221. // As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
  222. // can hang your server when it is executed against large databases (millions of items).
  223. // Whenever you hit this scale, you should really consider upgrading to Redis 2.8 or above.
  224. $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;
  225. continue;
  226. }
  227. $cursor = null;
  228. do {
  229. $keys = $host instanceof \Predis\Client ? $host->scan($cursor, 'MATCH', $namespace.'*', 'COUNT', 1000) : $host->scan($cursor, $namespace.'*', 1000);
  230. if (isset($keys[1]) && \is_array($keys[1])) {
  231. $cursor = $keys[0];
  232. $keys = $keys[1];
  233. }
  234. if ($keys) {
  235. $this->doDelete($keys);
  236. }
  237. } while ($cursor = (int) $cursor);
  238. }
  239. return $cleared;
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. protected function doDelete(array $ids)
  245. {
  246. if (!$ids) {
  247. return true;
  248. }
  249. if ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof ClusterInterface) {
  250. $this->pipeline(function () use ($ids) {
  251. foreach ($ids as $id) {
  252. yield 'del' => [$id];
  253. }
  254. })->rewind();
  255. } else {
  256. $this->redis->del($ids);
  257. }
  258. return true;
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. protected function doSave(array $values, $lifetime)
  264. {
  265. $serialized = [];
  266. $failed = [];
  267. foreach ($values as $id => $value) {
  268. try {
  269. $serialized[$id] = serialize($value);
  270. } catch (\Exception $e) {
  271. $failed[] = $id;
  272. }
  273. }
  274. if (!$serialized) {
  275. return $failed;
  276. }
  277. $results = $this->pipeline(function () use ($serialized, $lifetime) {
  278. foreach ($serialized as $id => $value) {
  279. if (0 >= $lifetime) {
  280. yield 'set' => [$id, $value];
  281. } else {
  282. yield 'setEx' => [$id, $lifetime, $value];
  283. }
  284. }
  285. });
  286. foreach ($results as $id => $result) {
  287. if (true !== $result && (!$result instanceof Status || $result !== Status::get('OK'))) {
  288. $failed[] = $id;
  289. }
  290. }
  291. return $failed;
  292. }
  293. private function pipeline(\Closure $generator)
  294. {
  295. $ids = [];
  296. if ($this->redis instanceof \RedisCluster || ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof RedisCluster)) {
  297. // phpredis & predis don't support pipelining with RedisCluster
  298. // see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#pipelining
  299. // see https://github.com/nrk/predis/issues/267#issuecomment-123781423
  300. $results = [];
  301. foreach ($generator() as $command => $args) {
  302. $results[] = \call_user_func_array([$this->redis, $command], $args);
  303. $ids[] = $args[0];
  304. }
  305. } elseif ($this->redis instanceof \Predis\Client) {
  306. $results = $this->redis->pipeline(function ($redis) use ($generator, &$ids) {
  307. foreach ($generator() as $command => $args) {
  308. \call_user_func_array([$redis, $command], $args);
  309. $ids[] = $args[0];
  310. }
  311. });
  312. } elseif ($this->redis instanceof \RedisArray) {
  313. $connections = $results = $ids = [];
  314. foreach ($generator() as $command => $args) {
  315. if (!isset($connections[$h = $this->redis->_target($args[0])])) {
  316. $connections[$h] = [$this->redis->_instance($h), -1];
  317. $connections[$h][0]->multi(\Redis::PIPELINE);
  318. }
  319. \call_user_func_array([$connections[$h][0], $command], $args);
  320. $results[] = [$h, ++$connections[$h][1]];
  321. $ids[] = $args[0];
  322. }
  323. foreach ($connections as $h => $c) {
  324. $connections[$h] = $c[0]->exec();
  325. }
  326. foreach ($results as $k => list($h, $c)) {
  327. $results[$k] = $connections[$h][$c];
  328. }
  329. } else {
  330. $this->redis->multi(\Redis::PIPELINE);
  331. foreach ($generator() as $command => $args) {
  332. \call_user_func_array([$this->redis, $command], $args);
  333. $ids[] = $args[0];
  334. }
  335. $results = $this->redis->exec();
  336. }
  337. foreach ($ids as $k => $id) {
  338. yield $id => $results[$k];
  339. }
  340. }
  341. }