PhpRedisClusterConnection.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Illuminate\Redis\Connections;
  3. use InvalidArgumentException;
  4. class PhpRedisClusterConnection extends PhpRedisConnection
  5. {
  6. /**
  7. * The RedisCluster client.
  8. *
  9. * @var \RedisCluster
  10. */
  11. protected $client;
  12. /**
  13. * The default node to use from the cluster.
  14. *
  15. * @var string|array
  16. */
  17. protected $defaultNode;
  18. /**
  19. * Scan all keys based on the given options.
  20. *
  21. * @param mixed $cursor
  22. * @param array $options
  23. * @return mixed
  24. *
  25. * @throws \InvalidArgumentException
  26. */
  27. #[\Override]
  28. public function scan($cursor, $options = [])
  29. {
  30. $result = $this->client->scan($cursor,
  31. $options['node'] ?? $this->defaultNode(),
  32. $options['match'] ?? '*',
  33. $options['count'] ?? 10
  34. );
  35. if ($result === false) {
  36. $result = [];
  37. }
  38. return $cursor === 0 && empty($result) ? false : [$cursor, $result];
  39. }
  40. /**
  41. * Flush the selected Redis database on all master nodes.
  42. *
  43. * @return void
  44. */
  45. public function flushdb()
  46. {
  47. $arguments = func_get_args();
  48. $async = strtoupper((string) ($arguments[0] ?? null)) === 'ASYNC';
  49. foreach ($this->client->_masters() as $master) {
  50. $async
  51. ? $this->command('rawCommand', [$master, 'flushdb', 'async'])
  52. : $this->command('flushdb', [$master]);
  53. }
  54. }
  55. /**
  56. * Return default node to use for cluster.
  57. *
  58. * @return string|array
  59. *
  60. * @throws \InvalidArgumentException
  61. */
  62. private function defaultNode()
  63. {
  64. if (! isset($this->defaultNode)) {
  65. $this->defaultNode = $this->client->_masters()[0] ?? throw new InvalidArgumentException('Unable to determine default node. No master nodes found in the cluster.');
  66. }
  67. return $this->defaultNode;
  68. }
  69. }