33namespace Illuminate \Redis \Connections ;
44
55use Closure ;
6+ use Illuminate \Contracts \Events \Dispatcher ;
7+ use Illuminate \Redis \Events \CommandExecuted ;
68use Illuminate \Redis \Limiters \DurationLimiterBuilder ;
79use Illuminate \Redis \Limiters \ConcurrencyLimiterBuilder ;
810
@@ -25,6 +27,13 @@ abstract class Connection
2527 */
2628 protected $ name ;
2729
30+ /**
31+ * The event dispatcher instance.
32+ *
33+ * @var \Illuminate\Contracts\Events\Dispatcher
34+ */
35+ protected $ events ;
36+
2837 /**
2938 * Subscribe to a set of given channels for messages.
3039 *
@@ -100,7 +109,43 @@ public function psubscribe($channels, Closure $callback)
100109 */
101110 public function command ($ method , array $ parameters = [])
102111 {
103- return $ this ->client ->{$ method }(...$ parameters );
112+ $ start = microtime (true );
113+
114+ $ result = $ this ->client ->{$ method }(...$ parameters );
115+
116+ $ time = round ((microtime (true ) - $ start ) * 1000 , 2 );
117+
118+ if (isset ($ this ->events )) {
119+ $ this ->event (new CommandExecuted ($ method , $ parameters , $ time , $ this ));
120+ }
121+
122+ return $ result ;
123+ }
124+
125+ /**
126+ * Fire the given event if possible.
127+ *
128+ * @param mixed $event
129+ * @return void
130+ */
131+ protected function event ($ event )
132+ {
133+ if (isset ($ this ->events )) {
134+ $ this ->events ->dispatch ($ event );
135+ }
136+ }
137+
138+ /**
139+ * Register a Redis command listener with the connection.
140+ *
141+ * @param \Closure $callback
142+ * @return void
143+ */
144+ public function listen (Closure $ callback )
145+ {
146+ if (isset ($ this ->events )) {
147+ $ this ->events ->listen (CommandExecuted::class, $ callback );
148+ }
104149 }
105150
106151 /**
@@ -126,6 +171,37 @@ public function setName($name)
126171 return $ this ;
127172 }
128173
174+ /**
175+ * Get the event dispatcher used by the connection.
176+ *
177+ * @return \Illuminate\Contracts\Events\Dispatcher
178+ */
179+ public function getEventDispatcher ()
180+ {
181+ return $ this ->events ;
182+ }
183+
184+ /**
185+ * Set the event dispatcher instance on the connection.
186+ *
187+ * @param \Illuminate\Contracts\Events\Dispatcher $events
188+ * @return void
189+ */
190+ public function setEventDispatcher (Dispatcher $ events )
191+ {
192+ $ this ->events = $ events ;
193+ }
194+
195+ /**
196+ * Unset the event dispatcher instance on the connection.
197+ *
198+ * @return void
199+ */
200+ public function unsetEventDispatcher ()
201+ {
202+ $ this ->events = null ;
203+ }
204+
129205 /**
130206 * Pass other method calls down to the underlying client.
131207 *
0 commit comments