Connection.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Illuminate\Redis\Connections;
  3. use Closure;
  4. use Illuminate\Contracts\Events\Dispatcher;
  5. use Illuminate\Redis\Events\CommandExecuted;
  6. use Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder;
  7. use Illuminate\Redis\Limiters\DurationLimiterBuilder;
  8. use Illuminate\Support\Traits\Macroable;
  9. abstract class Connection
  10. {
  11. use Macroable {
  12. __call as macroCall;
  13. }
  14. /**
  15. * The Redis client.
  16. *
  17. * @var \Redis
  18. */
  19. protected $client;
  20. /**
  21. * The Redis connection name.
  22. *
  23. * @var string|null
  24. */
  25. protected $name;
  26. /**
  27. * The event dispatcher instance.
  28. *
  29. * @var \Illuminate\Contracts\Events\Dispatcher|null
  30. */
  31. protected $events;
  32. /**
  33. * Subscribe to a set of given channels for messages.
  34. *
  35. * @param array|string $channels
  36. * @param \Closure $callback
  37. * @param string $method
  38. * @return void
  39. */
  40. abstract public function createSubscription($channels, Closure $callback, $method = 'subscribe');
  41. /**
  42. * Funnel a callback for a maximum number of simultaneous executions.
  43. *
  44. * @param string $name
  45. * @return \Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder
  46. */
  47. public function funnel($name)
  48. {
  49. return new ConcurrencyLimiterBuilder($this, $name);
  50. }
  51. /**
  52. * Throttle a callback for a maximum number of executions over a given duration.
  53. *
  54. * @param string $name
  55. * @return \Illuminate\Redis\Limiters\DurationLimiterBuilder
  56. */
  57. public function throttle($name)
  58. {
  59. return new DurationLimiterBuilder($this, $name);
  60. }
  61. /**
  62. * Get the underlying Redis client.
  63. *
  64. * @return mixed
  65. */
  66. public function client()
  67. {
  68. return $this->client;
  69. }
  70. /**
  71. * Subscribe to a set of given channels for messages.
  72. *
  73. * @param array|string $channels
  74. * @param \Closure $callback
  75. * @return void
  76. */
  77. public function subscribe($channels, Closure $callback)
  78. {
  79. $this->createSubscription($channels, $callback, __FUNCTION__);
  80. }
  81. /**
  82. * Subscribe to a set of given channels with wildcards.
  83. *
  84. * @param array|string $channels
  85. * @param \Closure $callback
  86. * @return void
  87. */
  88. public function psubscribe($channels, Closure $callback)
  89. {
  90. $this->createSubscription($channels, $callback, __FUNCTION__);
  91. }
  92. /**
  93. * Run a command against the Redis database.
  94. *
  95. * @param string $method
  96. * @param array $parameters
  97. * @return mixed
  98. */
  99. public function command($method, array $parameters = [])
  100. {
  101. $start = microtime(true);
  102. $result = $this->client->{$method}(...$parameters);
  103. $time = round((microtime(true) - $start) * 1000, 2);
  104. $this->events?->dispatch(new CommandExecuted(
  105. $method, $this->parseParametersForEvent($parameters), $time, $this
  106. ));
  107. return $result;
  108. }
  109. /**
  110. * Parse the command's parameters for event dispatching.
  111. *
  112. * @param array $parameters
  113. * @return array
  114. */
  115. protected function parseParametersForEvent(array $parameters)
  116. {
  117. return $parameters;
  118. }
  119. /**
  120. * Fire the given event if possible.
  121. *
  122. * @param mixed $event
  123. * @return void
  124. *
  125. * @deprecated since Laravel 11.x
  126. */
  127. protected function event($event)
  128. {
  129. $this->events?->dispatch($event);
  130. }
  131. /**
  132. * Register a Redis command listener with the connection.
  133. *
  134. * @param \Closure $callback
  135. * @return void
  136. */
  137. public function listen(Closure $callback)
  138. {
  139. $this->events?->listen(CommandExecuted::class, $callback);
  140. }
  141. /**
  142. * Get the connection name.
  143. *
  144. * @return string|null
  145. */
  146. public function getName()
  147. {
  148. return $this->name;
  149. }
  150. /**
  151. * Set the connection's name.
  152. *
  153. * @param string $name
  154. * @return $this
  155. */
  156. public function setName($name)
  157. {
  158. $this->name = $name;
  159. return $this;
  160. }
  161. /**
  162. * Get the event dispatcher used by the connection.
  163. *
  164. * @return \Illuminate\Contracts\Events\Dispatcher
  165. */
  166. public function getEventDispatcher()
  167. {
  168. return $this->events;
  169. }
  170. /**
  171. * Set the event dispatcher instance on the connection.
  172. *
  173. * @param \Illuminate\Contracts\Events\Dispatcher $events
  174. * @return void
  175. */
  176. public function setEventDispatcher(Dispatcher $events)
  177. {
  178. $this->events = $events;
  179. }
  180. /**
  181. * Unset the event dispatcher instance on the connection.
  182. *
  183. * @return void
  184. */
  185. public function unsetEventDispatcher()
  186. {
  187. $this->events = null;
  188. }
  189. /**
  190. * Pass other method calls down to the underlying client.
  191. *
  192. * @param string $method
  193. * @param array $parameters
  194. * @return mixed
  195. */
  196. public function __call($method, $parameters)
  197. {
  198. if (static::hasMacro($method)) {
  199. return $this->macroCall($method, $parameters);
  200. }
  201. return $this->command($method, $parameters);
  202. }
  203. }