|
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 Cache\Adapter\Apcu\ApcuCachePool; |
|
20
|
|
|
use Cache\Adapter\Redis\RedisCachePool; |
|
21
|
|
|
use liamsorsby\Hystrix\Storage\Adapter\{AbstractStorage, Apc, RedisCluster}; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class StorageFactory |
|
25
|
|
|
* |
|
26
|
|
|
* @category Adapter |
|
27
|
|
|
* @package Storage |
|
28
|
|
|
* @author Liam Sorsby <[email protected]> |
|
29
|
|
|
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache |
|
30
|
|
|
* @link https://github.org/liamsorsby/php-hystrix |
|
31
|
|
|
*/ |
|
32
|
|
|
class StorageFactory |
|
33
|
|
|
{ |
|
34
|
|
|
public const REDISCLUSTER = 'redis-cluster'; |
|
35
|
|
|
public const APCU = 'apcu'; |
|
36
|
|
|
public const DEFAULT_PREFIX = ''; |
|
37
|
|
|
public const DEFAULT_THRESHOLD = 10; |
|
38
|
|
|
public const DEFAULT_DURATION = 500; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates storage from static factory method |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $storage Storage string to create the instance. |
|
44
|
|
|
* @param array $options Storage options to be passed to the instance. |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \RedisClusterException |
|
47
|
|
|
* |
|
48
|
|
|
* @return AbstractStorage |
|
49
|
|
|
*/ |
|
50
|
|
|
public function create(string $storage, array $options): AbstractStorage |
|
51
|
|
|
{ |
|
52
|
|
|
$options = $this->normaliseOptions($options); |
|
53
|
|
|
|
|
54
|
|
|
switch ($storage) { |
|
55
|
|
|
case self::REDISCLUSTER: |
|
56
|
|
|
return $this->createRedisClusterAdapter($options); |
|
57
|
|
|
case self::APCU: |
|
58
|
|
|
return $this->createApcuAdapter($options); |
|
59
|
|
|
default: |
|
60
|
|
|
throw new \InvalidArgumentException( |
|
61
|
|
|
sprintf('Invalid storage provided: %s', $storage) |
|
62
|
|
|
); |
|
63
|
|
|
}; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create Redis Cluster Adapter. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $options Options to be passed to the redis cluster adapter. |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \RedisClusterException |
|
72
|
|
|
* |
|
73
|
|
|
* @return RedisCluster |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function createRedisClusterAdapter(array $options): RedisCluster |
|
76
|
|
|
{ |
|
77
|
|
|
$storage = new RedisCluster( |
|
78
|
|
|
$options['prefix'], |
|
79
|
|
|
$options['threshold'], |
|
80
|
|
|
$options['duration'] |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$storage->setStorage( |
|
84
|
|
|
$this->createRedisCluster($options) |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
return $storage; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Build Redis Cluster Config |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $options Options to be passed to the redis cluster. |
|
94
|
|
|
* |
|
95
|
|
|
* @throws \RedisClusterException |
|
96
|
|
|
* |
|
97
|
|
|
* @return RedisCachePool |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function createRedisCluster(array $options): RedisCachePool |
|
100
|
|
|
{ |
|
101
|
|
|
$redis = new \RedisCluster( |
|
102
|
|
|
$options['name'], |
|
103
|
|
|
$options['seeds'], |
|
104
|
|
|
$options['timeout'] ?? null, |
|
105
|
|
|
$options['readTimeout'] ?? null, |
|
106
|
|
|
$options['persistent'] ?? false |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
return new RedisCachePool($redis); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Build APC instance. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $options Options to be passed to the APC adapter. |
|
116
|
|
|
* |
|
117
|
|
|
* @return Apc |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function createApcuAdapter(array $options): Apc |
|
120
|
|
|
{ |
|
121
|
|
|
$apc = new Apc( |
|
122
|
|
|
$options['prefix'], |
|
123
|
|
|
$options['threshold'], |
|
124
|
|
|
$options['duration'] |
|
125
|
|
|
); |
|
126
|
|
|
$apc->setStorage(new ApcuCachePool($options['skipOnCli'] ?? false)); |
|
127
|
|
|
|
|
128
|
|
|
return $apc; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Normalise options to return defaults if they aren't set |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $options Options array to be checked |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function normaliseOptions(array $options): array |
|
139
|
|
|
{ |
|
140
|
|
|
if (!isset($options['prefix'])) { |
|
141
|
|
|
$options['prefix'] = self::DEFAULT_PREFIX; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (!isset($options['duration'])) { |
|
145
|
|
|
$options['duration'] = self::DEFAULT_DURATION; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (!isset($options['threshold'])) { |
|
149
|
|
|
$options['threshold'] = self::DEFAULT_THRESHOLD; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $options; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|