1 | <?php |
||
29 | class PredisConnection extends Client |
||
30 | { |
||
31 | /** |
||
32 | * The number of times the client attempts to retry a command when it fails |
||
33 | * to connect to a Redis instance behind Sentinel. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $retryLimit = 20; |
||
38 | |||
39 | /** |
||
40 | * The time in milliseconds to wait before the client retries a failed |
||
41 | * command. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $retryWait = 1000; |
||
46 | |||
47 | /** |
||
48 | * Set the default amount of time to wait before determining that a |
||
49 | * connection attempt to a Sentinel server failed. |
||
50 | * |
||
51 | * @param float $seconds The timeout value in seconds. |
||
52 | * |
||
53 | * @return $this The current instance for method chaining. |
||
54 | */ |
||
55 | public function setSentinelTimeout($seconds) |
||
61 | |||
62 | /** |
||
63 | * Set the default number of attempts to retry a command when the client |
||
64 | * fails to connect to a Redis instance behind Sentinel. |
||
65 | * |
||
66 | * @param int $attempts With a value of 0, throw an exception after the |
||
67 | * first failed attempt. Pass a value of -1 to retry connections forever. |
||
68 | * |
||
69 | * @return $this The current instance for method chaining. |
||
70 | */ |
||
71 | public function setRetryLimit($attempts) |
||
78 | |||
79 | /** |
||
80 | * Set the time to wait before retrying a command after a connection |
||
81 | * attempt failed. |
||
82 | * |
||
83 | * @param int $milliseconds The wait time in milliseconds. When 0, retry |
||
84 | * a failed command immediately. |
||
85 | * |
||
86 | * @return $this The current instance for method chaining. |
||
87 | */ |
||
88 | public function setRetryWait($milliseconds) |
||
95 | |||
96 | /** |
||
97 | * Set whether the client should update the list of known Sentinels each |
||
98 | * time it needs to connect to a Redis server behind Sentinel. |
||
99 | * |
||
100 | * @param bool $enable If TRUE, fetch the updated Sentinel list. |
||
101 | * |
||
102 | * @return $this The current instance for method chaining. |
||
103 | */ |
||
104 | public function setUpdateSentinels($enable) |
||
110 | |||
111 | /** |
||
112 | * Subscribe to a set of given channels for messages. |
||
113 | * |
||
114 | * @param array|string $channels The names of the channels to subscribe to. |
||
115 | * @param Closure $callback Executed for each message. Receives the |
||
116 | * message string in the first argument and the message channel as the |
||
117 | * second argument. Return FALSE to unsubscribe. |
||
118 | * @param string $method The subscription command ("subscribe" or |
||
119 | * "psubscribe"). |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function createSubscription( |
||
142 | |||
143 | /** |
||
144 | * Create a new PUB/SUB subscriber and pass messages to the callback if |
||
145 | * provided. |
||
146 | * |
||
147 | * WARNING: Consumers created using this method are not monitored for |
||
148 | * connection failures. For Sentinel support, use one of the methods |
||
149 | * provided by the Laravel API instead (subscribe() and psubscribe()). |
||
150 | * |
||
151 | * @param array|null $options Configures the channel(s) to subscribe to. |
||
152 | * @param callable $callback Optional callback executed for each message |
||
153 | * published to the configured channel(s). |
||
154 | * |
||
155 | * @return \Predis\PubSub\Consumer|null A PUB/SUB context used to create |
||
156 | * a subscription loop if no callback provided. |
||
157 | */ |
||
158 | public function pubSubLoop($options = null, $callback = null) |
||
164 | |||
165 | /** |
||
166 | * Execute commands in a transaction. |
||
167 | * |
||
168 | * This package overrides the transaction() method to work around a |
||
169 | * limitation in the Predis API that disallows transactions on "aggregate" |
||
170 | * connections like Sentinel. Note that transactions execute on the Redis |
||
171 | * master instance. |
||
172 | * |
||
173 | * @param callable|null $callback Contains the Redis commands to execute in |
||
174 | * the transaction. The callback receives a Predis\Transaction\MultiExec |
||
175 | * transaction abstraction as the only argument. We use this object to |
||
176 | * execute Redis commands by calling its methods just like we would with |
||
177 | * the Laravel Redis service. |
||
178 | * |
||
179 | * @return array|Predis\Transaction\MultiExec An array containing the |
||
180 | * result for each command executed during the transaction. If no callback |
||
181 | * provided, returns an instance of the Predis transaction abstraction. |
||
182 | */ |
||
183 | public function transaction(callable $callback = null) |
||
189 | |||
190 | /** |
||
191 | * Creates a new client instance for the specified connection ID or alias, |
||
192 | * only when working with an aggregate connection (cluster, replication). |
||
193 | * The new client instances uses the same options of the original one. |
||
194 | * |
||
195 | * @param string $connectionID Identifier of a connection. |
||
196 | * |
||
197 | * @return Client A Predis client instance for the specified connection. |
||
198 | * |
||
199 | * @throws InvalidArgumentException When the aggregate connection does not |
||
200 | * contain a node that matches the specified ID. |
||
201 | */ |
||
202 | public function getClientFor($connectionID) |
||
215 | |||
216 | /** |
||
217 | * Attempt to retry the provided operation when the client fails to connect |
||
218 | * to a Redis server. |
||
219 | * |
||
220 | * We adapt Predis' Sentinel connection failure handling logic here to |
||
221 | * reproduce the high-availability mode provided by the actual client. To |
||
222 | * work around "aggregate" connection limitations in Predis, this class |
||
223 | * provides methods that don't use the high-level Sentinel connection API |
||
224 | * of Predis directly, so it needs to handle connection failures itself. |
||
225 | * |
||
226 | * @param callable $callback The operation to execute. |
||
227 | * |
||
228 | * @return mixed The result of the first successful attempt. |
||
229 | * |
||
230 | * @throws CommunicationException After exhausting the allowed number of |
||
231 | * attempts to reconnect. |
||
232 | */ |
||
233 | protected function retryOnFailure(callable $callback) |
||
252 | |||
253 | /** |
||
254 | * Execute the provided callback for each message read by the PUB/SUB |
||
255 | * consumer. |
||
256 | * |
||
257 | * @param PubSub $loop Reads the messages published to a channel. |
||
258 | * @param string $kind The subscribed message type ([p]message). |
||
259 | * @param Closure $callback Executed for each message. |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | protected function consumeMessages(PubSub $loop, $kind, Closure $callback) |
||
273 | |||
274 | /** |
||
275 | * Get a Predis client instance for the master. |
||
276 | * |
||
277 | * @return Client The client instance for the current master. |
||
278 | */ |
||
279 | protected function getMaster() |
||
283 | |||
284 | /** |
||
285 | * Get a Predis client instance for a random slave. |
||
286 | * |
||
287 | * @param bool $fallbackToMaster If TRUE, return a client for the master |
||
288 | * if the connection does not include any slaves. |
||
289 | * |
||
290 | * @return Client The client instance for the selected slave. |
||
291 | * |
||
292 | * @throws RuntimeException When the client cannot reach any replicas |
||
293 | * (and the master if $fallbackToMaster is TRUE). |
||
294 | */ |
||
295 | protected function getRandomSlave($fallbackToMaster = true) |
||
311 | } |
||
312 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: