1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel\Connectors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Redis\Connections\PredisConnection; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Monospice\SpicyIdentifiers\DynamicMethod; |
8
|
|
|
use Predis\Client; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Initializes Predis Client instances for Redis Sentinel connections |
12
|
|
|
* |
13
|
|
|
* @category Package |
14
|
|
|
* @package Monospice\LaravelRedisSentinel |
15
|
|
|
* @author Cy Rossignol <[email protected]> |
16
|
|
|
* @license See LICENSE file |
17
|
|
|
* @link http://github.com/monospice/laravel-redis-sentinel-drivers |
18
|
|
|
*/ |
19
|
|
|
class PredisConnector |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Configuration options specific to Sentinel connection operation |
23
|
|
|
* |
24
|
|
|
* We cannot pass these options as an array to the Predis client. |
25
|
|
|
* Instead, we'll set them on the connection directly using methods |
26
|
|
|
* provided by the SentinelReplication class of the Predis package. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $sentinelConnectionOptionKeys = [ |
31
|
|
|
'sentinel_timeout', |
32
|
|
|
'retry_wait', |
33
|
|
|
'retry_limit', |
34
|
|
|
'update_sentinels', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new Redis Sentinel connection from the provided configuration |
39
|
|
|
* options |
40
|
|
|
* |
41
|
|
|
* @param array $server The client configuration for the connection |
42
|
|
|
* @param array $options The global client options shared by all Sentinel |
43
|
|
|
* connections |
44
|
|
|
* |
45
|
|
|
* @return PredisConnection The Sentinel connection containing a configured |
46
|
|
|
* Predis Client |
47
|
|
|
*/ |
48
|
|
|
public function connect(array $server, array $options = [ ]) |
49
|
|
|
{ |
50
|
|
|
// Merge the global options shared by all Sentinel connections with |
51
|
|
|
// connection-specific options |
52
|
|
|
$clientOpts = array_merge($options, Arr::pull($server, 'options', [ ])); |
53
|
|
|
|
54
|
|
|
// Automatically set "replication" to "sentinel". This is the Sentinel |
55
|
|
|
// driver, after all. |
56
|
|
|
$clientOpts['replication'] = 'sentinel'; |
57
|
|
|
|
58
|
|
|
// Extract the array of Sentinel connection options from the rest of |
59
|
|
|
// the client options |
60
|
|
|
$sentinelKeys = array_flip($this->sentinelConnectionOptionKeys); |
61
|
|
|
$sentinelOpts = array_intersect_key($clientOpts, $sentinelKeys); |
62
|
|
|
|
63
|
|
|
// Filter the Sentinel connection options elements from the client |
64
|
|
|
// options array |
65
|
|
|
$clientOpts = array_diff_key($clientOpts, $sentinelKeys); |
66
|
|
|
|
67
|
|
|
return new PredisConnection( |
68
|
|
|
$this->makePredisClient($server, $clientOpts, $sentinelOpts) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create a Predis Client instance configured with the provided options |
74
|
|
|
* |
75
|
|
|
* @param array $server The client configuration for the connection |
76
|
|
|
* @param array $clientOpts Non-sentinel client options |
77
|
|
|
* @param array $sentinelOpts Sentinel-specific options |
78
|
|
|
* |
79
|
|
|
* @return void |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
protected function makePredisClient( |
82
|
|
|
array $server, |
83
|
|
|
array $clientOpts, |
84
|
|
|
array $sentinelOpts |
85
|
|
|
) { |
86
|
|
|
$client = new Client($server, $clientOpts); |
87
|
|
|
$connection = $client->getConnection(); |
88
|
|
|
|
89
|
|
|
// Set the Sentinel-specific connection options on the Predis Client |
90
|
|
|
// connection |
91
|
|
|
foreach ($sentinelOpts as $option => $value) { |
92
|
|
|
DynamicMethod::parseFromUnderscore($option) |
93
|
|
|
->prepend('set') |
94
|
|
|
->callOn($connection, [ $value ]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $client; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.