|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\Redis\Connectors; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Redis\Connections\PhpRedisClusterConnection; |
|
8
|
|
|
use Illuminate\Redis\Connectors\PhpRedisConnector as BasePhpRedisConnector; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
use Illuminate\Support\Facades\Redis as RedisFacade; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use LogicException; |
|
13
|
|
|
use Redis; |
|
14
|
|
|
use RedisArray; |
|
15
|
|
|
|
|
16
|
|
|
use function array_map; |
|
17
|
|
|
use function array_merge; |
|
18
|
|
|
use function defined; |
|
19
|
|
|
|
|
20
|
|
|
class PhpRedisConnector extends BasePhpRedisConnector |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Create a new clustered PhpRedis connection. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $config |
|
26
|
|
|
* @param array $clusterOptions |
|
27
|
|
|
* @param array $options |
|
28
|
|
|
* @return \Illuminate\Redis\Connections\PhpRedisClusterConnection |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function connectToCluster(array $config, array $clusterOptions, array $options) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$options = array_merge($options, $clusterOptions, Arr::pull($config, 'options', [])); |
|
33
|
|
|
|
|
34
|
|
|
// Use native Redis clustering |
|
35
|
1 |
|
if (Arr::get($options, 'cluster') === 'redis') { |
|
36
|
|
|
return new PhpRedisClusterConnection($this->createRedisClusterInstance( |
|
37
|
|
|
array_map([$this, 'buildClusterConnectionString'], $config), |
|
38
|
|
|
$options, |
|
39
|
|
|
)); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Use client-side sharding |
|
43
|
1 |
|
return new PhpRedisClusterConnection($this->createRedisArrayInstance( |
|
44
|
1 |
|
array_map([$this, 'buildRedisArrayConnectionString'], $config), |
|
45
|
|
|
$options, |
|
46
|
|
|
)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create the Redis client instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $config |
|
53
|
|
|
* @return \Redis |
|
54
|
|
|
*/ |
|
55
|
1 |
|
protected function createClient(array $config) |
|
56
|
|
|
{ |
|
57
|
1 |
|
return tap(new Redis(), function (Redis $client) use ($config) { |
|
58
|
1 |
|
if ($client instanceof RedisFacade) { |
|
59
|
|
|
throw new LogicException( |
|
60
|
|
|
'Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension.', |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$this->establishConnection($client, $config); |
|
65
|
|
|
|
|
66
|
1 |
|
if (! empty($config['password'])) { |
|
67
|
|
|
$client->auth((string) $config['password']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
if (! empty($config['database'])) { |
|
71
|
1 |
|
$client->select((int) $config['database']); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
if (! empty($config['prefix'])) { |
|
75
|
1 |
|
$client->setOption(Redis::OPT_PREFIX, (string) $config['prefix']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if (! empty($config['read_timeout'])) { |
|
79
|
1 |
|
$client->setOption(Redis::OPT_READ_TIMEOUT, (string) $config['read_timeout']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
if (! empty($config['serializer'])) { |
|
83
|
1 |
|
$serializer = $this->getSerializerFromConfig($config['serializer']); |
|
84
|
1 |
|
$client->setOption(Redis::OPT_SERIALIZER, (string) $serializer); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
if (empty($config['scan'])) { |
|
88
|
1 |
|
$client->setOption(Redis::OPT_SCAN, (string) Redis::SCAN_RETRY); |
|
89
|
|
|
} |
|
90
|
1 |
|
}); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Build a PhpRedis hosts array. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $server |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
1 |
|
protected function buildRedisArrayConnectionString(array $server) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $server['host'] . ':' . $server['port']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create a new redis array instance. |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $servers |
|
108
|
|
|
* @param array $options |
|
109
|
|
|
* @return \RedisArray |
|
110
|
|
|
*/ |
|
111
|
1 |
|
protected function createRedisArrayInstance(array $servers, array $options) |
|
112
|
|
|
{ |
|
113
|
1 |
|
$client = new RedisArray($servers, Arr::only($options, [ |
|
114
|
1 |
|
'function', |
|
115
|
|
|
'previous', |
|
116
|
|
|
'retry_interval', |
|
117
|
|
|
'lazy_connect', |
|
118
|
|
|
'connect_timeout', |
|
119
|
|
|
'read_timeout', |
|
120
|
|
|
'algorithm', |
|
121
|
|
|
'consistent', |
|
122
|
|
|
'distributor', |
|
123
|
|
|
])); |
|
124
|
|
|
|
|
125
|
1 |
|
if (! empty($options['password'])) { |
|
126
|
|
|
// @TODO: Remove after this will be implemented |
|
127
|
|
|
// https://github.com/phpredis/phpredis/issues/1508 |
|
128
|
|
|
throw new InvalidArgumentException('RedisArray does not support authorization'); |
|
129
|
|
|
//$client->auth((string) $options['password']); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
if (! empty($options['database'])) { |
|
133
|
1 |
|
$client->select((int) $options['database']); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
if (! empty($options['prefix'])) { |
|
137
|
1 |
|
$client->setOption(Redis::OPT_PREFIX, (string) $options['prefix']); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
if (! empty($options['serializer'])) { |
|
141
|
1 |
|
$serializer = $this->getSerializerFromConfig($options['serializer']); |
|
142
|
1 |
|
$client->setOption(Redis::OPT_SERIALIZER, (string) $serializer); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
return $client; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Resolve serializer |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $serializer |
|
152
|
|
|
* @return int |
|
153
|
|
|
* @throws \InvalidArgumentException |
|
154
|
|
|
*/ |
|
155
|
2 |
|
protected function getSerializerFromConfig(string $serializer): int |
|
156
|
|
|
{ |
|
157
|
2 |
|
switch ($serializer) { |
|
158
|
2 |
|
case 'igbinary': |
|
159
|
2 |
|
if (! defined('Redis::SERIALIZER_IGBINARY')) { |
|
160
|
|
|
throw new InvalidArgumentException('Error: phpredis was not compiled with igbinary support!'); |
|
161
|
|
|
} |
|
162
|
2 |
|
$flag = Redis::SERIALIZER_IGBINARY; |
|
163
|
2 |
|
break; |
|
164
|
|
|
case 'php': |
|
165
|
|
|
$flag = Redis::SERIALIZER_PHP; |
|
166
|
|
|
break; |
|
167
|
|
|
default: |
|
168
|
|
|
$flag = Redis::SERIALIZER_NONE; |
|
169
|
|
|
break; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
2 |
|
return $flag; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|