Connection.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Illuminate\Redis\Connections;
  3. use Closure;
  4. use Illuminate\Redis\Limiters\DurationLimiterBuilder;
  5. use Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder;
  6. /**
  7. * @mixin \Predis\Client
  8. */
  9. abstract class Connection
  10. {
  11. /**
  12. * The Predis client.
  13. *
  14. * @var \Predis\Client
  15. */
  16. protected $client;
  17. /**
  18. * Subscribe to a set of given channels for messages.
  19. *
  20. * @param array|string $channels
  21. * @param \Closure $callback
  22. * @param string $method
  23. * @return void
  24. */
  25. abstract public function createSubscription($channels, Closure $callback, $method = 'subscribe');
  26. /**
  27. * Funnel a callback for a maximum number of simultaneous executions.
  28. *
  29. * @param string $name
  30. * @return \Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder
  31. */
  32. public function funnel($name)
  33. {
  34. return new ConcurrencyLimiterBuilder($this, $name);
  35. }
  36. /**
  37. * Throttle a callback for a maximum number of executions over a given duration.
  38. *
  39. * @param string $name
  40. * @return \Illuminate\Redis\Limiters\DurationLimiterBuilder
  41. */
  42. public function throttle($name)
  43. {
  44. return new DurationLimiterBuilder($this, $name);
  45. }
  46. /**
  47. * Get the underlying Redis client.
  48. *
  49. * @return mixed
  50. */
  51. public function client()
  52. {
  53. return $this->client;
  54. }
  55. /**
  56. * Subscribe to a set of given channels for messages.
  57. *
  58. * @param array|string $channels
  59. * @param \Closure $callback
  60. * @return void
  61. */
  62. public function subscribe($channels, Closure $callback)
  63. {
  64. return $this->createSubscription($channels, $callback, __FUNCTION__);
  65. }
  66. /**
  67. * Subscribe to a set of given channels with wildcards.
  68. *
  69. * @param array|string $channels
  70. * @param \Closure $callback
  71. * @return void
  72. */
  73. public function psubscribe($channels, Closure $callback)
  74. {
  75. return $this->createSubscription($channels, $callback, __FUNCTION__);
  76. }
  77. /**
  78. * Run a command against the Redis database.
  79. *
  80. * @param string $method
  81. * @param array $parameters
  82. * @return mixed
  83. */
  84. public function command($method, array $parameters = [])
  85. {
  86. return $this->client->{$method}(...$parameters);
  87. }
  88. /**
  89. * Pass other method calls down to the underlying client.
  90. *
  91. * @param string $method
  92. * @param array $parameters
  93. * @return mixed
  94. */
  95. public function __call($method, $parameters)
  96. {
  97. return $this->command($method, $parameters);
  98. }
  99. }