@@ -26,23 +26,23 @@ |
||
26 | 26 | */ |
27 | 27 | abstract class ItemAbstract implements ExtendedCacheItemInterface |
28 | 28 | { |
29 | - use ItemBaseTrait { |
|
30 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
31 | - } |
|
29 | + use ItemBaseTrait { |
|
30 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param ExtendedCacheItemPoolInterface $driver |
|
35 | - * @return static |
|
36 | - * @throws PhpfastcacheInvalidArgumentException |
|
37 | - */ |
|
38 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
39 | - { |
|
40 | - if ($driver instanceof ClusterPoolInterface) { |
|
41 | - $this->driver = $driver; |
|
33 | + /** |
|
34 | + * @param ExtendedCacheItemPoolInterface $driver |
|
35 | + * @return static |
|
36 | + * @throws PhpfastcacheInvalidArgumentException |
|
37 | + */ |
|
38 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
39 | + { |
|
40 | + if ($driver instanceof ClusterPoolInterface) { |
|
41 | + $this->driver = $driver; |
|
42 | 42 | |
43 | - return $this; |
|
44 | - } |
|
43 | + return $this; |
|
44 | + } |
|
45 | 45 | |
46 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
47 | - } |
|
46 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
47 | + } |
|
48 | 48 | } |
@@ -24,8 +24,8 @@ |
||
24 | 24 | */ |
25 | 25 | interface ClusterPoolInterface extends ExtendedCacheItemPoolInterface |
26 | 26 | { |
27 | - /** |
|
28 | - * @return ExtendedCacheItemPoolInterface[] |
|
29 | - */ |
|
30 | - public function getClusterPools(): array; |
|
27 | + /** |
|
28 | + * @return ExtendedCacheItemPoolInterface[] |
|
29 | + */ |
|
30 | + public function getClusterPools(): array; |
|
31 | 31 | } |
@@ -35,140 +35,140 @@ |
||
35 | 35 | class ClusterAggregator implements AggregatorInterface |
36 | 36 | { |
37 | 37 | |
38 | - protected $driverPools; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var ClusterPoolInterface |
|
42 | - */ |
|
43 | - protected $cluster; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $clusterAggregatorName; |
|
49 | - |
|
50 | - /** |
|
51 | - * ClusterAggregator constructor. |
|
52 | - * @param string $clusterAggregatorName |
|
53 | - * @param AggregatablePoolInterface ...$driverPools |
|
54 | - * @throws PhpfastcacheLogicException |
|
55 | - */ |
|
56 | - public function __construct(string $clusterAggregatorName = '', AggregatablePoolInterface ...$driverPools) |
|
57 | - { |
|
58 | - $clusterAggregatorName = trim($clusterAggregatorName); |
|
59 | - if (empty($clusterAggregatorName)) { |
|
60 | - try { |
|
61 | - $clusterAggregatorName = 'cluster_' . \bin2hex(\random_bytes(15)); |
|
62 | - } catch (Exception $e) { |
|
63 | - $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_hash(new stdClass())); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - $this->clusterAggregatorName = $clusterAggregatorName; |
|
68 | - |
|
69 | - foreach ($driverPools as $driverPool) { |
|
70 | - $this->aggregateDriver($driverPool); |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param AggregatablePoolInterface $driverPool |
|
76 | - * |
|
77 | - * @throws PhpfastcacheLogicException |
|
78 | - */ |
|
79 | - public function aggregateDriver(AggregatablePoolInterface $driverPool): void |
|
80 | - { |
|
81 | - if ($this->cluster) { |
|
82 | - throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
83 | - } |
|
84 | - |
|
85 | - $splHash = \spl_object_hash($driverPool); |
|
86 | - if (!isset($this->driverPools[$splHash])) { |
|
87 | - if ($driverPool instanceof ClusterPoolInterface) { |
|
88 | - throw new PhpfastcacheLogicException('Recursive cluster aggregation is not allowed !'); |
|
89 | - } |
|
90 | - |
|
91 | - $this->driverPools[$splHash] = $driverPool; |
|
92 | - } else { |
|
93 | - throw new PhpfastcacheLogicException('This pool has been already aggregated !'); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $driverName |
|
99 | - * @param ConfigurationOption|null $driverConfig |
|
100 | - * @throws PhpfastcacheDriverCheckException |
|
101 | - * @throws PhpfastcacheDriverException |
|
102 | - * @throws PhpfastcacheDriverNotFoundException |
|
103 | - * @throws PhpfastcacheInvalidArgumentException |
|
104 | - * @throws PhpfastcacheInvalidConfigurationException |
|
105 | - * @throws PhpfastcacheLogicException |
|
106 | - * @throws ReflectionException |
|
107 | - */ |
|
108 | - public function aggregateNewDriver(string $driverName, ConfigurationOption $driverConfig = null): void |
|
109 | - { |
|
110 | - if ($this->cluster) { |
|
111 | - throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
112 | - } |
|
113 | - $this->aggregateDriver( |
|
114 | - CacheManager::getInstance($driverName, $driverConfig) |
|
115 | - ); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param AggregatablePoolInterface $driverPool |
|
120 | - * |
|
121 | - * @throws PhpfastcacheLogicException |
|
122 | - */ |
|
123 | - public function disaggregateDriver(AggregatablePoolInterface $driverPool): void |
|
124 | - { |
|
125 | - if ($this->cluster) { |
|
126 | - throw new PhpfastcacheLogicException('The cluster has been already build, cannot disaggregate pools.'); |
|
127 | - } |
|
128 | - |
|
129 | - $splHash = \spl_object_hash($driverPool); |
|
130 | - if (isset($this->driverPools[$splHash])) { |
|
131 | - unset($this->driverPools[$splHash]); |
|
132 | - } else { |
|
133 | - throw new PhpfastcacheLogicException('This pool was not aggregated !'); |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param int $strategy |
|
139 | - * |
|
140 | - * @return ClusterPoolInterface |
|
141 | - * @throws PhpfastcacheInvalidArgumentException |
|
142 | - */ |
|
143 | - public function getCluster(int $strategy = AggregatorInterface::STRATEGY_FULL_REPLICATION): ClusterPoolInterface |
|
144 | - { |
|
145 | - if (isset(ClusterPoolAbstract::STRATEGY[$strategy])) { |
|
146 | - if (!$this->cluster) { |
|
147 | - $clusterClass = ClusterPoolAbstract::STRATEGY[$strategy]; |
|
148 | - $this->cluster = new $clusterClass( |
|
149 | - $this->getClusterAggregatorName(), |
|
150 | - ...\array_values($this->driverPools) |
|
151 | - ); |
|
152 | - |
|
153 | - /** |
|
154 | - * @eventName CacheClusterBuilt |
|
155 | - * @param $clusterAggregator AggregatorInterface |
|
156 | - * @param $cluster ClusterPoolInterface |
|
157 | - */ |
|
158 | - $this->cluster->getEventManager()->dispatch('CacheClusterBuilt', $this, $this->cluster); |
|
159 | - } |
|
160 | - } else { |
|
161 | - throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy'); |
|
162 | - } |
|
163 | - |
|
164 | - return $this->cluster; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - public function getClusterAggregatorName(): string |
|
171 | - { |
|
172 | - return $this->clusterAggregatorName; |
|
173 | - } |
|
38 | + protected $driverPools; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var ClusterPoolInterface |
|
42 | + */ |
|
43 | + protected $cluster; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $clusterAggregatorName; |
|
49 | + |
|
50 | + /** |
|
51 | + * ClusterAggregator constructor. |
|
52 | + * @param string $clusterAggregatorName |
|
53 | + * @param AggregatablePoolInterface ...$driverPools |
|
54 | + * @throws PhpfastcacheLogicException |
|
55 | + */ |
|
56 | + public function __construct(string $clusterAggregatorName = '', AggregatablePoolInterface ...$driverPools) |
|
57 | + { |
|
58 | + $clusterAggregatorName = trim($clusterAggregatorName); |
|
59 | + if (empty($clusterAggregatorName)) { |
|
60 | + try { |
|
61 | + $clusterAggregatorName = 'cluster_' . \bin2hex(\random_bytes(15)); |
|
62 | + } catch (Exception $e) { |
|
63 | + $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_hash(new stdClass())); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + $this->clusterAggregatorName = $clusterAggregatorName; |
|
68 | + |
|
69 | + foreach ($driverPools as $driverPool) { |
|
70 | + $this->aggregateDriver($driverPool); |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param AggregatablePoolInterface $driverPool |
|
76 | + * |
|
77 | + * @throws PhpfastcacheLogicException |
|
78 | + */ |
|
79 | + public function aggregateDriver(AggregatablePoolInterface $driverPool): void |
|
80 | + { |
|
81 | + if ($this->cluster) { |
|
82 | + throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
83 | + } |
|
84 | + |
|
85 | + $splHash = \spl_object_hash($driverPool); |
|
86 | + if (!isset($this->driverPools[$splHash])) { |
|
87 | + if ($driverPool instanceof ClusterPoolInterface) { |
|
88 | + throw new PhpfastcacheLogicException('Recursive cluster aggregation is not allowed !'); |
|
89 | + } |
|
90 | + |
|
91 | + $this->driverPools[$splHash] = $driverPool; |
|
92 | + } else { |
|
93 | + throw new PhpfastcacheLogicException('This pool has been already aggregated !'); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $driverName |
|
99 | + * @param ConfigurationOption|null $driverConfig |
|
100 | + * @throws PhpfastcacheDriverCheckException |
|
101 | + * @throws PhpfastcacheDriverException |
|
102 | + * @throws PhpfastcacheDriverNotFoundException |
|
103 | + * @throws PhpfastcacheInvalidArgumentException |
|
104 | + * @throws PhpfastcacheInvalidConfigurationException |
|
105 | + * @throws PhpfastcacheLogicException |
|
106 | + * @throws ReflectionException |
|
107 | + */ |
|
108 | + public function aggregateNewDriver(string $driverName, ConfigurationOption $driverConfig = null): void |
|
109 | + { |
|
110 | + if ($this->cluster) { |
|
111 | + throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
112 | + } |
|
113 | + $this->aggregateDriver( |
|
114 | + CacheManager::getInstance($driverName, $driverConfig) |
|
115 | + ); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param AggregatablePoolInterface $driverPool |
|
120 | + * |
|
121 | + * @throws PhpfastcacheLogicException |
|
122 | + */ |
|
123 | + public function disaggregateDriver(AggregatablePoolInterface $driverPool): void |
|
124 | + { |
|
125 | + if ($this->cluster) { |
|
126 | + throw new PhpfastcacheLogicException('The cluster has been already build, cannot disaggregate pools.'); |
|
127 | + } |
|
128 | + |
|
129 | + $splHash = \spl_object_hash($driverPool); |
|
130 | + if (isset($this->driverPools[$splHash])) { |
|
131 | + unset($this->driverPools[$splHash]); |
|
132 | + } else { |
|
133 | + throw new PhpfastcacheLogicException('This pool was not aggregated !'); |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param int $strategy |
|
139 | + * |
|
140 | + * @return ClusterPoolInterface |
|
141 | + * @throws PhpfastcacheInvalidArgumentException |
|
142 | + */ |
|
143 | + public function getCluster(int $strategy = AggregatorInterface::STRATEGY_FULL_REPLICATION): ClusterPoolInterface |
|
144 | + { |
|
145 | + if (isset(ClusterPoolAbstract::STRATEGY[$strategy])) { |
|
146 | + if (!$this->cluster) { |
|
147 | + $clusterClass = ClusterPoolAbstract::STRATEGY[$strategy]; |
|
148 | + $this->cluster = new $clusterClass( |
|
149 | + $this->getClusterAggregatorName(), |
|
150 | + ...\array_values($this->driverPools) |
|
151 | + ); |
|
152 | + |
|
153 | + /** |
|
154 | + * @eventName CacheClusterBuilt |
|
155 | + * @param $clusterAggregator AggregatorInterface |
|
156 | + * @param $cluster ClusterPoolInterface |
|
157 | + */ |
|
158 | + $this->cluster->getEventManager()->dispatch('CacheClusterBuilt', $this, $this->cluster); |
|
159 | + } |
|
160 | + } else { |
|
161 | + throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy'); |
|
162 | + } |
|
163 | + |
|
164 | + return $this->cluster; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + public function getClusterAggregatorName(): string |
|
171 | + { |
|
172 | + return $this->clusterAggregatorName; |
|
173 | + } |
|
174 | 174 | } |
@@ -16,9 +16,9 @@ discard block |
||
16 | 16 | namespace Phpfastcache\Cluster; |
17 | 17 | |
18 | 18 | use Phpfastcache\Cluster\Drivers\{FullReplication\FullReplicationCluster, |
19 | - MasterSlaveReplication\MasterSlaveReplicationCluster, |
|
20 | - RandomReplication\RandomReplicationCluster, |
|
21 | - SemiReplication\SemiReplicationCluster |
|
19 | + MasterSlaveReplication\MasterSlaveReplicationCluster, |
|
20 | + RandomReplication\RandomReplicationCluster, |
|
21 | + SemiReplication\SemiReplicationCluster |
|
22 | 22 | }; |
23 | 23 | use Phpfastcache\Config\ConfigurationOption; |
24 | 24 | use Phpfastcache\Core\{Item\ExtendedCacheItemInterface, Pool\DriverBaseTrait, Pool\ExtendedCacheItemPoolInterface}; |
@@ -39,196 +39,196 @@ discard block |
||
39 | 39 | */ |
40 | 40 | abstract class ClusterPoolAbstract implements ClusterPoolInterface |
41 | 41 | { |
42 | - use DriverBaseTrait; |
|
43 | - use ClusterPoolTrait { |
|
44 | - DriverBaseTrait::__construct as private __parentConstruct; |
|
45 | - } |
|
46 | - |
|
47 | - public const STRATEGY = [ |
|
48 | - AggregatorInterface::STRATEGY_FULL_REPLICATION => FullReplicationCluster::class, |
|
49 | - AggregatorInterface::STRATEGY_SEMI_REPLICATION => SemiReplicationCluster::class, |
|
50 | - AggregatorInterface::STRATEGY_MASTER_SLAVE => MasterSlaveReplicationCluster::class, |
|
51 | - AggregatorInterface::STRATEGY_RANDOM_REPLICATION => RandomReplicationCluster::class, |
|
52 | - ]; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var ExtendedCacheItemPoolInterface[] |
|
56 | - */ |
|
57 | - protected $clusterPools; |
|
58 | - |
|
59 | - /** |
|
60 | - * ClusterPoolAbstract constructor. |
|
61 | - * @param string $clusterName |
|
62 | - * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
63 | - * @throws PhpfastcacheInvalidArgumentException |
|
64 | - * @throws PhpfastcacheDriverCheckException |
|
65 | - * @throws PhpfastcacheDriverConnectException |
|
66 | - * @throws PhpfastcacheInvalidConfigurationException |
|
67 | - * @throws ReflectionException |
|
68 | - */ |
|
69 | - public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
70 | - { |
|
71 | - if (count($driverPools) < 2) { |
|
72 | - throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
|
73 | - } |
|
74 | - $this->clusterPools = $driverPools; |
|
75 | - $this->__parentConstruct(new ConfigurationOption(), $clusterName); |
|
76 | - $this->setEventManager(EventManager::getInstance()); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @inheritDoc |
|
81 | - */ |
|
82 | - public function getIO(): DriverIO |
|
83 | - { |
|
84 | - $IO = new DriverIO(); |
|
85 | - foreach ($this->clusterPools as $clusterPool) { |
|
86 | - $IO->setReadHit($IO->getReadHit() + $clusterPool->getIO()->getReadHit()) |
|
87 | - ->setReadMiss($IO->getReadMiss() + $clusterPool->getIO()->getReadMiss()) |
|
88 | - ->setWriteHit($IO->getWriteHit() + $clusterPool->getIO()->getWriteHit()); |
|
89 | - } |
|
90 | - return $IO; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @inheritDoc |
|
95 | - */ |
|
96 | - public function getClusterPools(): array |
|
97 | - { |
|
98 | - return $this->clusterPools; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @inheritDoc |
|
103 | - */ |
|
104 | - public function getItems(array $keys = []) |
|
105 | - { |
|
106 | - $items = []; |
|
107 | - |
|
108 | - foreach ($keys as $key) { |
|
109 | - $items[$key] = $this->getItem($key); |
|
110 | - } |
|
111 | - |
|
112 | - return $items; |
|
113 | - } |
|
114 | - /** |
|
115 | - * Shared method used by All Clusters |
|
116 | - */ |
|
117 | - |
|
118 | - /** |
|
119 | - * @inheritDoc |
|
120 | - */ |
|
121 | - public function deleteItems(array $keys) |
|
122 | - { |
|
123 | - $hasDeletedOnce = false; |
|
124 | - foreach ($this->clusterPools as $driverPool) { |
|
125 | - if ($result = $driverPool->deleteItems($keys)) { |
|
126 | - $hasDeletedOnce = $result; |
|
127 | - } |
|
128 | - } |
|
129 | - // Return true only if at least one backend confirmed the "clear" operation |
|
130 | - return $hasDeletedOnce; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @inheritDoc |
|
135 | - */ |
|
136 | - public function saveDeferred(CacheItemInterface $item) |
|
137 | - { |
|
138 | - /** @var ExtendedCacheItemInterface $item */ |
|
139 | - $hasSavedOnce = false; |
|
140 | - foreach ($this->clusterPools as $driverPool) { |
|
141 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
142 | - if ($result = $driverPool->saveDeferred($poolItem)) { |
|
143 | - $hasSavedOnce = $result; |
|
144 | - } |
|
145 | - } |
|
146 | - // Return true only if at least one backend confirmed the "commit" operation |
|
147 | - return $hasSavedOnce; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param ExtendedCacheItemInterface $item |
|
152 | - * @param ExtendedCacheItemPoolInterface $driverPool |
|
153 | - * @return CacheItemInterface |
|
154 | - * @throws InvalidArgumentException |
|
155 | - */ |
|
156 | - protected function getStandardizedItem(ExtendedCacheItemInterface $item, ExtendedCacheItemPoolInterface $driverPool): CacheItemInterface |
|
157 | - { |
|
158 | - if (!$item->doesItemBelongToThatDriverBackend($driverPool)) { |
|
159 | - /** |
|
160 | - * Avoid infinite loop |
|
161 | - */ |
|
162 | - if ($driverPool === $this) { |
|
163 | - /** @var ExtendedCacheItemInterface $itemPool */ |
|
164 | - $itemClass = $driverPool->getClassNamespace() . '\\' . 'Item'; |
|
165 | - $itemPool = new $itemClass($this, $item->getKey()); |
|
166 | - $itemPool->setEventManager($this->getEventManager()) |
|
167 | - ->set($item->get()) |
|
168 | - ->setHit($item->isHit()) |
|
169 | - ->setTags($item->getTags()) |
|
170 | - ->expiresAt($item->getExpirationDate()) |
|
171 | - ->setDriver($driverPool); |
|
172 | - return $itemPool; |
|
173 | - } |
|
174 | - return $driverPool->getItem($item->getKey()) |
|
175 | - ->setEventManager($this->getEventManager()) |
|
176 | - ->set($item->get()) |
|
177 | - ->setHit($item->isHit()) |
|
178 | - ->setTags($item->getTags()) |
|
179 | - ->expiresAt($item->getExpirationDate()) |
|
180 | - ->setDriver($driverPool); |
|
181 | - } |
|
182 | - |
|
183 | - return $item->setEventManager($this->getEventManager()); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Interfaced methods that needs to be faked |
|
188 | - */ |
|
189 | - |
|
190 | - /** |
|
191 | - * @return DriverStatistic |
|
192 | - */ |
|
193 | - public function getStats(): DriverStatistic |
|
194 | - { |
|
195 | - $stats = new DriverStatistic(); |
|
196 | - $stats->setInfo( |
|
197 | - sprintf( |
|
198 | - 'Using %d pool(s): %s', |
|
199 | - \count($this->clusterPools), |
|
200 | - \implode( |
|
201 | - ', ', |
|
202 | - \array_map( |
|
203 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
204 | - return \get_class($pool); |
|
205 | - }, |
|
206 | - $this->clusterPools |
|
207 | - ) |
|
208 | - ) |
|
209 | - ) |
|
210 | - ); |
|
211 | - |
|
212 | - $stats->setSize( |
|
213 | - (int)\array_sum( |
|
214 | - \array_map( |
|
215 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
216 | - return $pool->getStats()->getSize(); |
|
217 | - }, |
|
218 | - $this->clusterPools |
|
219 | - ) |
|
220 | - ) |
|
221 | - ); |
|
222 | - |
|
223 | - $stats->setData( |
|
224 | - (int)\array_map( |
|
225 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
226 | - return $pool->getStats()->getData(); |
|
227 | - }, |
|
228 | - $this->clusterPools |
|
229 | - ) |
|
230 | - ); |
|
231 | - |
|
232 | - return $stats; |
|
233 | - } |
|
42 | + use DriverBaseTrait; |
|
43 | + use ClusterPoolTrait { |
|
44 | + DriverBaseTrait::__construct as private __parentConstruct; |
|
45 | + } |
|
46 | + |
|
47 | + public const STRATEGY = [ |
|
48 | + AggregatorInterface::STRATEGY_FULL_REPLICATION => FullReplicationCluster::class, |
|
49 | + AggregatorInterface::STRATEGY_SEMI_REPLICATION => SemiReplicationCluster::class, |
|
50 | + AggregatorInterface::STRATEGY_MASTER_SLAVE => MasterSlaveReplicationCluster::class, |
|
51 | + AggregatorInterface::STRATEGY_RANDOM_REPLICATION => RandomReplicationCluster::class, |
|
52 | + ]; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var ExtendedCacheItemPoolInterface[] |
|
56 | + */ |
|
57 | + protected $clusterPools; |
|
58 | + |
|
59 | + /** |
|
60 | + * ClusterPoolAbstract constructor. |
|
61 | + * @param string $clusterName |
|
62 | + * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
63 | + * @throws PhpfastcacheInvalidArgumentException |
|
64 | + * @throws PhpfastcacheDriverCheckException |
|
65 | + * @throws PhpfastcacheDriverConnectException |
|
66 | + * @throws PhpfastcacheInvalidConfigurationException |
|
67 | + * @throws ReflectionException |
|
68 | + */ |
|
69 | + public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
70 | + { |
|
71 | + if (count($driverPools) < 2) { |
|
72 | + throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
|
73 | + } |
|
74 | + $this->clusterPools = $driverPools; |
|
75 | + $this->__parentConstruct(new ConfigurationOption(), $clusterName); |
|
76 | + $this->setEventManager(EventManager::getInstance()); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @inheritDoc |
|
81 | + */ |
|
82 | + public function getIO(): DriverIO |
|
83 | + { |
|
84 | + $IO = new DriverIO(); |
|
85 | + foreach ($this->clusterPools as $clusterPool) { |
|
86 | + $IO->setReadHit($IO->getReadHit() + $clusterPool->getIO()->getReadHit()) |
|
87 | + ->setReadMiss($IO->getReadMiss() + $clusterPool->getIO()->getReadMiss()) |
|
88 | + ->setWriteHit($IO->getWriteHit() + $clusterPool->getIO()->getWriteHit()); |
|
89 | + } |
|
90 | + return $IO; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @inheritDoc |
|
95 | + */ |
|
96 | + public function getClusterPools(): array |
|
97 | + { |
|
98 | + return $this->clusterPools; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @inheritDoc |
|
103 | + */ |
|
104 | + public function getItems(array $keys = []) |
|
105 | + { |
|
106 | + $items = []; |
|
107 | + |
|
108 | + foreach ($keys as $key) { |
|
109 | + $items[$key] = $this->getItem($key); |
|
110 | + } |
|
111 | + |
|
112 | + return $items; |
|
113 | + } |
|
114 | + /** |
|
115 | + * Shared method used by All Clusters |
|
116 | + */ |
|
117 | + |
|
118 | + /** |
|
119 | + * @inheritDoc |
|
120 | + */ |
|
121 | + public function deleteItems(array $keys) |
|
122 | + { |
|
123 | + $hasDeletedOnce = false; |
|
124 | + foreach ($this->clusterPools as $driverPool) { |
|
125 | + if ($result = $driverPool->deleteItems($keys)) { |
|
126 | + $hasDeletedOnce = $result; |
|
127 | + } |
|
128 | + } |
|
129 | + // Return true only if at least one backend confirmed the "clear" operation |
|
130 | + return $hasDeletedOnce; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @inheritDoc |
|
135 | + */ |
|
136 | + public function saveDeferred(CacheItemInterface $item) |
|
137 | + { |
|
138 | + /** @var ExtendedCacheItemInterface $item */ |
|
139 | + $hasSavedOnce = false; |
|
140 | + foreach ($this->clusterPools as $driverPool) { |
|
141 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
142 | + if ($result = $driverPool->saveDeferred($poolItem)) { |
|
143 | + $hasSavedOnce = $result; |
|
144 | + } |
|
145 | + } |
|
146 | + // Return true only if at least one backend confirmed the "commit" operation |
|
147 | + return $hasSavedOnce; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param ExtendedCacheItemInterface $item |
|
152 | + * @param ExtendedCacheItemPoolInterface $driverPool |
|
153 | + * @return CacheItemInterface |
|
154 | + * @throws InvalidArgumentException |
|
155 | + */ |
|
156 | + protected function getStandardizedItem(ExtendedCacheItemInterface $item, ExtendedCacheItemPoolInterface $driverPool): CacheItemInterface |
|
157 | + { |
|
158 | + if (!$item->doesItemBelongToThatDriverBackend($driverPool)) { |
|
159 | + /** |
|
160 | + * Avoid infinite loop |
|
161 | + */ |
|
162 | + if ($driverPool === $this) { |
|
163 | + /** @var ExtendedCacheItemInterface $itemPool */ |
|
164 | + $itemClass = $driverPool->getClassNamespace() . '\\' . 'Item'; |
|
165 | + $itemPool = new $itemClass($this, $item->getKey()); |
|
166 | + $itemPool->setEventManager($this->getEventManager()) |
|
167 | + ->set($item->get()) |
|
168 | + ->setHit($item->isHit()) |
|
169 | + ->setTags($item->getTags()) |
|
170 | + ->expiresAt($item->getExpirationDate()) |
|
171 | + ->setDriver($driverPool); |
|
172 | + return $itemPool; |
|
173 | + } |
|
174 | + return $driverPool->getItem($item->getKey()) |
|
175 | + ->setEventManager($this->getEventManager()) |
|
176 | + ->set($item->get()) |
|
177 | + ->setHit($item->isHit()) |
|
178 | + ->setTags($item->getTags()) |
|
179 | + ->expiresAt($item->getExpirationDate()) |
|
180 | + ->setDriver($driverPool); |
|
181 | + } |
|
182 | + |
|
183 | + return $item->setEventManager($this->getEventManager()); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Interfaced methods that needs to be faked |
|
188 | + */ |
|
189 | + |
|
190 | + /** |
|
191 | + * @return DriverStatistic |
|
192 | + */ |
|
193 | + public function getStats(): DriverStatistic |
|
194 | + { |
|
195 | + $stats = new DriverStatistic(); |
|
196 | + $stats->setInfo( |
|
197 | + sprintf( |
|
198 | + 'Using %d pool(s): %s', |
|
199 | + \count($this->clusterPools), |
|
200 | + \implode( |
|
201 | + ', ', |
|
202 | + \array_map( |
|
203 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
204 | + return \get_class($pool); |
|
205 | + }, |
|
206 | + $this->clusterPools |
|
207 | + ) |
|
208 | + ) |
|
209 | + ) |
|
210 | + ); |
|
211 | + |
|
212 | + $stats->setSize( |
|
213 | + (int)\array_sum( |
|
214 | + \array_map( |
|
215 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
216 | + return $pool->getStats()->getSize(); |
|
217 | + }, |
|
218 | + $this->clusterPools |
|
219 | + ) |
|
220 | + ) |
|
221 | + ); |
|
222 | + |
|
223 | + $stats->setData( |
|
224 | + (int)\array_map( |
|
225 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
226 | + return $pool->getStats()->getData(); |
|
227 | + }, |
|
228 | + $this->clusterPools |
|
229 | + ) |
|
230 | + ); |
|
231 | + |
|
232 | + return $stats; |
|
233 | + } |
|
234 | 234 | } |
@@ -19,54 +19,54 @@ |
||
19 | 19 | |
20 | 20 | trait ClusterPoolTrait |
21 | 21 | { |
22 | - /** |
|
23 | - * @return bool |
|
24 | - */ |
|
25 | - protected function driverCheck(): bool |
|
26 | - { |
|
27 | - return true; |
|
28 | - } |
|
22 | + /** |
|
23 | + * @return bool |
|
24 | + */ |
|
25 | + protected function driverCheck(): bool |
|
26 | + { |
|
27 | + return true; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - protected function driverConnect(): bool |
|
34 | - { |
|
35 | - return true; |
|
36 | - } |
|
30 | + /** |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + protected function driverConnect(): bool |
|
34 | + { |
|
35 | + return true; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param CacheItemInterface $item |
|
40 | - * @return null |
|
41 | - */ |
|
42 | - protected function driverRead(CacheItemInterface $item) |
|
43 | - { |
|
44 | - return null; |
|
45 | - } |
|
38 | + /** |
|
39 | + * @param CacheItemInterface $item |
|
40 | + * @return null |
|
41 | + */ |
|
42 | + protected function driverRead(CacheItemInterface $item) |
|
43 | + { |
|
44 | + return null; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param CacheItemInterface $item |
|
49 | - * @return bool |
|
50 | - */ |
|
51 | - protected function driverWrite(CacheItemInterface $item): bool |
|
52 | - { |
|
53 | - return true; |
|
54 | - } |
|
47 | + /** |
|
48 | + * @param CacheItemInterface $item |
|
49 | + * @return bool |
|
50 | + */ |
|
51 | + protected function driverWrite(CacheItemInterface $item): bool |
|
52 | + { |
|
53 | + return true; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param CacheItemInterface $item |
|
58 | - * @return bool |
|
59 | - */ |
|
60 | - protected function driverDelete(CacheItemInterface $item): bool |
|
61 | - { |
|
62 | - return true; |
|
63 | - } |
|
56 | + /** |
|
57 | + * @param CacheItemInterface $item |
|
58 | + * @return bool |
|
59 | + */ |
|
60 | + protected function driverDelete(CacheItemInterface $item): bool |
|
61 | + { |
|
62 | + return true; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - protected function driverClear(): bool |
|
69 | - { |
|
70 | - return true; |
|
71 | - } |
|
65 | + /** |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + protected function driverClear(): bool |
|
69 | + { |
|
70 | + return true; |
|
71 | + } |
|
72 | 72 | } |
@@ -25,81 +25,81 @@ |
||
25 | 25 | interface AggregatorInterface |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Full replication mechanism |
|
30 | - * |
|
31 | - * Read on first working (and synchronize if needed, no failure allowed), |
|
32 | - * Write on all (no failure allowed), |
|
33 | - * Delete on all (no failure allowed) |
|
34 | - * |
|
35 | - * Conflict on multiple reads: Keep first found item (but sync the others) |
|
36 | - * Cluster size: 2 minimum, unlimited |
|
37 | - */ |
|
38 | - public const STRATEGY_FULL_REPLICATION = 1; |
|
28 | + /** |
|
29 | + * Full replication mechanism |
|
30 | + * |
|
31 | + * Read on first working (and synchronize if needed, no failure allowed), |
|
32 | + * Write on all (no failure allowed), |
|
33 | + * Delete on all (no failure allowed) |
|
34 | + * |
|
35 | + * Conflict on multiple reads: Keep first found item (but sync the others) |
|
36 | + * Cluster size: 2 minimum, unlimited |
|
37 | + */ |
|
38 | + public const STRATEGY_FULL_REPLICATION = 1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Semi replication mechanism |
|
42 | - * |
|
43 | - * Read first working (but do not synchronize, with partial failure allowed), |
|
44 | - * Write on all (with partial failure allowed) |
|
45 | - * Delete on all (with partial failure allowed) |
|
46 | - * |
|
47 | - * Conflict on multiple reads: Keep first found item |
|
48 | - * Cluster size: 2 minimum, unlimited |
|
49 | - */ |
|
50 | - public const STRATEGY_SEMI_REPLICATION = 2; |
|
40 | + /** |
|
41 | + * Semi replication mechanism |
|
42 | + * |
|
43 | + * Read first working (but do not synchronize, with partial failure allowed), |
|
44 | + * Write on all (with partial failure allowed) |
|
45 | + * Delete on all (with partial failure allowed) |
|
46 | + * |
|
47 | + * Conflict on multiple reads: Keep first found item |
|
48 | + * Cluster size: 2 minimum, unlimited |
|
49 | + */ |
|
50 | + public const STRATEGY_SEMI_REPLICATION = 2; |
|
51 | 51 | |
52 | - /** |
|
53 | - * First pool is master, second is slave |
|
54 | - * |
|
55 | - * Read from master (but do not synchronize, with master failure only allowed) |
|
56 | - * Write on all (with master failure only allowed) |
|
57 | - * Delete on all (with master failure only allowed) |
|
58 | - * |
|
59 | - * Conflict on multiple reads: No, master is exclusive source except if it fails |
|
60 | - * Cluster size: 2 exactly: Master & Slave (Exception if more or less) |
|
61 | - */ |
|
62 | - public const STRATEGY_MASTER_SLAVE = 4; |
|
52 | + /** |
|
53 | + * First pool is master, second is slave |
|
54 | + * |
|
55 | + * Read from master (but do not synchronize, with master failure only allowed) |
|
56 | + * Write on all (with master failure only allowed) |
|
57 | + * Delete on all (with master failure only allowed) |
|
58 | + * |
|
59 | + * Conflict on multiple reads: No, master is exclusive source except if it fails |
|
60 | + * Cluster size: 2 exactly: Master & Slave (Exception if more or less) |
|
61 | + */ |
|
62 | + public const STRATEGY_MASTER_SLAVE = 4; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Mostly used for development testing |
|
66 | - * |
|
67 | - * CRUD operations are made on a random-chosen backend from a given cluster. |
|
68 | - * This means you have 1 chance out of (n count of pools) to find an existing cache item |
|
69 | - * but also to write/delete an non-existing item. |
|
70 | - */ |
|
71 | - public const STRATEGY_RANDOM_REPLICATION = 8; |
|
64 | + /** |
|
65 | + * Mostly used for development testing |
|
66 | + * |
|
67 | + * CRUD operations are made on a random-chosen backend from a given cluster. |
|
68 | + * This means you have 1 chance out of (n count of pools) to find an existing cache item |
|
69 | + * but also to write/delete an non-existing item. |
|
70 | + */ |
|
71 | + public const STRATEGY_RANDOM_REPLICATION = 8; |
|
72 | 72 | |
73 | - /** |
|
74 | - * AggregatorInterface constructor. |
|
75 | - * |
|
76 | - * @param string $clusterAggregatorName |
|
77 | - * @param AggregatablePoolInterface ...$driverPools |
|
78 | - */ |
|
79 | - public function __construct(string $clusterAggregatorName, AggregatablePoolInterface ...$driverPools); |
|
73 | + /** |
|
74 | + * AggregatorInterface constructor. |
|
75 | + * |
|
76 | + * @param string $clusterAggregatorName |
|
77 | + * @param AggregatablePoolInterface ...$driverPools |
|
78 | + */ |
|
79 | + public function __construct(string $clusterAggregatorName, AggregatablePoolInterface ...$driverPools); |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param int $strategy |
|
83 | - * |
|
84 | - * @return ClusterPoolInterface |
|
85 | - */ |
|
86 | - public function getCluster(int $strategy): ClusterPoolInterface; |
|
81 | + /** |
|
82 | + * @param int $strategy |
|
83 | + * |
|
84 | + * @return ClusterPoolInterface |
|
85 | + */ |
|
86 | + public function getCluster(int $strategy): ClusterPoolInterface; |
|
87 | 87 | |
88 | - /** |
|
89 | - * @param string $driverName |
|
90 | - * @param ConfigurationOption|NULL $driverConfig |
|
91 | - * |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function aggregateNewDriver(string $driverName, ConfigurationOption $driverConfig = null): void; |
|
88 | + /** |
|
89 | + * @param string $driverName |
|
90 | + * @param ConfigurationOption|NULL $driverConfig |
|
91 | + * |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function aggregateNewDriver(string $driverName, ConfigurationOption $driverConfig = null): void; |
|
95 | 95 | |
96 | - /** |
|
97 | - * @param AggregatablePoolInterface $driverPool |
|
98 | - */ |
|
99 | - public function aggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
96 | + /** |
|
97 | + * @param AggregatablePoolInterface $driverPool |
|
98 | + */ |
|
99 | + public function aggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param AggregatablePoolInterface $driverPool |
|
103 | - */ |
|
104 | - public function disaggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
101 | + /** |
|
102 | + * @param AggregatablePoolInterface $driverPool |
|
103 | + */ |
|
104 | + public function disaggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
105 | 105 | } |
@@ -26,151 +26,151 @@ |
||
26 | 26 | */ |
27 | 27 | class FullReplicationCluster extends ClusterPoolAbstract |
28 | 28 | { |
29 | - /** |
|
30 | - * @inheritDoc |
|
31 | - */ |
|
32 | - public function getItem($key) |
|
33 | - { |
|
34 | - /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */ |
|
35 | - $poolsToResync = []; |
|
36 | - /** @var ExtendedCacheItemInterface $item */ |
|
37 | - $item = null; |
|
38 | - |
|
39 | - foreach ($this->clusterPools as $driverPool) { |
|
40 | - $poolItem = $driverPool->getItem($key); |
|
41 | - if ($poolItem->isHit()) { |
|
42 | - if (!$item) { |
|
43 | - $item = $poolItem; |
|
44 | - continue; |
|
45 | - } |
|
46 | - |
|
47 | - $itemData = $item->get(); |
|
48 | - $poolItemData = $poolItem->get(); |
|
49 | - |
|
50 | - if (\is_object($itemData) |
|
51 | - ) { |
|
52 | - if ($item->get() != $poolItemData) { |
|
53 | - $poolsToResync[] = $driverPool; |
|
54 | - } |
|
55 | - } else { |
|
56 | - if ($item->get() !== $poolItemData) { |
|
57 | - $poolsToResync[] = $driverPool; |
|
58 | - } |
|
59 | - } |
|
60 | - } else { |
|
61 | - $poolsToResync[] = $driverPool; |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - if ($item && $item->isHit() && \count($poolsToResync) < \count($this->clusterPools)) { |
|
66 | - foreach ($poolsToResync as $poolToResync) { |
|
67 | - $poolItem = $poolToResync->getItem($key); |
|
68 | - $poolItem->setEventManager($this->getEventManager()) |
|
69 | - ->set($item->get()) |
|
70 | - ->setHit($item->isHit()) |
|
71 | - ->setTags($item->getTags()) |
|
72 | - ->expiresAt($item->getExpirationDate()) |
|
73 | - ->setDriver($poolToResync); |
|
74 | - $poolToResync->save($poolItem); |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - return $this->getStandardizedItem($item ?? new Item($this, $key), $this); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @inheritDoc |
|
83 | - */ |
|
84 | - public function hasItem($key) |
|
85 | - { |
|
86 | - foreach ($this->clusterPools as $driverPool) { |
|
87 | - $poolItem = $driverPool->getItem($key); |
|
88 | - if ($poolItem->isHit()) { |
|
89 | - return true; |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - return false; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @inheritDoc |
|
98 | - */ |
|
99 | - public function clear() |
|
100 | - { |
|
101 | - $hasClearedOnce = false; |
|
102 | - foreach ($this->clusterPools as $driverPool) { |
|
103 | - if ($result = $driverPool->clear()) { |
|
104 | - $hasClearedOnce = $result; |
|
105 | - } |
|
106 | - } |
|
107 | - // Return true only if at least one backend confirmed the "clear" operation |
|
108 | - return $hasClearedOnce; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @inheritDoc |
|
113 | - */ |
|
114 | - public function deleteItem($key) |
|
115 | - { |
|
116 | - $hasDeletedOnce = false; |
|
117 | - foreach ($this->clusterPools as $driverPool) { |
|
118 | - if ($result = $driverPool->deleteItem($key)) { |
|
119 | - $hasDeletedOnce = $result; |
|
120 | - } |
|
121 | - } |
|
122 | - // Return true only if at least one backend confirmed the "clear" operation |
|
123 | - return $hasDeletedOnce; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @inheritDoc |
|
128 | - */ |
|
129 | - public function save(CacheItemInterface $item) |
|
130 | - { |
|
131 | - /** @var ExtendedCacheItemInterface $item */ |
|
132 | - $hasSavedOnce = false; |
|
133 | - foreach ($this->clusterPools as $driverPool) { |
|
134 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
135 | - if ($result = $driverPool->save($poolItem)) { |
|
136 | - $hasSavedOnce = $result; |
|
137 | - } |
|
138 | - } |
|
139 | - // Return true only if at least one backend confirmed the "commit" operation |
|
140 | - return $hasSavedOnce; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @inheritDoc |
|
145 | - */ |
|
146 | - public function saveDeferred(CacheItemInterface $item) |
|
147 | - { |
|
148 | - /** @var ExtendedCacheItemInterface $item */ |
|
149 | - $hasSavedOnce = false; |
|
150 | - foreach ($this->clusterPools as $driverPool) { |
|
151 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
152 | - if ($result = $driverPool->saveDeferred($poolItem)) { |
|
153 | - $hasSavedOnce = $result; |
|
154 | - } |
|
155 | - } |
|
156 | - // Return true only if at least one backend confirmed the "commit" operation |
|
157 | - return $hasSavedOnce; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @inheritDoc |
|
162 | - */ |
|
163 | - public function commit() |
|
164 | - { |
|
165 | - $hasCommitOnce = false; |
|
166 | - foreach ($this->clusterPools as $driverPool) { |
|
167 | - if ($result = $driverPool->commit()) { |
|
168 | - $hasCommitOnce = $result; |
|
169 | - } |
|
170 | - } |
|
171 | - // Return true only if at least one backend confirmed the "commit" operation |
|
172 | - return $hasCommitOnce; |
|
173 | - } |
|
29 | + /** |
|
30 | + * @inheritDoc |
|
31 | + */ |
|
32 | + public function getItem($key) |
|
33 | + { |
|
34 | + /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */ |
|
35 | + $poolsToResync = []; |
|
36 | + /** @var ExtendedCacheItemInterface $item */ |
|
37 | + $item = null; |
|
38 | + |
|
39 | + foreach ($this->clusterPools as $driverPool) { |
|
40 | + $poolItem = $driverPool->getItem($key); |
|
41 | + if ($poolItem->isHit()) { |
|
42 | + if (!$item) { |
|
43 | + $item = $poolItem; |
|
44 | + continue; |
|
45 | + } |
|
46 | + |
|
47 | + $itemData = $item->get(); |
|
48 | + $poolItemData = $poolItem->get(); |
|
49 | + |
|
50 | + if (\is_object($itemData) |
|
51 | + ) { |
|
52 | + if ($item->get() != $poolItemData) { |
|
53 | + $poolsToResync[] = $driverPool; |
|
54 | + } |
|
55 | + } else { |
|
56 | + if ($item->get() !== $poolItemData) { |
|
57 | + $poolsToResync[] = $driverPool; |
|
58 | + } |
|
59 | + } |
|
60 | + } else { |
|
61 | + $poolsToResync[] = $driverPool; |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + if ($item && $item->isHit() && \count($poolsToResync) < \count($this->clusterPools)) { |
|
66 | + foreach ($poolsToResync as $poolToResync) { |
|
67 | + $poolItem = $poolToResync->getItem($key); |
|
68 | + $poolItem->setEventManager($this->getEventManager()) |
|
69 | + ->set($item->get()) |
|
70 | + ->setHit($item->isHit()) |
|
71 | + ->setTags($item->getTags()) |
|
72 | + ->expiresAt($item->getExpirationDate()) |
|
73 | + ->setDriver($poolToResync); |
|
74 | + $poolToResync->save($poolItem); |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + return $this->getStandardizedItem($item ?? new Item($this, $key), $this); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @inheritDoc |
|
83 | + */ |
|
84 | + public function hasItem($key) |
|
85 | + { |
|
86 | + foreach ($this->clusterPools as $driverPool) { |
|
87 | + $poolItem = $driverPool->getItem($key); |
|
88 | + if ($poolItem->isHit()) { |
|
89 | + return true; |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + return false; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @inheritDoc |
|
98 | + */ |
|
99 | + public function clear() |
|
100 | + { |
|
101 | + $hasClearedOnce = false; |
|
102 | + foreach ($this->clusterPools as $driverPool) { |
|
103 | + if ($result = $driverPool->clear()) { |
|
104 | + $hasClearedOnce = $result; |
|
105 | + } |
|
106 | + } |
|
107 | + // Return true only if at least one backend confirmed the "clear" operation |
|
108 | + return $hasClearedOnce; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @inheritDoc |
|
113 | + */ |
|
114 | + public function deleteItem($key) |
|
115 | + { |
|
116 | + $hasDeletedOnce = false; |
|
117 | + foreach ($this->clusterPools as $driverPool) { |
|
118 | + if ($result = $driverPool->deleteItem($key)) { |
|
119 | + $hasDeletedOnce = $result; |
|
120 | + } |
|
121 | + } |
|
122 | + // Return true only if at least one backend confirmed the "clear" operation |
|
123 | + return $hasDeletedOnce; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @inheritDoc |
|
128 | + */ |
|
129 | + public function save(CacheItemInterface $item) |
|
130 | + { |
|
131 | + /** @var ExtendedCacheItemInterface $item */ |
|
132 | + $hasSavedOnce = false; |
|
133 | + foreach ($this->clusterPools as $driverPool) { |
|
134 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
135 | + if ($result = $driverPool->save($poolItem)) { |
|
136 | + $hasSavedOnce = $result; |
|
137 | + } |
|
138 | + } |
|
139 | + // Return true only if at least one backend confirmed the "commit" operation |
|
140 | + return $hasSavedOnce; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @inheritDoc |
|
145 | + */ |
|
146 | + public function saveDeferred(CacheItemInterface $item) |
|
147 | + { |
|
148 | + /** @var ExtendedCacheItemInterface $item */ |
|
149 | + $hasSavedOnce = false; |
|
150 | + foreach ($this->clusterPools as $driverPool) { |
|
151 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
152 | + if ($result = $driverPool->saveDeferred($poolItem)) { |
|
153 | + $hasSavedOnce = $result; |
|
154 | + } |
|
155 | + } |
|
156 | + // Return true only if at least one backend confirmed the "commit" operation |
|
157 | + return $hasSavedOnce; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @inheritDoc |
|
162 | + */ |
|
163 | + public function commit() |
|
164 | + { |
|
165 | + $hasCommitOnce = false; |
|
166 | + foreach ($this->clusterPools as $driverPool) { |
|
167 | + if ($result = $driverPool->commit()) { |
|
168 | + $hasCommitOnce = $result; |
|
169 | + } |
|
170 | + } |
|
171 | + // Return true only if at least one backend confirmed the "commit" operation |
|
172 | + return $hasCommitOnce; |
|
173 | + } |
|
174 | 174 | |
175 | 175 | |
176 | 176 | } |
@@ -27,177 +27,177 @@ |
||
27 | 27 | */ |
28 | 28 | class SemiReplicationCluster extends ClusterPoolAbstract |
29 | 29 | { |
30 | - /** |
|
31 | - * @inheritDoc |
|
32 | - */ |
|
33 | - public function getItem($key) |
|
34 | - { |
|
35 | - /** @var ExtendedCacheItemInterface $item */ |
|
36 | - $item = null; |
|
37 | - $eCount = 0; |
|
38 | - |
|
39 | - foreach ($this->clusterPools as $driverPool) { |
|
40 | - try { |
|
41 | - $poolItem = $driverPool->getItem($key); |
|
42 | - if ($poolItem->isHit()) { |
|
43 | - if (!$item) { |
|
44 | - $item = $poolItem; |
|
45 | - break; |
|
46 | - } |
|
47 | - } |
|
48 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
49 | - $eCount++; |
|
50 | - } |
|
51 | - } |
|
52 | - |
|
53 | - if (\count($this->clusterPools) <= $eCount) { |
|
54 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
55 | - } |
|
56 | - |
|
57 | - return $this->getStandardizedItem($item ?? new Item($this, $key), $this); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @inheritDoc |
|
62 | - */ |
|
63 | - public function hasItem($key) |
|
64 | - { |
|
65 | - $eCount = 0; |
|
66 | - foreach ($this->clusterPools as $driverPool) { |
|
67 | - try { |
|
68 | - $poolItem = $driverPool->getItem($key); |
|
69 | - if ($poolItem->isHit()) { |
|
70 | - return true; |
|
71 | - } |
|
72 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
73 | - $eCount++; |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - if (\count($this->clusterPools) <= $eCount) { |
|
78 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
79 | - } |
|
80 | - |
|
81 | - return false; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @inheritDoc |
|
86 | - */ |
|
87 | - public function clear() |
|
88 | - { |
|
89 | - $hasClearedOnce = false; |
|
90 | - $eCount = 0; |
|
91 | - |
|
92 | - foreach ($this->clusterPools as $driverPool) { |
|
93 | - try { |
|
94 | - if ($result = $driverPool->clear()) { |
|
95 | - $hasClearedOnce = $result; |
|
96 | - } |
|
97 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
98 | - $eCount++; |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - if (\count($this->clusterPools) <= $eCount) { |
|
103 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
104 | - } |
|
105 | - |
|
106 | - // Return true only if at least one backend confirmed the "clear" operation |
|
107 | - return $hasClearedOnce; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @inheritDoc |
|
112 | - */ |
|
113 | - public function deleteItem($key) |
|
114 | - { |
|
115 | - $hasDeletedOnce = false; |
|
116 | - $eCount = 0; |
|
117 | - |
|
118 | - foreach ($this->clusterPools as $driverPool) { |
|
119 | - try { |
|
120 | - if ($result = $driverPool->deleteItem($key)) { |
|
121 | - $hasDeletedOnce = $result; |
|
122 | - } |
|
123 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
124 | - $eCount++; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - if (\count($this->clusterPools) <= $eCount) { |
|
129 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
130 | - } |
|
131 | - // Return true only if at least one backend confirmed the "clear" operation |
|
132 | - return $hasDeletedOnce; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @inheritDoc |
|
137 | - */ |
|
138 | - public function save(CacheItemInterface $item) |
|
139 | - { |
|
140 | - /** @var ExtendedCacheItemInterface $item */ |
|
141 | - $hasSavedOnce = false; |
|
142 | - $eCount = 0; |
|
143 | - |
|
144 | - foreach ($this->clusterPools as $driverPool) { |
|
145 | - try { |
|
146 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
147 | - if ($result = $driverPool->save($poolItem)) { |
|
148 | - $hasSavedOnce = $result; |
|
149 | - } |
|
150 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
151 | - $eCount++; |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - if (\count($this->clusterPools) <= $eCount) { |
|
156 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
157 | - } |
|
158 | - // Return true only if at least one backend confirmed the "commit" operation |
|
159 | - return $hasSavedOnce; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @inheritDoc |
|
164 | - */ |
|
165 | - public function saveDeferred(CacheItemInterface $item) |
|
166 | - { |
|
167 | - /** @var ExtendedCacheItemInterface $item */ |
|
168 | - $hasSavedOnce = false; |
|
169 | - foreach ($this->clusterPools as $driverPool) { |
|
170 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
171 | - if ($result = $driverPool->saveDeferred($poolItem)) { |
|
172 | - $hasSavedOnce = $result; |
|
173 | - } |
|
174 | - } |
|
175 | - // Return true only if at least one backend confirmed the "commit" operation |
|
176 | - return $hasSavedOnce; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @inheritDoc |
|
181 | - */ |
|
182 | - public function commit() |
|
183 | - { |
|
184 | - $hasCommitOnce = false; |
|
185 | - $eCount = 0; |
|
186 | - |
|
187 | - foreach ($this->clusterPools as $driverPool) { |
|
188 | - try { |
|
189 | - if ($result = $driverPool->commit()) { |
|
190 | - $hasCommitOnce = $result; |
|
191 | - } |
|
192 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
193 | - $eCount++; |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - if (\count($this->clusterPools) <= $eCount) { |
|
198 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
199 | - } |
|
200 | - // Return true only if at least one backend confirmed the "commit" operation |
|
201 | - return $hasCommitOnce; |
|
202 | - } |
|
30 | + /** |
|
31 | + * @inheritDoc |
|
32 | + */ |
|
33 | + public function getItem($key) |
|
34 | + { |
|
35 | + /** @var ExtendedCacheItemInterface $item */ |
|
36 | + $item = null; |
|
37 | + $eCount = 0; |
|
38 | + |
|
39 | + foreach ($this->clusterPools as $driverPool) { |
|
40 | + try { |
|
41 | + $poolItem = $driverPool->getItem($key); |
|
42 | + if ($poolItem->isHit()) { |
|
43 | + if (!$item) { |
|
44 | + $item = $poolItem; |
|
45 | + break; |
|
46 | + } |
|
47 | + } |
|
48 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
49 | + $eCount++; |
|
50 | + } |
|
51 | + } |
|
52 | + |
|
53 | + if (\count($this->clusterPools) <= $eCount) { |
|
54 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
55 | + } |
|
56 | + |
|
57 | + return $this->getStandardizedItem($item ?? new Item($this, $key), $this); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @inheritDoc |
|
62 | + */ |
|
63 | + public function hasItem($key) |
|
64 | + { |
|
65 | + $eCount = 0; |
|
66 | + foreach ($this->clusterPools as $driverPool) { |
|
67 | + try { |
|
68 | + $poolItem = $driverPool->getItem($key); |
|
69 | + if ($poolItem->isHit()) { |
|
70 | + return true; |
|
71 | + } |
|
72 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
73 | + $eCount++; |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + if (\count($this->clusterPools) <= $eCount) { |
|
78 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
79 | + } |
|
80 | + |
|
81 | + return false; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @inheritDoc |
|
86 | + */ |
|
87 | + public function clear() |
|
88 | + { |
|
89 | + $hasClearedOnce = false; |
|
90 | + $eCount = 0; |
|
91 | + |
|
92 | + foreach ($this->clusterPools as $driverPool) { |
|
93 | + try { |
|
94 | + if ($result = $driverPool->clear()) { |
|
95 | + $hasClearedOnce = $result; |
|
96 | + } |
|
97 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
98 | + $eCount++; |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + if (\count($this->clusterPools) <= $eCount) { |
|
103 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
104 | + } |
|
105 | + |
|
106 | + // Return true only if at least one backend confirmed the "clear" operation |
|
107 | + return $hasClearedOnce; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @inheritDoc |
|
112 | + */ |
|
113 | + public function deleteItem($key) |
|
114 | + { |
|
115 | + $hasDeletedOnce = false; |
|
116 | + $eCount = 0; |
|
117 | + |
|
118 | + foreach ($this->clusterPools as $driverPool) { |
|
119 | + try { |
|
120 | + if ($result = $driverPool->deleteItem($key)) { |
|
121 | + $hasDeletedOnce = $result; |
|
122 | + } |
|
123 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
124 | + $eCount++; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + if (\count($this->clusterPools) <= $eCount) { |
|
129 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
130 | + } |
|
131 | + // Return true only if at least one backend confirmed the "clear" operation |
|
132 | + return $hasDeletedOnce; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @inheritDoc |
|
137 | + */ |
|
138 | + public function save(CacheItemInterface $item) |
|
139 | + { |
|
140 | + /** @var ExtendedCacheItemInterface $item */ |
|
141 | + $hasSavedOnce = false; |
|
142 | + $eCount = 0; |
|
143 | + |
|
144 | + foreach ($this->clusterPools as $driverPool) { |
|
145 | + try { |
|
146 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
147 | + if ($result = $driverPool->save($poolItem)) { |
|
148 | + $hasSavedOnce = $result; |
|
149 | + } |
|
150 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
151 | + $eCount++; |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + if (\count($this->clusterPools) <= $eCount) { |
|
156 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
157 | + } |
|
158 | + // Return true only if at least one backend confirmed the "commit" operation |
|
159 | + return $hasSavedOnce; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @inheritDoc |
|
164 | + */ |
|
165 | + public function saveDeferred(CacheItemInterface $item) |
|
166 | + { |
|
167 | + /** @var ExtendedCacheItemInterface $item */ |
|
168 | + $hasSavedOnce = false; |
|
169 | + foreach ($this->clusterPools as $driverPool) { |
|
170 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
171 | + if ($result = $driverPool->saveDeferred($poolItem)) { |
|
172 | + $hasSavedOnce = $result; |
|
173 | + } |
|
174 | + } |
|
175 | + // Return true only if at least one backend confirmed the "commit" operation |
|
176 | + return $hasSavedOnce; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @inheritDoc |
|
181 | + */ |
|
182 | + public function commit() |
|
183 | + { |
|
184 | + $hasCommitOnce = false; |
|
185 | + $eCount = 0; |
|
186 | + |
|
187 | + foreach ($this->clusterPools as $driverPool) { |
|
188 | + try { |
|
189 | + if ($result = $driverPool->commit()) { |
|
190 | + $hasCommitOnce = $result; |
|
191 | + } |
|
192 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
193 | + $eCount++; |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + if (\count($this->clusterPools) <= $eCount) { |
|
198 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
199 | + } |
|
200 | + // Return true only if at least one backend confirmed the "commit" operation |
|
201 | + return $hasCommitOnce; |
|
202 | + } |
|
203 | 203 | } |
@@ -33,138 +33,138 @@ |
||
33 | 33 | */ |
34 | 34 | class MasterSlaveReplicationCluster extends ClusterPoolAbstract |
35 | 35 | { |
36 | - /** |
|
37 | - * MasterSlaveReplicationCluster constructor. |
|
38 | - * @param string $clusterName |
|
39 | - * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
40 | - * @throws PhpfastcacheInvalidArgumentException |
|
41 | - * @throws PhpfastcacheDriverCheckException |
|
42 | - * @throws PhpfastcacheDriverConnectException |
|
43 | - * @throws PhpfastcacheInvalidConfigurationException |
|
44 | - * @throws ReflectionException |
|
45 | - */ |
|
46 | - public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
47 | - { |
|
48 | - if (\count($driverPools) !== 2) { |
|
49 | - throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
50 | - } |
|
51 | - |
|
52 | - parent::__construct($clusterName, ...$driverPools); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @inheritDoc |
|
57 | - */ |
|
58 | - public function getItem($key) |
|
59 | - { |
|
60 | - return $this->getStandardizedItem( |
|
61 | - $this->makeOperation( |
|
62 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
63 | - return $pool->getItem($key); |
|
64 | - } |
|
65 | - ) ?? new Item($this, $key), |
|
66 | - $this |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param callable $operation |
|
72 | - * @return mixed |
|
73 | - * @throws PhpfastcacheReplicationException |
|
74 | - */ |
|
75 | - protected function makeOperation(callable $operation) |
|
76 | - { |
|
77 | - try { |
|
78 | - return $operation($this->getMasterPool()); |
|
79 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
80 | - try { |
|
81 | - $this->eventManager->dispatch( |
|
82 | - 'CacheReplicationSlaveFallback', |
|
83 | - $this, |
|
84 | - \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
85 | - ); |
|
86 | - return $operation($this->getSlavePool()); |
|
87 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
88 | - throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @return ExtendedCacheItemPoolInterface |
|
95 | - */ |
|
96 | - protected function getMasterPool(): ExtendedCacheItemPoolInterface |
|
97 | - { |
|
98 | - return $this->clusterPools[0]; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return ExtendedCacheItemPoolInterface |
|
103 | - */ |
|
104 | - protected function getSlavePool(): ExtendedCacheItemPoolInterface |
|
105 | - { |
|
106 | - return $this->clusterPools[1]; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @inheritDoc |
|
111 | - */ |
|
112 | - public function hasItem($key) |
|
113 | - { |
|
114 | - return $this->makeOperation( |
|
115 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
116 | - return $pool->hasItem($key); |
|
117 | - } |
|
118 | - ); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @inheritDoc |
|
123 | - */ |
|
124 | - public function clear() |
|
125 | - { |
|
126 | - return $this->makeOperation( |
|
127 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
128 | - return $pool->clear(); |
|
129 | - } |
|
130 | - ); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @inheritDoc |
|
135 | - */ |
|
136 | - public function deleteItem($key) |
|
137 | - { |
|
138 | - return $this->makeOperation( |
|
139 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
140 | - return $pool->deleteItem($key); |
|
141 | - } |
|
142 | - ); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @inheritDoc |
|
147 | - */ |
|
148 | - public function save(CacheItemInterface $item) |
|
149 | - { |
|
150 | - return $this->makeOperation( |
|
151 | - function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
152 | - $item->setHit(true); |
|
153 | - return $pool->save($this->getStandardizedItem($item, $pool)); |
|
154 | - } |
|
155 | - ); |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * @inheritDoc |
|
161 | - */ |
|
162 | - public function commit() |
|
163 | - { |
|
164 | - return $this->makeOperation( |
|
165 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
166 | - return $pool->commit(); |
|
167 | - } |
|
168 | - ); |
|
169 | - } |
|
36 | + /** |
|
37 | + * MasterSlaveReplicationCluster constructor. |
|
38 | + * @param string $clusterName |
|
39 | + * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
40 | + * @throws PhpfastcacheInvalidArgumentException |
|
41 | + * @throws PhpfastcacheDriverCheckException |
|
42 | + * @throws PhpfastcacheDriverConnectException |
|
43 | + * @throws PhpfastcacheInvalidConfigurationException |
|
44 | + * @throws ReflectionException |
|
45 | + */ |
|
46 | + public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
47 | + { |
|
48 | + if (\count($driverPools) !== 2) { |
|
49 | + throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
50 | + } |
|
51 | + |
|
52 | + parent::__construct($clusterName, ...$driverPools); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @inheritDoc |
|
57 | + */ |
|
58 | + public function getItem($key) |
|
59 | + { |
|
60 | + return $this->getStandardizedItem( |
|
61 | + $this->makeOperation( |
|
62 | + static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
63 | + return $pool->getItem($key); |
|
64 | + } |
|
65 | + ) ?? new Item($this, $key), |
|
66 | + $this |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param callable $operation |
|
72 | + * @return mixed |
|
73 | + * @throws PhpfastcacheReplicationException |
|
74 | + */ |
|
75 | + protected function makeOperation(callable $operation) |
|
76 | + { |
|
77 | + try { |
|
78 | + return $operation($this->getMasterPool()); |
|
79 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
80 | + try { |
|
81 | + $this->eventManager->dispatch( |
|
82 | + 'CacheReplicationSlaveFallback', |
|
83 | + $this, |
|
84 | + \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
85 | + ); |
|
86 | + return $operation($this->getSlavePool()); |
|
87 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
88 | + throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @return ExtendedCacheItemPoolInterface |
|
95 | + */ |
|
96 | + protected function getMasterPool(): ExtendedCacheItemPoolInterface |
|
97 | + { |
|
98 | + return $this->clusterPools[0]; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return ExtendedCacheItemPoolInterface |
|
103 | + */ |
|
104 | + protected function getSlavePool(): ExtendedCacheItemPoolInterface |
|
105 | + { |
|
106 | + return $this->clusterPools[1]; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @inheritDoc |
|
111 | + */ |
|
112 | + public function hasItem($key) |
|
113 | + { |
|
114 | + return $this->makeOperation( |
|
115 | + static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
116 | + return $pool->hasItem($key); |
|
117 | + } |
|
118 | + ); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @inheritDoc |
|
123 | + */ |
|
124 | + public function clear() |
|
125 | + { |
|
126 | + return $this->makeOperation( |
|
127 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
128 | + return $pool->clear(); |
|
129 | + } |
|
130 | + ); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @inheritDoc |
|
135 | + */ |
|
136 | + public function deleteItem($key) |
|
137 | + { |
|
138 | + return $this->makeOperation( |
|
139 | + static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
140 | + return $pool->deleteItem($key); |
|
141 | + } |
|
142 | + ); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @inheritDoc |
|
147 | + */ |
|
148 | + public function save(CacheItemInterface $item) |
|
149 | + { |
|
150 | + return $this->makeOperation( |
|
151 | + function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
152 | + $item->setHit(true); |
|
153 | + return $pool->save($this->getStandardizedItem($item, $pool)); |
|
154 | + } |
|
155 | + ); |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * @inheritDoc |
|
161 | + */ |
|
162 | + public function commit() |
|
163 | + { |
|
164 | + return $this->makeOperation( |
|
165 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
166 | + return $pool->commit(); |
|
167 | + } |
|
168 | + ); |
|
169 | + } |
|
170 | 170 | } |