1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* StorageFactory for storage adapter for php-hystrix. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.2 |
7
|
|
|
* |
8
|
|
|
* @category Adapter |
9
|
|
|
* @package Storage |
10
|
|
|
* @author Liam Sorsby <[email protected]> |
11
|
|
|
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache |
12
|
|
|
* @link https://github.org/liamsorsby/php-hystrix |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE file. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace liamsorsby\Hystrix\Storage; |
18
|
|
|
|
19
|
|
|
use liamsorsby\Hystrix\Storage\Adapter\{ |
20
|
|
|
AbstractStorage, |
21
|
|
|
Apc, |
22
|
|
|
Memcached, |
23
|
|
|
RedisCluster, |
24
|
|
|
Redis, |
25
|
|
|
RedisArray |
26
|
|
|
}; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class StorageFactory |
30
|
|
|
* |
31
|
|
|
* @category Adapter |
32
|
|
|
* @package Storage |
33
|
|
|
* @author Liam Sorsby <[email protected]> |
34
|
|
|
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache |
35
|
|
|
* @link https://github.org/liamsorsby/php-hystrix |
36
|
|
|
*/ |
37
|
|
|
class StorageFactory |
38
|
|
|
{ |
39
|
|
|
// Caching constants |
40
|
|
|
public const APCU = 'apcu'; |
41
|
|
|
public const MEMCACHED = 'memcached'; |
42
|
|
|
public const REDIS = 'redis'; |
43
|
|
|
public const REDIS_ARRAY = 'redis-array'; |
44
|
|
|
public const REDIS_CLUSTER = 'redis-cluster'; |
45
|
|
|
|
46
|
|
|
/* |
47
|
|
|
* Default config for Circuit Breaker |
48
|
|
|
*/ |
49
|
|
|
public const DEFAULT_PREFIX = ''; |
50
|
|
|
public const DEFAULT_THRESHOLD = 10; |
51
|
|
|
public const DEFAULT_DURATION = 500; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Creates storage from static factory method |
55
|
|
|
* |
56
|
|
|
* @param string $storage Storage string to create the instance. |
57
|
|
|
* @param array $options Storage options to be passed to the instance. |
58
|
|
|
* |
59
|
|
|
* @throws \RedisClusterException |
60
|
|
|
* |
61
|
|
|
* @return AbstractStorage |
62
|
|
|
*/ |
63
|
|
|
public function create(string $storage, array $options): AbstractStorage |
64
|
|
|
{ |
65
|
|
|
$options = $this->normaliseOptions($options); |
66
|
|
|
|
67
|
|
|
switch ($storage) { |
68
|
|
|
case self::APCU: |
69
|
|
|
return $this->createApcuAdapter($options); |
70
|
|
|
case self::MEMCACHED: |
71
|
|
|
return $this->createMemcachedAdapter($options); |
72
|
|
|
case self::REDIS: |
73
|
|
|
return $this->createRedisAdapter($options); |
74
|
|
|
case self::REDIS_ARRAY: |
75
|
|
|
return $this->createRedisArrayAdapter($options); |
76
|
|
|
case self::REDIS_CLUSTER: |
77
|
|
|
return $this->createRedisClusterAdapter($options); |
78
|
|
|
default: |
79
|
|
|
throw new \InvalidArgumentException( |
80
|
|
|
sprintf('Invalid storage provided: %s', $storage) |
81
|
|
|
); |
82
|
|
|
}; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create Memcached Adapter. |
87
|
|
|
* |
88
|
|
|
* @param array $options Options to be passed to the memcached adapter. |
89
|
|
|
* |
90
|
|
|
* @return Memcached |
91
|
|
|
*/ |
92
|
|
|
protected function createMemcachedAdapter(array $options) |
93
|
|
|
{ |
94
|
|
|
$memcached = new Memcached( |
95
|
|
|
$options['prefix'], |
96
|
|
|
$options['threshold'], |
97
|
|
|
$options['duration'] |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$memcached->create($options); |
101
|
|
|
|
102
|
|
|
return $memcached; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create Redis Cluster Adapter. |
107
|
|
|
* |
108
|
|
|
* @param array $options Options to be passed to the redis cluster adapter. |
109
|
|
|
* |
110
|
|
|
* @throws \RedisClusterException |
111
|
|
|
* |
112
|
|
|
* @return RedisCluster |
113
|
|
|
*/ |
114
|
|
|
protected function createRedisClusterAdapter(array $options): RedisCluster |
115
|
|
|
{ |
116
|
|
|
$redis = new RedisCluster( |
117
|
|
|
$options['prefix'], |
118
|
|
|
$options['threshold'], |
119
|
|
|
$options['duration'] |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$redis->create($options); |
123
|
|
|
|
124
|
|
|
return $redis; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create Redis Array Adapter. |
129
|
|
|
* |
130
|
|
|
* @param array $options Options to be passed to the redis cluster adapter. |
131
|
|
|
* |
132
|
|
|
* @throws \RedisException |
133
|
|
|
* |
134
|
|
|
* @return RedisArray |
135
|
|
|
*/ |
136
|
|
|
protected function createRedisArrayAdapter(array $options): RedisArray |
137
|
|
|
{ |
138
|
|
|
$redis = new RedisArray( |
139
|
|
|
$options['prefix'], |
140
|
|
|
$options['threshold'], |
141
|
|
|
$options['duration'] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$redis->create($options); |
145
|
|
|
|
146
|
|
|
return $redis; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Create Redis Cluster Adapter. |
151
|
|
|
* |
152
|
|
|
* @param array $options Options to be passed to the redis cluster adapter. |
153
|
|
|
* |
154
|
|
|
* @throws \RedisClusterException |
155
|
|
|
* |
156
|
|
|
* @return Redis |
157
|
|
|
*/ |
158
|
|
|
protected function createRedisAdapter(array $options): Redis |
159
|
|
|
{ |
160
|
|
|
$redis = new Redis( |
161
|
|
|
$options['prefix'], |
162
|
|
|
$options['threshold'], |
163
|
|
|
$options['duration'] |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$redis->create($options); |
167
|
|
|
|
168
|
|
|
return $redis; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Build APC instance. |
173
|
|
|
* |
174
|
|
|
* @param array $options Options to be passed to the APC adapter. |
175
|
|
|
* |
176
|
|
|
* @return Apc |
177
|
|
|
*/ |
178
|
|
|
protected function createApcuAdapter(array $options): Apc |
179
|
|
|
{ |
180
|
|
|
$apc = new Apc( |
181
|
|
|
$options['prefix'], |
182
|
|
|
$options['threshold'], |
183
|
|
|
$options['duration'] |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$apc->create($options); |
187
|
|
|
|
188
|
|
|
return $apc; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Normalise options to return defaults if they aren't set |
193
|
|
|
* |
194
|
|
|
* @param array $options Options array to be checked |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
protected function normaliseOptions(array $options): array |
199
|
|
|
{ |
200
|
|
|
if (!isset($options['prefix'])) { |
201
|
|
|
$options['prefix'] = self::DEFAULT_PREFIX; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (!isset($options['duration'])) { |
205
|
|
|
$options['duration'] = self::DEFAULT_DURATION; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (!isset($options['threshold'])) { |
209
|
|
|
$options['threshold'] = self::DEFAULT_THRESHOLD; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $options; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|