Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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