@@ -20,7 +20,7 @@ |
||
20 | 20 | |
21 | 21 | interface AggregatablePoolInterface extends ExtendedCacheItemPoolInterface |
22 | 22 | { |
23 | - public function isAggregatedBy(): ?ClusterPoolInterface; |
|
23 | + public function isAggregatedBy(): ?ClusterPoolInterface; |
|
24 | 24 | |
25 | - public function setAggregatedBy(ClusterPoolInterface $clusterPool): static; |
|
25 | + public function setAggregatedBy(ClusterPoolInterface $clusterPool): static; |
|
26 | 26 | } |
@@ -20,8 +20,8 @@ |
||
20 | 20 | |
21 | 21 | class Item extends ItemAbstract |
22 | 22 | { |
23 | - protected function getDriverClass(): string |
|
24 | - { |
|
25 | - return Driver::class; |
|
26 | - } |
|
23 | + protected function getDriverClass(): string |
|
24 | + { |
|
25 | + return Driver::class; |
|
26 | + } |
|
27 | 27 | } |
@@ -24,165 +24,165 @@ |
||
24 | 24 | |
25 | 25 | class Driver extends ClusterPoolAbstract |
26 | 26 | { |
27 | - /** |
|
28 | - * @inheritDoc |
|
29 | - */ |
|
30 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
31 | - { |
|
32 | - /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */ |
|
33 | - $poolsToResync = []; |
|
34 | - /** @var ?ExtendedCacheItemInterface $item */ |
|
35 | - $item = null; |
|
36 | - |
|
37 | - foreach ($this->clusterPools as $driverPool) { |
|
38 | - $poolItem = $driverPool->getItem($key); |
|
39 | - if ($poolItem->isHit()) { |
|
40 | - if (!$item) { |
|
41 | - $item = $poolItem; |
|
42 | - continue; |
|
43 | - } |
|
44 | - |
|
45 | - $itemData = $item->get(); |
|
46 | - $poolItemData = $poolItem->get(); |
|
47 | - |
|
48 | - if (\is_object($itemData)) { |
|
49 | - if ($item->get() != $poolItemData) { |
|
50 | - $poolsToResync[] = $driverPool; |
|
51 | - } |
|
52 | - } else { |
|
53 | - if ($item->get() !== $poolItemData) { |
|
54 | - $poolsToResync[] = $driverPool; |
|
55 | - } |
|
56 | - } |
|
57 | - } else { |
|
58 | - $poolsToResync[] = $driverPool; |
|
59 | - } |
|
60 | - } |
|
61 | - |
|
62 | - $this->resynchronizePool($poolsToResync, $key, $item); |
|
63 | - |
|
64 | - if ($item === null) { |
|
65 | - $item = new Item($this, $key, $this->getEventManager()); |
|
66 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
67 | - } |
|
68 | - |
|
69 | - return $this->getStandardizedItem($item, $this); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @inheritDoc |
|
74 | - */ |
|
75 | - public function hasItem(string $key): bool |
|
76 | - { |
|
77 | - foreach ($this->clusterPools as $driverPool) { |
|
78 | - $poolItem = $driverPool->getItem($key); |
|
79 | - if ($poolItem->isHit()) { |
|
80 | - return true; |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - return false; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @inheritDoc |
|
89 | - */ |
|
90 | - public function clear(): bool |
|
91 | - { |
|
92 | - $hasClearedOnce = false; |
|
93 | - foreach ($this->clusterPools as $driverPool) { |
|
94 | - if ($result = $driverPool->clear()) { |
|
95 | - $hasClearedOnce = $result; |
|
96 | - } |
|
97 | - } |
|
98 | - // Return true only if at least one backend confirmed the "clear" operation |
|
99 | - return $hasClearedOnce; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @inheritDoc |
|
104 | - */ |
|
105 | - public function deleteItem(string $key): bool |
|
106 | - { |
|
107 | - $hasDeletedOnce = false; |
|
108 | - foreach ($this->clusterPools as $driverPool) { |
|
109 | - if ($result = $driverPool->deleteItem($key)) { |
|
110 | - $hasDeletedOnce = $result; |
|
111 | - } |
|
112 | - } |
|
113 | - // Return true only if at least one backend confirmed the "clear" operation |
|
114 | - return $hasDeletedOnce; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @inheritDoc |
|
119 | - */ |
|
120 | - public function save(CacheItemInterface $item): bool |
|
121 | - { |
|
122 | - /** @var ExtendedCacheItemInterface $item */ |
|
123 | - $hasSavedOnce = false; |
|
124 | - foreach ($this->clusterPools as $driverPool) { |
|
125 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
126 | - if ($result = $driverPool->save($poolItem)) { |
|
127 | - $hasSavedOnce = $result; |
|
128 | - } |
|
129 | - } |
|
130 | - // Return true only if at least one backend confirmed the "commit" operation |
|
131 | - return $hasSavedOnce; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @inheritDoc |
|
136 | - */ |
|
137 | - public function saveDeferred(CacheItemInterface $item): bool |
|
138 | - { |
|
139 | - /** @var ExtendedCacheItemInterface $item */ |
|
140 | - $hasSavedOnce = false; |
|
141 | - foreach ($this->clusterPools as $driverPool) { |
|
142 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
143 | - if ($result = $driverPool->saveDeferred($poolItem)) { |
|
144 | - $hasSavedOnce = $result; |
|
145 | - } |
|
146 | - } |
|
147 | - // Return true only if at least one backend confirmed the "commit" operation |
|
148 | - return $hasSavedOnce; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @inheritDoc |
|
153 | - */ |
|
154 | - public function commit(): bool |
|
155 | - { |
|
156 | - $hasCommitOnce = false; |
|
157 | - foreach ($this->clusterPools as $driverPool) { |
|
158 | - if ($result = $driverPool->commit()) { |
|
159 | - $hasCommitOnce = $result; |
|
160 | - } |
|
161 | - } |
|
162 | - // Return true only if at least one backend confirmed the "commit" operation |
|
163 | - return $hasCommitOnce; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param ExtendedCacheItemPoolInterface[] $poolsToResynchronize |
|
168 | - * @param string $key |
|
169 | - * @param ?ExtendedCacheItemInterface $item |
|
170 | - * @return void |
|
171 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
|
172 | - */ |
|
173 | - protected function resynchronizePool(array $poolsToResynchronize, string $key, ?ExtendedCacheItemInterface $item): void |
|
174 | - { |
|
175 | - if ($item && $item->isHit() && \count($poolsToResynchronize) < \count($this->clusterPools)) { |
|
176 | - foreach ($poolsToResynchronize as $poolToResynchronize) { |
|
177 | - $poolItem = $poolToResynchronize->getItem($key); |
|
178 | - $poolItem->setEventManager($this->getEventManager()) |
|
179 | - ->set($item->get()) |
|
180 | - ->setHit($item->isHit()) |
|
181 | - ->setTags($item->getTags()) |
|
182 | - ->expiresAt($item->getExpirationDate()) |
|
183 | - ->setDriver($poolToResynchronize); |
|
184 | - $poolToResynchronize->save($poolItem); |
|
185 | - } |
|
186 | - } |
|
187 | - } |
|
27 | + /** |
|
28 | + * @inheritDoc |
|
29 | + */ |
|
30 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
31 | + { |
|
32 | + /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */ |
|
33 | + $poolsToResync = []; |
|
34 | + /** @var ?ExtendedCacheItemInterface $item */ |
|
35 | + $item = null; |
|
36 | + |
|
37 | + foreach ($this->clusterPools as $driverPool) { |
|
38 | + $poolItem = $driverPool->getItem($key); |
|
39 | + if ($poolItem->isHit()) { |
|
40 | + if (!$item) { |
|
41 | + $item = $poolItem; |
|
42 | + continue; |
|
43 | + } |
|
44 | + |
|
45 | + $itemData = $item->get(); |
|
46 | + $poolItemData = $poolItem->get(); |
|
47 | + |
|
48 | + if (\is_object($itemData)) { |
|
49 | + if ($item->get() != $poolItemData) { |
|
50 | + $poolsToResync[] = $driverPool; |
|
51 | + } |
|
52 | + } else { |
|
53 | + if ($item->get() !== $poolItemData) { |
|
54 | + $poolsToResync[] = $driverPool; |
|
55 | + } |
|
56 | + } |
|
57 | + } else { |
|
58 | + $poolsToResync[] = $driverPool; |
|
59 | + } |
|
60 | + } |
|
61 | + |
|
62 | + $this->resynchronizePool($poolsToResync, $key, $item); |
|
63 | + |
|
64 | + if ($item === null) { |
|
65 | + $item = new Item($this, $key, $this->getEventManager()); |
|
66 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
67 | + } |
|
68 | + |
|
69 | + return $this->getStandardizedItem($item, $this); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @inheritDoc |
|
74 | + */ |
|
75 | + public function hasItem(string $key): bool |
|
76 | + { |
|
77 | + foreach ($this->clusterPools as $driverPool) { |
|
78 | + $poolItem = $driverPool->getItem($key); |
|
79 | + if ($poolItem->isHit()) { |
|
80 | + return true; |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + return false; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @inheritDoc |
|
89 | + */ |
|
90 | + public function clear(): bool |
|
91 | + { |
|
92 | + $hasClearedOnce = false; |
|
93 | + foreach ($this->clusterPools as $driverPool) { |
|
94 | + if ($result = $driverPool->clear()) { |
|
95 | + $hasClearedOnce = $result; |
|
96 | + } |
|
97 | + } |
|
98 | + // Return true only if at least one backend confirmed the "clear" operation |
|
99 | + return $hasClearedOnce; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @inheritDoc |
|
104 | + */ |
|
105 | + public function deleteItem(string $key): bool |
|
106 | + { |
|
107 | + $hasDeletedOnce = false; |
|
108 | + foreach ($this->clusterPools as $driverPool) { |
|
109 | + if ($result = $driverPool->deleteItem($key)) { |
|
110 | + $hasDeletedOnce = $result; |
|
111 | + } |
|
112 | + } |
|
113 | + // Return true only if at least one backend confirmed the "clear" operation |
|
114 | + return $hasDeletedOnce; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @inheritDoc |
|
119 | + */ |
|
120 | + public function save(CacheItemInterface $item): bool |
|
121 | + { |
|
122 | + /** @var ExtendedCacheItemInterface $item */ |
|
123 | + $hasSavedOnce = false; |
|
124 | + foreach ($this->clusterPools as $driverPool) { |
|
125 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
126 | + if ($result = $driverPool->save($poolItem)) { |
|
127 | + $hasSavedOnce = $result; |
|
128 | + } |
|
129 | + } |
|
130 | + // Return true only if at least one backend confirmed the "commit" operation |
|
131 | + return $hasSavedOnce; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @inheritDoc |
|
136 | + */ |
|
137 | + public function saveDeferred(CacheItemInterface $item): bool |
|
138 | + { |
|
139 | + /** @var ExtendedCacheItemInterface $item */ |
|
140 | + $hasSavedOnce = false; |
|
141 | + foreach ($this->clusterPools as $driverPool) { |
|
142 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
143 | + if ($result = $driverPool->saveDeferred($poolItem)) { |
|
144 | + $hasSavedOnce = $result; |
|
145 | + } |
|
146 | + } |
|
147 | + // Return true only if at least one backend confirmed the "commit" operation |
|
148 | + return $hasSavedOnce; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @inheritDoc |
|
153 | + */ |
|
154 | + public function commit(): bool |
|
155 | + { |
|
156 | + $hasCommitOnce = false; |
|
157 | + foreach ($this->clusterPools as $driverPool) { |
|
158 | + if ($result = $driverPool->commit()) { |
|
159 | + $hasCommitOnce = $result; |
|
160 | + } |
|
161 | + } |
|
162 | + // Return true only if at least one backend confirmed the "commit" operation |
|
163 | + return $hasCommitOnce; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param ExtendedCacheItemPoolInterface[] $poolsToResynchronize |
|
168 | + * @param string $key |
|
169 | + * @param ?ExtendedCacheItemInterface $item |
|
170 | + * @return void |
|
171 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
|
172 | + */ |
|
173 | + protected function resynchronizePool(array $poolsToResynchronize, string $key, ?ExtendedCacheItemInterface $item): void |
|
174 | + { |
|
175 | + if ($item && $item->isHit() && \count($poolsToResynchronize) < \count($this->clusterPools)) { |
|
176 | + foreach ($poolsToResynchronize as $poolToResynchronize) { |
|
177 | + $poolItem = $poolToResynchronize->getItem($key); |
|
178 | + $poolItem->setEventManager($this->getEventManager()) |
|
179 | + ->set($item->get()) |
|
180 | + ->setHit($item->isHit()) |
|
181 | + ->setTags($item->getTags()) |
|
182 | + ->expiresAt($item->getExpirationDate()) |
|
183 | + ->setDriver($poolToResynchronize); |
|
184 | + $poolToResynchronize->save($poolItem); |
|
185 | + } |
|
186 | + } |
|
187 | + } |
|
188 | 188 | } |
@@ -49,12 +49,14 @@ |
||
49 | 49 | if ($item->get() != $poolItemData) { |
50 | 50 | $poolsToResync[] = $driverPool; |
51 | 51 | } |
52 | - } else { |
|
52 | + } |
|
53 | + else { |
|
53 | 54 | if ($item->get() !== $poolItemData) { |
54 | 55 | $poolsToResync[] = $driverPool; |
55 | 56 | } |
56 | 57 | } |
57 | - } else { |
|
58 | + } |
|
59 | + else { |
|
58 | 60 | $poolsToResync[] = $driverPool; |
59 | 61 | } |
60 | 62 | } |
@@ -25,41 +25,41 @@ |
||
25 | 25 | |
26 | 26 | class Driver extends MasterSlaveReplicationDriver |
27 | 27 | { |
28 | - /** |
|
29 | - * RandomReplicationCluster constructor. |
|
30 | - * @param string $clusterName |
|
31 | - * @param EventManagerInterface $em |
|
32 | - * @param AggregatablePoolInterface ...$driverPools |
|
33 | - * @throws ReflectionException |
|
34 | - */ |
|
35 | - public function __construct( |
|
36 | - string $clusterName, |
|
37 | - EventManagerInterface $em, |
|
38 | - AggregatablePoolInterface ...$driverPools |
|
39 | - ) { |
|
40 | - /** Straight call to ClusterPoolAbstract constructor */ |
|
41 | - (new ReflectionMethod( |
|
42 | - \get_parent_class(\get_parent_class($this)), |
|
43 | - __FUNCTION__ |
|
44 | - ))->invoke($this, $clusterName, $em, ...$driverPools); |
|
28 | + /** |
|
29 | + * RandomReplicationCluster constructor. |
|
30 | + * @param string $clusterName |
|
31 | + * @param EventManagerInterface $em |
|
32 | + * @param AggregatablePoolInterface ...$driverPools |
|
33 | + * @throws ReflectionException |
|
34 | + */ |
|
35 | + public function __construct( |
|
36 | + string $clusterName, |
|
37 | + EventManagerInterface $em, |
|
38 | + AggregatablePoolInterface ...$driverPools |
|
39 | + ) { |
|
40 | + /** Straight call to ClusterPoolAbstract constructor */ |
|
41 | + (new ReflectionMethod( |
|
42 | + \get_parent_class(\get_parent_class($this)), |
|
43 | + __FUNCTION__ |
|
44 | + ))->invoke($this, $clusterName, $em, ...$driverPools); |
|
45 | 45 | |
46 | - $randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)]; |
|
46 | + $randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)]; |
|
47 | 47 | |
48 | - $this->eventManager->dispatch( |
|
49 | - Event::CACHE_REPLICATION_RANDOM_POOL_CHOSEN, |
|
50 | - $this, |
|
51 | - $randomPool |
|
52 | - ); |
|
48 | + $this->eventManager->dispatch( |
|
49 | + Event::CACHE_REPLICATION_RANDOM_POOL_CHOSEN, |
|
50 | + $this, |
|
51 | + $randomPool |
|
52 | + ); |
|
53 | 53 | |
54 | - $this->clusterPools = [$randomPool]; |
|
55 | - } |
|
54 | + $this->clusterPools = [$randomPool]; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param callable $operation |
|
59 | - * @return mixed |
|
60 | - */ |
|
61 | - protected function makeOperation(callable $operation) |
|
62 | - { |
|
63 | - return $operation($this->getMasterPool()); |
|
64 | - } |
|
57 | + /** |
|
58 | + * @param callable $operation |
|
59 | + * @return mixed |
|
60 | + */ |
|
61 | + protected function makeOperation(callable $operation) |
|
62 | + { |
|
63 | + return $operation($this->getMasterPool()); |
|
64 | + } |
|
65 | 65 | } |
@@ -58,8 +58,7 @@ |
||
58 | 58 | * @param callable $operation |
59 | 59 | * @return mixed |
60 | 60 | */ |
61 | - protected function makeOperation(callable $operation) |
|
62 | - { |
|
61 | + protected function makeOperation(callable $operation) { |
|
63 | 62 | return $operation($this->getMasterPool()); |
64 | 63 | } |
65 | 64 | } |
@@ -25,173 +25,173 @@ |
||
25 | 25 | |
26 | 26 | class Driver extends ClusterPoolAbstract |
27 | 27 | { |
28 | - /** |
|
29 | - * @inheritDoc |
|
30 | - * @throws InvalidArgumentException |
|
31 | - * @throws PhpfastcacheReplicationException |
|
32 | - */ |
|
33 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
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 (!$item && $poolItem->isHit()) { |
|
43 | - $item = $poolItem; |
|
44 | - break; |
|
45 | - } |
|
46 | - } catch (PhpfastcacheExceptionInterface) { |
|
47 | - $eCount++; |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - if (\count($this->clusterPools) <= $eCount) { |
|
52 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
53 | - } |
|
54 | - |
|
55 | - if ($item === null) { |
|
56 | - $item = new Item($this, $key, $this->getEventManager()); |
|
57 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
58 | - } |
|
59 | - |
|
60 | - return $this->getStandardizedItem($item, $this); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @inheritDoc |
|
65 | - * @throws PhpfastcacheReplicationException |
|
66 | - */ |
|
67 | - public function hasItem(string $key): bool |
|
68 | - { |
|
69 | - $eCount = 0; |
|
70 | - foreach ($this->clusterPools as $driverPool) { |
|
71 | - try { |
|
72 | - $poolItem = $driverPool->getItem($key); |
|
73 | - if ($poolItem->isHit()) { |
|
74 | - return true; |
|
75 | - } |
|
76 | - } catch (PhpfastcacheExceptionInterface) { |
|
77 | - $eCount++; |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - if (\count($this->clusterPools) <= $eCount) { |
|
82 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
83 | - } |
|
84 | - |
|
85 | - return false; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @inheritDoc |
|
90 | - * @throws PhpfastcacheReplicationException |
|
91 | - */ |
|
92 | - public function clear(): bool |
|
93 | - { |
|
94 | - $hasClearedOnce = false; |
|
95 | - $eCount = 0; |
|
96 | - |
|
97 | - foreach ($this->clusterPools as $driverPool) { |
|
98 | - try { |
|
99 | - if ($result = $driverPool->clear()) { |
|
100 | - $hasClearedOnce = $result; |
|
101 | - } |
|
102 | - } catch (PhpfastcacheExceptionInterface) { |
|
103 | - $eCount++; |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - if (\count($this->clusterPools) <= $eCount) { |
|
108 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
109 | - } |
|
110 | - |
|
111 | - // Return true only if at least one backend confirmed the "clear" operation |
|
112 | - return $hasClearedOnce; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @inheritDoc |
|
117 | - * @throws PhpfastcacheReplicationException |
|
118 | - * @throws InvalidArgumentException |
|
119 | - */ |
|
120 | - public function deleteItem(string $key): bool |
|
121 | - { |
|
122 | - $hasDeletedOnce = false; |
|
123 | - $eCount = 0; |
|
124 | - |
|
125 | - foreach ($this->clusterPools as $driverPool) { |
|
126 | - try { |
|
127 | - if ($result = $driverPool->deleteItem($key)) { |
|
128 | - $hasDeletedOnce = $result; |
|
129 | - } |
|
130 | - } catch (PhpfastcacheExceptionInterface) { |
|
131 | - $eCount++; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - if (\count($this->clusterPools) <= $eCount) { |
|
136 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
137 | - } |
|
138 | - // Return true only if at least one backend confirmed the "clear" operation |
|
139 | - return $hasDeletedOnce; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param CacheItemInterface $item |
|
144 | - * @return bool |
|
145 | - * @throws InvalidArgumentException |
|
146 | - * @throws PhpfastcacheReplicationException |
|
147 | - */ |
|
148 | - public function save(CacheItemInterface $item): bool |
|
149 | - { |
|
150 | - /** @var ExtendedCacheItemInterface $item */ |
|
151 | - $hasSavedOnce = false; |
|
152 | - $eCount = 0; |
|
153 | - |
|
154 | - foreach ($this->clusterPools as $driverPool) { |
|
155 | - try { |
|
156 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
157 | - if ($result = $driverPool->save($poolItem)) { |
|
158 | - $hasSavedOnce = $result; |
|
159 | - } |
|
160 | - } catch (PhpfastcacheExceptionInterface) { |
|
161 | - $eCount++; |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - if (\count($this->clusterPools) <= $eCount) { |
|
166 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
167 | - } |
|
168 | - // Return true only if at least one backend confirmed the "commit" operation |
|
169 | - return $hasSavedOnce; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @inheritDoc |
|
174 | - * @throws PhpfastcacheReplicationException |
|
175 | - */ |
|
176 | - public function commit(): bool |
|
177 | - { |
|
178 | - $hasCommitOnce = false; |
|
179 | - $eCount = 0; |
|
180 | - |
|
181 | - foreach ($this->clusterPools as $driverPool) { |
|
182 | - try { |
|
183 | - if ($result = $driverPool->commit()) { |
|
184 | - $hasCommitOnce = $result; |
|
185 | - } |
|
186 | - } catch (PhpfastcacheExceptionInterface) { |
|
187 | - $eCount++; |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - if (\count($this->clusterPools) <= $eCount) { |
|
192 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
193 | - } |
|
194 | - // Return true only if at least one backend confirmed the "commit" operation |
|
195 | - return $hasCommitOnce; |
|
196 | - } |
|
28 | + /** |
|
29 | + * @inheritDoc |
|
30 | + * @throws InvalidArgumentException |
|
31 | + * @throws PhpfastcacheReplicationException |
|
32 | + */ |
|
33 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
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 (!$item && $poolItem->isHit()) { |
|
43 | + $item = $poolItem; |
|
44 | + break; |
|
45 | + } |
|
46 | + } catch (PhpfastcacheExceptionInterface) { |
|
47 | + $eCount++; |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + if (\count($this->clusterPools) <= $eCount) { |
|
52 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
53 | + } |
|
54 | + |
|
55 | + if ($item === null) { |
|
56 | + $item = new Item($this, $key, $this->getEventManager()); |
|
57 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
58 | + } |
|
59 | + |
|
60 | + return $this->getStandardizedItem($item, $this); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @inheritDoc |
|
65 | + * @throws PhpfastcacheReplicationException |
|
66 | + */ |
|
67 | + public function hasItem(string $key): bool |
|
68 | + { |
|
69 | + $eCount = 0; |
|
70 | + foreach ($this->clusterPools as $driverPool) { |
|
71 | + try { |
|
72 | + $poolItem = $driverPool->getItem($key); |
|
73 | + if ($poolItem->isHit()) { |
|
74 | + return true; |
|
75 | + } |
|
76 | + } catch (PhpfastcacheExceptionInterface) { |
|
77 | + $eCount++; |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + if (\count($this->clusterPools) <= $eCount) { |
|
82 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
83 | + } |
|
84 | + |
|
85 | + return false; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @inheritDoc |
|
90 | + * @throws PhpfastcacheReplicationException |
|
91 | + */ |
|
92 | + public function clear(): bool |
|
93 | + { |
|
94 | + $hasClearedOnce = false; |
|
95 | + $eCount = 0; |
|
96 | + |
|
97 | + foreach ($this->clusterPools as $driverPool) { |
|
98 | + try { |
|
99 | + if ($result = $driverPool->clear()) { |
|
100 | + $hasClearedOnce = $result; |
|
101 | + } |
|
102 | + } catch (PhpfastcacheExceptionInterface) { |
|
103 | + $eCount++; |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + if (\count($this->clusterPools) <= $eCount) { |
|
108 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
109 | + } |
|
110 | + |
|
111 | + // Return true only if at least one backend confirmed the "clear" operation |
|
112 | + return $hasClearedOnce; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @inheritDoc |
|
117 | + * @throws PhpfastcacheReplicationException |
|
118 | + * @throws InvalidArgumentException |
|
119 | + */ |
|
120 | + public function deleteItem(string $key): bool |
|
121 | + { |
|
122 | + $hasDeletedOnce = false; |
|
123 | + $eCount = 0; |
|
124 | + |
|
125 | + foreach ($this->clusterPools as $driverPool) { |
|
126 | + try { |
|
127 | + if ($result = $driverPool->deleteItem($key)) { |
|
128 | + $hasDeletedOnce = $result; |
|
129 | + } |
|
130 | + } catch (PhpfastcacheExceptionInterface) { |
|
131 | + $eCount++; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + if (\count($this->clusterPools) <= $eCount) { |
|
136 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
137 | + } |
|
138 | + // Return true only if at least one backend confirmed the "clear" operation |
|
139 | + return $hasDeletedOnce; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param CacheItemInterface $item |
|
144 | + * @return bool |
|
145 | + * @throws InvalidArgumentException |
|
146 | + * @throws PhpfastcacheReplicationException |
|
147 | + */ |
|
148 | + public function save(CacheItemInterface $item): bool |
|
149 | + { |
|
150 | + /** @var ExtendedCacheItemInterface $item */ |
|
151 | + $hasSavedOnce = false; |
|
152 | + $eCount = 0; |
|
153 | + |
|
154 | + foreach ($this->clusterPools as $driverPool) { |
|
155 | + try { |
|
156 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
157 | + if ($result = $driverPool->save($poolItem)) { |
|
158 | + $hasSavedOnce = $result; |
|
159 | + } |
|
160 | + } catch (PhpfastcacheExceptionInterface) { |
|
161 | + $eCount++; |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + if (\count($this->clusterPools) <= $eCount) { |
|
166 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
167 | + } |
|
168 | + // Return true only if at least one backend confirmed the "commit" operation |
|
169 | + return $hasSavedOnce; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @inheritDoc |
|
174 | + * @throws PhpfastcacheReplicationException |
|
175 | + */ |
|
176 | + public function commit(): bool |
|
177 | + { |
|
178 | + $hasCommitOnce = false; |
|
179 | + $eCount = 0; |
|
180 | + |
|
181 | + foreach ($this->clusterPools as $driverPool) { |
|
182 | + try { |
|
183 | + if ($result = $driverPool->commit()) { |
|
184 | + $hasCommitOnce = $result; |
|
185 | + } |
|
186 | + } catch (PhpfastcacheExceptionInterface) { |
|
187 | + $eCount++; |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + if (\count($this->clusterPools) <= $eCount) { |
|
192 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
193 | + } |
|
194 | + // Return true only if at least one backend confirmed the "commit" operation |
|
195 | + return $hasCommitOnce; |
|
196 | + } |
|
197 | 197 | } |
@@ -43,7 +43,8 @@ discard block |
||
43 | 43 | $item = $poolItem; |
44 | 44 | break; |
45 | 45 | } |
46 | - } catch (PhpfastcacheExceptionInterface) { |
|
46 | + } |
|
47 | + catch (PhpfastcacheExceptionInterface) { |
|
47 | 48 | $eCount++; |
48 | 49 | } |
49 | 50 | } |
@@ -73,7 +74,8 @@ discard block |
||
73 | 74 | if ($poolItem->isHit()) { |
74 | 75 | return true; |
75 | 76 | } |
76 | - } catch (PhpfastcacheExceptionInterface) { |
|
77 | + } |
|
78 | + catch (PhpfastcacheExceptionInterface) { |
|
77 | 79 | $eCount++; |
78 | 80 | } |
79 | 81 | } |
@@ -99,7 +101,8 @@ discard block |
||
99 | 101 | if ($result = $driverPool->clear()) { |
100 | 102 | $hasClearedOnce = $result; |
101 | 103 | } |
102 | - } catch (PhpfastcacheExceptionInterface) { |
|
104 | + } |
|
105 | + catch (PhpfastcacheExceptionInterface) { |
|
103 | 106 | $eCount++; |
104 | 107 | } |
105 | 108 | } |
@@ -127,7 +130,8 @@ discard block |
||
127 | 130 | if ($result = $driverPool->deleteItem($key)) { |
128 | 131 | $hasDeletedOnce = $result; |
129 | 132 | } |
130 | - } catch (PhpfastcacheExceptionInterface) { |
|
133 | + } |
|
134 | + catch (PhpfastcacheExceptionInterface) { |
|
131 | 135 | $eCount++; |
132 | 136 | } |
133 | 137 | } |
@@ -157,7 +161,8 @@ discard block |
||
157 | 161 | if ($result = $driverPool->save($poolItem)) { |
158 | 162 | $hasSavedOnce = $result; |
159 | 163 | } |
160 | - } catch (PhpfastcacheExceptionInterface) { |
|
164 | + } |
|
165 | + catch (PhpfastcacheExceptionInterface) { |
|
161 | 166 | $eCount++; |
162 | 167 | } |
163 | 168 | } |
@@ -183,7 +188,8 @@ discard block |
||
183 | 188 | if ($result = $driverPool->commit()) { |
184 | 189 | $hasCommitOnce = $result; |
185 | 190 | } |
186 | - } catch (PhpfastcacheExceptionInterface) { |
|
191 | + } |
|
192 | + catch (PhpfastcacheExceptionInterface) { |
|
187 | 193 | $eCount++; |
188 | 194 | } |
189 | 195 | } |
@@ -34,131 +34,131 @@ |
||
34 | 34 | |
35 | 35 | class Driver extends ClusterPoolAbstract |
36 | 36 | { |
37 | - /** |
|
38 | - * MasterSlaveReplicationCluster constructor. |
|
39 | - * @param string $clusterName |
|
40 | - * @param EventManagerInterface $em |
|
41 | - * @param AggregatablePoolInterface ...$driverPools |
|
42 | - * @throws PhpfastcacheDriverCheckException |
|
43 | - * @throws PhpfastcacheDriverConnectException |
|
44 | - * @throws PhpfastcacheInvalidArgumentException |
|
45 | - * @throws PhpfastcacheCoreException |
|
46 | - * @throws PhpfastcacheDriverException |
|
47 | - * @throws PhpfastcacheIOException |
|
48 | - */ |
|
49 | - public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
50 | - { |
|
51 | - if (\count($driverPools) !== 2) { |
|
52 | - throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
53 | - } |
|
54 | - |
|
55 | - parent::__construct($clusterName, $em, ...$driverPools); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @inheritDoc |
|
60 | - */ |
|
61 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
62 | - { |
|
63 | - return $this->getStandardizedItem( |
|
64 | - $this->makeOperation(static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
65 | - return $pool->getItem($key); |
|
66 | - }) ?? (new Item($this, $key, $this->getEventManager()))->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())), |
|
67 | - $this |
|
68 | - ); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param callable $operation |
|
73 | - * @return mixed |
|
74 | - * @throws PhpfastcacheReplicationException |
|
75 | - */ |
|
76 | - protected function makeOperation(callable $operation) |
|
77 | - { |
|
78 | - try { |
|
79 | - return $operation($this->getMasterPool()); |
|
80 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
81 | - try { |
|
82 | - $this->eventManager->dispatch( |
|
83 | - Event::CACHE_REPLICATION_SLAVE_FALLBACK, |
|
84 | - $this, |
|
85 | - \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
86 | - ); |
|
87 | - return $operation($this->getSlavePool()); |
|
88 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
89 | - throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return AggregatablePoolInterface |
|
96 | - */ |
|
97 | - protected function getMasterPool(): AggregatablePoolInterface |
|
98 | - { |
|
99 | - return $this->clusterPools[0]; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @return AggregatablePoolInterface |
|
104 | - */ |
|
105 | - protected function getSlavePool(): AggregatablePoolInterface |
|
106 | - { |
|
107 | - return $this->clusterPools[1]; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @inheritDoc |
|
112 | - */ |
|
113 | - public function hasItem(string $key): bool |
|
114 | - { |
|
115 | - return $this->makeOperation( |
|
116 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->hasItem($key) |
|
117 | - ); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @inheritDoc |
|
122 | - */ |
|
123 | - public function clear(): bool |
|
124 | - { |
|
125 | - return $this->makeOperation( |
|
126 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->clear() |
|
127 | - ); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @inheritDoc |
|
132 | - */ |
|
133 | - public function deleteItem(string $key): bool |
|
134 | - { |
|
135 | - return $this->makeOperation( |
|
136 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->deleteItem($key) |
|
137 | - ); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @inheritDoc |
|
142 | - */ |
|
143 | - public function save(CacheItemInterface $item): bool |
|
144 | - { |
|
145 | - return $this->makeOperation( |
|
146 | - function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
147 | - /** @var ExtendedCacheItemInterface $item */ |
|
148 | - $item->setHit(true); |
|
149 | - return $pool->save($this->getStandardizedItem($item, $pool)); |
|
150 | - } |
|
151 | - ); |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * @inheritDoc |
|
157 | - */ |
|
158 | - public function commit(): bool |
|
159 | - { |
|
160 | - return $this->makeOperation( |
|
161 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->commit() |
|
162 | - ); |
|
163 | - } |
|
37 | + /** |
|
38 | + * MasterSlaveReplicationCluster constructor. |
|
39 | + * @param string $clusterName |
|
40 | + * @param EventManagerInterface $em |
|
41 | + * @param AggregatablePoolInterface ...$driverPools |
|
42 | + * @throws PhpfastcacheDriverCheckException |
|
43 | + * @throws PhpfastcacheDriverConnectException |
|
44 | + * @throws PhpfastcacheInvalidArgumentException |
|
45 | + * @throws PhpfastcacheCoreException |
|
46 | + * @throws PhpfastcacheDriverException |
|
47 | + * @throws PhpfastcacheIOException |
|
48 | + */ |
|
49 | + public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
50 | + { |
|
51 | + if (\count($driverPools) !== 2) { |
|
52 | + throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
53 | + } |
|
54 | + |
|
55 | + parent::__construct($clusterName, $em, ...$driverPools); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @inheritDoc |
|
60 | + */ |
|
61 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
62 | + { |
|
63 | + return $this->getStandardizedItem( |
|
64 | + $this->makeOperation(static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
65 | + return $pool->getItem($key); |
|
66 | + }) ?? (new Item($this, $key, $this->getEventManager()))->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())), |
|
67 | + $this |
|
68 | + ); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param callable $operation |
|
73 | + * @return mixed |
|
74 | + * @throws PhpfastcacheReplicationException |
|
75 | + */ |
|
76 | + protected function makeOperation(callable $operation) |
|
77 | + { |
|
78 | + try { |
|
79 | + return $operation($this->getMasterPool()); |
|
80 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
81 | + try { |
|
82 | + $this->eventManager->dispatch( |
|
83 | + Event::CACHE_REPLICATION_SLAVE_FALLBACK, |
|
84 | + $this, |
|
85 | + \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
86 | + ); |
|
87 | + return $operation($this->getSlavePool()); |
|
88 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
89 | + throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return AggregatablePoolInterface |
|
96 | + */ |
|
97 | + protected function getMasterPool(): AggregatablePoolInterface |
|
98 | + { |
|
99 | + return $this->clusterPools[0]; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @return AggregatablePoolInterface |
|
104 | + */ |
|
105 | + protected function getSlavePool(): AggregatablePoolInterface |
|
106 | + { |
|
107 | + return $this->clusterPools[1]; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @inheritDoc |
|
112 | + */ |
|
113 | + public function hasItem(string $key): bool |
|
114 | + { |
|
115 | + return $this->makeOperation( |
|
116 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->hasItem($key) |
|
117 | + ); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @inheritDoc |
|
122 | + */ |
|
123 | + public function clear(): bool |
|
124 | + { |
|
125 | + return $this->makeOperation( |
|
126 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->clear() |
|
127 | + ); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @inheritDoc |
|
132 | + */ |
|
133 | + public function deleteItem(string $key): bool |
|
134 | + { |
|
135 | + return $this->makeOperation( |
|
136 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->deleteItem($key) |
|
137 | + ); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @inheritDoc |
|
142 | + */ |
|
143 | + public function save(CacheItemInterface $item): bool |
|
144 | + { |
|
145 | + return $this->makeOperation( |
|
146 | + function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
147 | + /** @var ExtendedCacheItemInterface $item */ |
|
148 | + $item->setHit(true); |
|
149 | + return $pool->save($this->getStandardizedItem($item, $pool)); |
|
150 | + } |
|
151 | + ); |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * @inheritDoc |
|
157 | + */ |
|
158 | + public function commit(): bool |
|
159 | + { |
|
160 | + return $this->makeOperation( |
|
161 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->commit() |
|
162 | + ); |
|
163 | + } |
|
164 | 164 | } |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | public function getItem(string $key): ExtendedCacheItemInterface |
62 | 62 | { |
63 | 63 | return $this->getStandardizedItem( |
64 | - $this->makeOperation(static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
64 | + $this->makeOperation(static function(ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
65 | 65 | return $pool->getItem($key); |
66 | 66 | }) ?? (new Item($this, $key, $this->getEventManager()))->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())), |
67 | 67 | $this |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | public function save(CacheItemInterface $item): bool |
144 | 144 | { |
145 | 145 | return $this->makeOperation( |
146 | - function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
146 | + function(ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
147 | 147 | /** @var ExtendedCacheItemInterface $item */ |
148 | 148 | $item->setHit(true); |
149 | 149 | return $pool->save($this->getStandardizedItem($item, $pool)); |
@@ -46,8 +46,7 @@ discard block |
||
46 | 46 | * @throws PhpfastcacheDriverException |
47 | 47 | * @throws PhpfastcacheIOException |
48 | 48 | */ |
49 | - public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
50 | - { |
|
49 | + public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) { |
|
51 | 50 | if (\count($driverPools) !== 2) { |
52 | 51 | throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
53 | 52 | } |
@@ -73,11 +72,11 @@ discard block |
||
73 | 72 | * @return mixed |
74 | 73 | * @throws PhpfastcacheReplicationException |
75 | 74 | */ |
76 | - protected function makeOperation(callable $operation) |
|
77 | - { |
|
75 | + protected function makeOperation(callable $operation) { |
|
78 | 76 | try { |
79 | 77 | return $operation($this->getMasterPool()); |
80 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
78 | + } |
|
79 | + catch (PhpfastcacheExceptionInterface $e) { |
|
81 | 80 | try { |
82 | 81 | $this->eventManager->dispatch( |
83 | 82 | Event::CACHE_REPLICATION_SLAVE_FALLBACK, |
@@ -85,7 +84,8 @@ discard block |
||
85 | 84 | \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
86 | 85 | ); |
87 | 86 | return $operation($this->getSlavePool()); |
88 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
87 | + } |
|
88 | + catch (PhpfastcacheExceptionInterface $e) { |
|
89 | 89 | throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
90 | 90 | } |
91 | 91 | } |
@@ -40,211 +40,211 @@ |
||
40 | 40 | |
41 | 41 | abstract class ClusterPoolAbstract implements ClusterPoolInterface |
42 | 42 | { |
43 | - use TaggableCacheItemPoolTrait; |
|
44 | - use ClusterPoolTrait { |
|
45 | - TaggableCacheItemPoolTrait::__construct as private __parentConstruct; |
|
46 | - } |
|
47 | - |
|
48 | - public const STRATEGY = [ |
|
49 | - AggregatorInterface::STRATEGY_FULL_REPLICATION => FullReplicationCluster::class, |
|
50 | - AggregatorInterface::STRATEGY_SEMI_REPLICATION => SemiReplicationCluster::class, |
|
51 | - AggregatorInterface::STRATEGY_MASTER_SLAVE => MasterSlaveReplicationCluster::class, |
|
52 | - AggregatorInterface::STRATEGY_RANDOM_REPLICATION => RandomReplicationCluster::class, |
|
53 | - ]; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var AggregatablePoolInterface[] |
|
57 | - */ |
|
58 | - protected array $clusterPools; |
|
59 | - |
|
60 | - /** |
|
61 | - * ClusterPoolAbstract constructor. |
|
62 | - * @param string $clusterName |
|
63 | - * @param EventManagerInterface $em |
|
64 | - * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
65 | - * @throws PhpfastcacheDriverCheckException |
|
66 | - * @throws PhpfastcacheDriverConnectException |
|
67 | - * @throws PhpfastcacheInvalidArgumentException |
|
68 | - * @throws PhpfastcacheCoreException |
|
69 | - * @throws PhpfastcacheDriverException |
|
70 | - * @throws PhpfastcacheIOException |
|
71 | - */ |
|
72 | - public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
73 | - { |
|
74 | - if (count($driverPools) < 2) { |
|
75 | - throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
|
76 | - } |
|
77 | - $this->clusterPools = $driverPools; |
|
78 | - $this->__parentConstruct(new ConfigurationOption(), $clusterName, $em); |
|
79 | - $this->setEventManager(EventManager::getInstance()); |
|
80 | - $this->setClusterPoolsAggregator(); |
|
81 | - } |
|
82 | - |
|
83 | - protected function setClusterPoolsAggregator(): void |
|
84 | - { |
|
85 | - foreach ($this->clusterPools as $clusterPool) { |
|
86 | - $clusterPool->setAggregatedBy($this); |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @inheritDoc |
|
92 | - */ |
|
93 | - public function getIO(): DriverIO |
|
94 | - { |
|
95 | - $io = new DriverIO(); |
|
96 | - foreach ($this->clusterPools as $clusterPool) { |
|
97 | - $io->setReadHit($io->getReadHit() + $clusterPool->getIO()->getReadHit()) |
|
98 | - ->setReadMiss($io->getReadMiss() + $clusterPool->getIO()->getReadMiss()) |
|
99 | - ->setWriteHit($io->getWriteHit() + $clusterPool->getIO()->getWriteHit()); |
|
100 | - } |
|
101 | - return $io; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @inheritDoc |
|
106 | - */ |
|
107 | - public function getClusterPools(): array |
|
108 | - { |
|
109 | - return $this->clusterPools; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @inheritDoc |
|
114 | - */ |
|
115 | - public function getConfigs(): array |
|
116 | - { |
|
117 | - $configs = []; |
|
118 | - |
|
119 | - foreach ($this->getClusterPools() as $clusterPool) { |
|
120 | - $configs[$clusterPool->getDriverName()] = $clusterPool->getConfig(); |
|
121 | - } |
|
122 | - |
|
123 | - return $configs; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @inheritDoc |
|
128 | - */ |
|
129 | - public function getItems(array $keys = []): iterable |
|
130 | - { |
|
131 | - $items = []; |
|
132 | - |
|
133 | - foreach ($keys as $key) { |
|
134 | - $items[$key] = $this->getItem($key); |
|
135 | - } |
|
136 | - |
|
137 | - return $items; |
|
138 | - } |
|
139 | - /** |
|
140 | - * Shared method used by All Clusters |
|
141 | - */ |
|
142 | - |
|
143 | - /** |
|
144 | - * @inheritDoc |
|
145 | - */ |
|
146 | - public function deleteItems(array $keys): bool |
|
147 | - { |
|
148 | - $hasDeletedOnce = false; |
|
149 | - foreach ($this->clusterPools as $driverPool) { |
|
150 | - if ($result = $driverPool->deleteItems($keys)) { |
|
151 | - $hasDeletedOnce = $result; |
|
152 | - } |
|
153 | - } |
|
154 | - // Return true only if at least one backend confirmed the "clear" operation |
|
155 | - return $hasDeletedOnce; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @param CacheItemInterface $item |
|
160 | - * @return bool |
|
161 | - * @throws InvalidArgumentException |
|
162 | - * @throws PhpfastcacheLogicException |
|
163 | - */ |
|
164 | - public function saveDeferred(CacheItemInterface $item): bool |
|
165 | - { |
|
166 | - /** @var ExtendedCacheItemInterface $item */ |
|
167 | - $hasSavedOnce = false; |
|
168 | - foreach ($this->clusterPools as $driverPool) { |
|
169 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
170 | - if ($result = $driverPool->saveDeferred($poolItem)) { |
|
171 | - $hasSavedOnce = $result; |
|
172 | - } |
|
173 | - } |
|
174 | - // Return true only if at least one backend confirmed the "commit" operation |
|
175 | - return $hasSavedOnce; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @param ExtendedCacheItemInterface $item |
|
180 | - * @param ExtendedCacheItemPoolInterface $driverPool |
|
181 | - * @return ExtendedCacheItemInterface |
|
182 | - * @throws InvalidArgumentException |
|
183 | - * @throws PhpfastcacheLogicException |
|
184 | - */ |
|
185 | - protected function getStandardizedItem(ExtendedCacheItemInterface $item, ExtendedCacheItemPoolInterface $driverPool): ExtendedCacheItemInterface |
|
186 | - { |
|
187 | - if (!$item->doesItemBelongToThatDriverBackend($driverPool)) { |
|
188 | - /** |
|
189 | - * Avoid infinite loop |
|
190 | - */ |
|
191 | - if ($driverPool === $this) { |
|
192 | - /** @var ExtendedCacheItemInterface $itemPool */ |
|
193 | - $itemClass = $driverPool::getItemClass(); |
|
194 | - $itemPool = new $itemClass($this, $item->getKey(), $this->getEventManager()); |
|
195 | - $item->cloneInto($itemPool, $driverPool); |
|
196 | - |
|
197 | - return $itemPool; |
|
198 | - } |
|
199 | - |
|
200 | - $itemPool = $driverPool->getItem($item->getKey()); |
|
201 | - $item->cloneInto($itemPool, $driverPool); |
|
202 | - |
|
203 | - return $itemPool; |
|
204 | - } |
|
205 | - |
|
206 | - return $item->setEventManager($this->getEventManager()); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @return DriverStatistic |
|
211 | - */ |
|
212 | - public function getStats(): DriverStatistic |
|
213 | - { |
|
214 | - $stats = new DriverStatistic(); |
|
215 | - $stats->setInfo( |
|
216 | - sprintf( |
|
217 | - 'Using %d pool(s): %s', |
|
218 | - \count($this->clusterPools), |
|
219 | - \implode( |
|
220 | - ', ', |
|
221 | - \array_map( |
|
222 | - static fn (ExtendedCacheItemPoolInterface $pool) => \get_class($pool), |
|
223 | - $this->clusterPools |
|
224 | - ) |
|
225 | - ) |
|
226 | - ) |
|
227 | - ); |
|
228 | - |
|
229 | - $stats->setSize( |
|
230 | - (int)\array_sum( |
|
231 | - \array_map( |
|
232 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->getStats()->getSize(), |
|
233 | - $this->clusterPools |
|
234 | - ) |
|
235 | - ) |
|
236 | - ); |
|
237 | - |
|
238 | - $stats->setData( |
|
239 | - \implode( |
|
240 | - ', ', |
|
241 | - \array_map( |
|
242 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->getStats()->getData(), |
|
243 | - $this->clusterPools |
|
244 | - ) |
|
245 | - ) |
|
246 | - ); |
|
247 | - |
|
248 | - return $stats; |
|
249 | - } |
|
43 | + use TaggableCacheItemPoolTrait; |
|
44 | + use ClusterPoolTrait { |
|
45 | + TaggableCacheItemPoolTrait::__construct as private __parentConstruct; |
|
46 | + } |
|
47 | + |
|
48 | + public const STRATEGY = [ |
|
49 | + AggregatorInterface::STRATEGY_FULL_REPLICATION => FullReplicationCluster::class, |
|
50 | + AggregatorInterface::STRATEGY_SEMI_REPLICATION => SemiReplicationCluster::class, |
|
51 | + AggregatorInterface::STRATEGY_MASTER_SLAVE => MasterSlaveReplicationCluster::class, |
|
52 | + AggregatorInterface::STRATEGY_RANDOM_REPLICATION => RandomReplicationCluster::class, |
|
53 | + ]; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var AggregatablePoolInterface[] |
|
57 | + */ |
|
58 | + protected array $clusterPools; |
|
59 | + |
|
60 | + /** |
|
61 | + * ClusterPoolAbstract constructor. |
|
62 | + * @param string $clusterName |
|
63 | + * @param EventManagerInterface $em |
|
64 | + * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
65 | + * @throws PhpfastcacheDriverCheckException |
|
66 | + * @throws PhpfastcacheDriverConnectException |
|
67 | + * @throws PhpfastcacheInvalidArgumentException |
|
68 | + * @throws PhpfastcacheCoreException |
|
69 | + * @throws PhpfastcacheDriverException |
|
70 | + * @throws PhpfastcacheIOException |
|
71 | + */ |
|
72 | + public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
73 | + { |
|
74 | + if (count($driverPools) < 2) { |
|
75 | + throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
|
76 | + } |
|
77 | + $this->clusterPools = $driverPools; |
|
78 | + $this->__parentConstruct(new ConfigurationOption(), $clusterName, $em); |
|
79 | + $this->setEventManager(EventManager::getInstance()); |
|
80 | + $this->setClusterPoolsAggregator(); |
|
81 | + } |
|
82 | + |
|
83 | + protected function setClusterPoolsAggregator(): void |
|
84 | + { |
|
85 | + foreach ($this->clusterPools as $clusterPool) { |
|
86 | + $clusterPool->setAggregatedBy($this); |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @inheritDoc |
|
92 | + */ |
|
93 | + public function getIO(): DriverIO |
|
94 | + { |
|
95 | + $io = new DriverIO(); |
|
96 | + foreach ($this->clusterPools as $clusterPool) { |
|
97 | + $io->setReadHit($io->getReadHit() + $clusterPool->getIO()->getReadHit()) |
|
98 | + ->setReadMiss($io->getReadMiss() + $clusterPool->getIO()->getReadMiss()) |
|
99 | + ->setWriteHit($io->getWriteHit() + $clusterPool->getIO()->getWriteHit()); |
|
100 | + } |
|
101 | + return $io; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @inheritDoc |
|
106 | + */ |
|
107 | + public function getClusterPools(): array |
|
108 | + { |
|
109 | + return $this->clusterPools; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @inheritDoc |
|
114 | + */ |
|
115 | + public function getConfigs(): array |
|
116 | + { |
|
117 | + $configs = []; |
|
118 | + |
|
119 | + foreach ($this->getClusterPools() as $clusterPool) { |
|
120 | + $configs[$clusterPool->getDriverName()] = $clusterPool->getConfig(); |
|
121 | + } |
|
122 | + |
|
123 | + return $configs; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @inheritDoc |
|
128 | + */ |
|
129 | + public function getItems(array $keys = []): iterable |
|
130 | + { |
|
131 | + $items = []; |
|
132 | + |
|
133 | + foreach ($keys as $key) { |
|
134 | + $items[$key] = $this->getItem($key); |
|
135 | + } |
|
136 | + |
|
137 | + return $items; |
|
138 | + } |
|
139 | + /** |
|
140 | + * Shared method used by All Clusters |
|
141 | + */ |
|
142 | + |
|
143 | + /** |
|
144 | + * @inheritDoc |
|
145 | + */ |
|
146 | + public function deleteItems(array $keys): bool |
|
147 | + { |
|
148 | + $hasDeletedOnce = false; |
|
149 | + foreach ($this->clusterPools as $driverPool) { |
|
150 | + if ($result = $driverPool->deleteItems($keys)) { |
|
151 | + $hasDeletedOnce = $result; |
|
152 | + } |
|
153 | + } |
|
154 | + // Return true only if at least one backend confirmed the "clear" operation |
|
155 | + return $hasDeletedOnce; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @param CacheItemInterface $item |
|
160 | + * @return bool |
|
161 | + * @throws InvalidArgumentException |
|
162 | + * @throws PhpfastcacheLogicException |
|
163 | + */ |
|
164 | + public function saveDeferred(CacheItemInterface $item): bool |
|
165 | + { |
|
166 | + /** @var ExtendedCacheItemInterface $item */ |
|
167 | + $hasSavedOnce = false; |
|
168 | + foreach ($this->clusterPools as $driverPool) { |
|
169 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
170 | + if ($result = $driverPool->saveDeferred($poolItem)) { |
|
171 | + $hasSavedOnce = $result; |
|
172 | + } |
|
173 | + } |
|
174 | + // Return true only if at least one backend confirmed the "commit" operation |
|
175 | + return $hasSavedOnce; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @param ExtendedCacheItemInterface $item |
|
180 | + * @param ExtendedCacheItemPoolInterface $driverPool |
|
181 | + * @return ExtendedCacheItemInterface |
|
182 | + * @throws InvalidArgumentException |
|
183 | + * @throws PhpfastcacheLogicException |
|
184 | + */ |
|
185 | + protected function getStandardizedItem(ExtendedCacheItemInterface $item, ExtendedCacheItemPoolInterface $driverPool): ExtendedCacheItemInterface |
|
186 | + { |
|
187 | + if (!$item->doesItemBelongToThatDriverBackend($driverPool)) { |
|
188 | + /** |
|
189 | + * Avoid infinite loop |
|
190 | + */ |
|
191 | + if ($driverPool === $this) { |
|
192 | + /** @var ExtendedCacheItemInterface $itemPool */ |
|
193 | + $itemClass = $driverPool::getItemClass(); |
|
194 | + $itemPool = new $itemClass($this, $item->getKey(), $this->getEventManager()); |
|
195 | + $item->cloneInto($itemPool, $driverPool); |
|
196 | + |
|
197 | + return $itemPool; |
|
198 | + } |
|
199 | + |
|
200 | + $itemPool = $driverPool->getItem($item->getKey()); |
|
201 | + $item->cloneInto($itemPool, $driverPool); |
|
202 | + |
|
203 | + return $itemPool; |
|
204 | + } |
|
205 | + |
|
206 | + return $item->setEventManager($this->getEventManager()); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @return DriverStatistic |
|
211 | + */ |
|
212 | + public function getStats(): DriverStatistic |
|
213 | + { |
|
214 | + $stats = new DriverStatistic(); |
|
215 | + $stats->setInfo( |
|
216 | + sprintf( |
|
217 | + 'Using %d pool(s): %s', |
|
218 | + \count($this->clusterPools), |
|
219 | + \implode( |
|
220 | + ', ', |
|
221 | + \array_map( |
|
222 | + static fn (ExtendedCacheItemPoolInterface $pool) => \get_class($pool), |
|
223 | + $this->clusterPools |
|
224 | + ) |
|
225 | + ) |
|
226 | + ) |
|
227 | + ); |
|
228 | + |
|
229 | + $stats->setSize( |
|
230 | + (int)\array_sum( |
|
231 | + \array_map( |
|
232 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->getStats()->getSize(), |
|
233 | + $this->clusterPools |
|
234 | + ) |
|
235 | + ) |
|
236 | + ); |
|
237 | + |
|
238 | + $stats->setData( |
|
239 | + \implode( |
|
240 | + ', ', |
|
241 | + \array_map( |
|
242 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->getStats()->getData(), |
|
243 | + $this->clusterPools |
|
244 | + ) |
|
245 | + ) |
|
246 | + ); |
|
247 | + |
|
248 | + return $stats; |
|
249 | + } |
|
250 | 250 | } |
@@ -227,7 +227,7 @@ |
||
227 | 227 | ); |
228 | 228 | |
229 | 229 | $stats->setSize( |
230 | - (int)\array_sum( |
|
230 | + (int) \array_sum( |
|
231 | 231 | \array_map( |
232 | 232 | static fn (ExtendedCacheItemPoolInterface $pool) => $pool->getStats()->getSize(), |
233 | 233 | $this->clusterPools |
@@ -69,8 +69,7 @@ |
||
69 | 69 | * @throws PhpfastcacheDriverException |
70 | 70 | * @throws PhpfastcacheIOException |
71 | 71 | */ |
72 | - public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
73 | - { |
|
72 | + public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) { |
|
74 | 73 | if (count($driverPools) < 2) { |
75 | 74 | throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
76 | 75 | } |
@@ -30,141 +30,141 @@ |
||
30 | 30 | |
31 | 31 | class ClusterAggregator implements AggregatorInterface |
32 | 32 | { |
33 | - /** |
|
34 | - * @var array<string, AggregatablePoolInterface> |
|
35 | - */ |
|
36 | - protected array $driverPools; |
|
37 | - |
|
38 | - protected ClusterPoolInterface $cluster; |
|
39 | - |
|
40 | - protected string $clusterAggregatorName; |
|
41 | - |
|
42 | - /** |
|
43 | - * ClusterAggregator constructor. |
|
44 | - * @param string $clusterAggregatorName |
|
45 | - * @param array<AggregatablePoolInterface> $driverPools |
|
46 | - * @throws PhpfastcacheLogicException |
|
47 | - */ |
|
48 | - public function __construct(string $clusterAggregatorName = '', AggregatablePoolInterface ...$driverPools) |
|
49 | - { |
|
50 | - $clusterAggregatorName = trim($clusterAggregatorName); |
|
51 | - if (empty($clusterAggregatorName)) { |
|
52 | - try { |
|
53 | - $clusterAggregatorName = 'cluster_' . \bin2hex(\random_bytes(15)); |
|
54 | - } catch (Exception) { |
|
55 | - $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_hash(new stdClass())); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - $this->clusterAggregatorName = $clusterAggregatorName; |
|
60 | - |
|
61 | - foreach ($driverPools as $driverPool) { |
|
62 | - $this->aggregateDriver($driverPool); |
|
63 | - } |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @param AggregatablePoolInterface $driverPool |
|
68 | - * |
|
69 | - * @throws PhpfastcacheLogicException |
|
70 | - */ |
|
71 | - public function aggregateDriver(AggregatablePoolInterface $driverPool): void |
|
72 | - { |
|
73 | - if (isset($this->cluster)) { |
|
74 | - throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
75 | - } |
|
76 | - |
|
77 | - $splHash = \spl_object_hash($driverPool); |
|
78 | - if (!isset($this->driverPools[$splHash])) { |
|
79 | - if ($driverPool instanceof ClusterPoolInterface) { |
|
80 | - throw new PhpfastcacheLogicException('Recursive cluster aggregation is not allowed !'); |
|
81 | - } |
|
82 | - |
|
83 | - $this->driverPools[$splHash] = $driverPool; |
|
84 | - } else { |
|
85 | - throw new PhpfastcacheLogicException('This pool has been already aggregated !'); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param string $driverName |
|
91 | - * @param ConfigurationOption|null $driverConfig |
|
92 | - * @throws PhpfastcacheDriverCheckException |
|
93 | - * @throws PhpfastcacheDriverException |
|
94 | - * @throws PhpfastcacheDriverNotFoundException |
|
95 | - * @throws PhpfastcacheLogicException |
|
96 | - */ |
|
97 | - public function aggregateDriverByName(string $driverName, ConfigurationOption $driverConfig = null): void |
|
98 | - { |
|
99 | - if (isset($this->cluster)) { |
|
100 | - throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
101 | - } |
|
102 | - |
|
103 | - $driverInstance = CacheManager::getInstance($driverName, $driverConfig); |
|
104 | - |
|
105 | - if ($driverInstance instanceof AggregatablePoolInterface) { |
|
106 | - $this->aggregateDriver($driverInstance); |
|
107 | - } |
|
108 | - |
|
109 | - throw new PhpfastcacheDriverException( |
|
110 | - \sprintf( |
|
111 | - 'Driver "%s" does not implements "%s"', |
|
112 | - $driverInstance::class, |
|
113 | - AggregatablePoolInterface::class |
|
114 | - ) |
|
115 | - ); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param AggregatablePoolInterface $driverPool |
|
120 | - * |
|
121 | - * @throws PhpfastcacheLogicException |
|
122 | - */ |
|
123 | - public function disaggregateDriver(AggregatablePoolInterface $driverPool): void |
|
124 | - { |
|
125 | - if (isset($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 (!isset($this->cluster)) { |
|
147 | - $clusterClass = ClusterPoolAbstract::STRATEGY[$strategy]; |
|
148 | - $this->cluster = new $clusterClass( |
|
149 | - $this->getClusterAggregatorName(), |
|
150 | - EventManager::getInstance(), |
|
151 | - ...\array_values($this->driverPools) |
|
152 | - ); |
|
153 | - |
|
154 | - $this->cluster->getEventManager()->dispatch(Event::CACHE_CLUSTER_BUILT, $this, $this->cluster); |
|
155 | - } |
|
156 | - } else { |
|
157 | - throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy'); |
|
158 | - } |
|
159 | - |
|
160 | - return $this->cluster; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public function getClusterAggregatorName(): string |
|
167 | - { |
|
168 | - return $this->clusterAggregatorName; |
|
169 | - } |
|
33 | + /** |
|
34 | + * @var array<string, AggregatablePoolInterface> |
|
35 | + */ |
|
36 | + protected array $driverPools; |
|
37 | + |
|
38 | + protected ClusterPoolInterface $cluster; |
|
39 | + |
|
40 | + protected string $clusterAggregatorName; |
|
41 | + |
|
42 | + /** |
|
43 | + * ClusterAggregator constructor. |
|
44 | + * @param string $clusterAggregatorName |
|
45 | + * @param array<AggregatablePoolInterface> $driverPools |
|
46 | + * @throws PhpfastcacheLogicException |
|
47 | + */ |
|
48 | + public function __construct(string $clusterAggregatorName = '', AggregatablePoolInterface ...$driverPools) |
|
49 | + { |
|
50 | + $clusterAggregatorName = trim($clusterAggregatorName); |
|
51 | + if (empty($clusterAggregatorName)) { |
|
52 | + try { |
|
53 | + $clusterAggregatorName = 'cluster_' . \bin2hex(\random_bytes(15)); |
|
54 | + } catch (Exception) { |
|
55 | + $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_hash(new stdClass())); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + $this->clusterAggregatorName = $clusterAggregatorName; |
|
60 | + |
|
61 | + foreach ($driverPools as $driverPool) { |
|
62 | + $this->aggregateDriver($driverPool); |
|
63 | + } |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @param AggregatablePoolInterface $driverPool |
|
68 | + * |
|
69 | + * @throws PhpfastcacheLogicException |
|
70 | + */ |
|
71 | + public function aggregateDriver(AggregatablePoolInterface $driverPool): void |
|
72 | + { |
|
73 | + if (isset($this->cluster)) { |
|
74 | + throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
75 | + } |
|
76 | + |
|
77 | + $splHash = \spl_object_hash($driverPool); |
|
78 | + if (!isset($this->driverPools[$splHash])) { |
|
79 | + if ($driverPool instanceof ClusterPoolInterface) { |
|
80 | + throw new PhpfastcacheLogicException('Recursive cluster aggregation is not allowed !'); |
|
81 | + } |
|
82 | + |
|
83 | + $this->driverPools[$splHash] = $driverPool; |
|
84 | + } else { |
|
85 | + throw new PhpfastcacheLogicException('This pool has been already aggregated !'); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param string $driverName |
|
91 | + * @param ConfigurationOption|null $driverConfig |
|
92 | + * @throws PhpfastcacheDriverCheckException |
|
93 | + * @throws PhpfastcacheDriverException |
|
94 | + * @throws PhpfastcacheDriverNotFoundException |
|
95 | + * @throws PhpfastcacheLogicException |
|
96 | + */ |
|
97 | + public function aggregateDriverByName(string $driverName, ConfigurationOption $driverConfig = null): void |
|
98 | + { |
|
99 | + if (isset($this->cluster)) { |
|
100 | + throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); |
|
101 | + } |
|
102 | + |
|
103 | + $driverInstance = CacheManager::getInstance($driverName, $driverConfig); |
|
104 | + |
|
105 | + if ($driverInstance instanceof AggregatablePoolInterface) { |
|
106 | + $this->aggregateDriver($driverInstance); |
|
107 | + } |
|
108 | + |
|
109 | + throw new PhpfastcacheDriverException( |
|
110 | + \sprintf( |
|
111 | + 'Driver "%s" does not implements "%s"', |
|
112 | + $driverInstance::class, |
|
113 | + AggregatablePoolInterface::class |
|
114 | + ) |
|
115 | + ); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param AggregatablePoolInterface $driverPool |
|
120 | + * |
|
121 | + * @throws PhpfastcacheLogicException |
|
122 | + */ |
|
123 | + public function disaggregateDriver(AggregatablePoolInterface $driverPool): void |
|
124 | + { |
|
125 | + if (isset($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 (!isset($this->cluster)) { |
|
147 | + $clusterClass = ClusterPoolAbstract::STRATEGY[$strategy]; |
|
148 | + $this->cluster = new $clusterClass( |
|
149 | + $this->getClusterAggregatorName(), |
|
150 | + EventManager::getInstance(), |
|
151 | + ...\array_values($this->driverPools) |
|
152 | + ); |
|
153 | + |
|
154 | + $this->cluster->getEventManager()->dispatch(Event::CACHE_CLUSTER_BUILT, $this, $this->cluster); |
|
155 | + } |
|
156 | + } else { |
|
157 | + throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy'); |
|
158 | + } |
|
159 | + |
|
160 | + return $this->cluster; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public function getClusterAggregatorName(): string |
|
167 | + { |
|
168 | + return $this->clusterAggregatorName; |
|
169 | + } |
|
170 | 170 | } |
@@ -45,13 +45,13 @@ discard block |
||
45 | 45 | * @param array<AggregatablePoolInterface> $driverPools |
46 | 46 | * @throws PhpfastcacheLogicException |
47 | 47 | */ |
48 | - public function __construct(string $clusterAggregatorName = '', AggregatablePoolInterface ...$driverPools) |
|
49 | - { |
|
48 | + public function __construct(string $clusterAggregatorName = '', AggregatablePoolInterface ...$driverPools) { |
|
50 | 49 | $clusterAggregatorName = trim($clusterAggregatorName); |
51 | 50 | if (empty($clusterAggregatorName)) { |
52 | 51 | try { |
53 | 52 | $clusterAggregatorName = 'cluster_' . \bin2hex(\random_bytes(15)); |
54 | - } catch (Exception) { |
|
53 | + } |
|
54 | + catch (Exception) { |
|
55 | 55 | $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_hash(new stdClass())); |
56 | 56 | } |
57 | 57 | } |
@@ -81,7 +81,8 @@ discard block |
||
81 | 81 | } |
82 | 82 | |
83 | 83 | $this->driverPools[$splHash] = $driverPool; |
84 | - } else { |
|
84 | + } |
|
85 | + else { |
|
85 | 86 | throw new PhpfastcacheLogicException('This pool has been already aggregated !'); |
86 | 87 | } |
87 | 88 | } |
@@ -129,7 +130,8 @@ discard block |
||
129 | 130 | $splHash = \spl_object_hash($driverPool); |
130 | 131 | if (isset($this->driverPools[$splHash])) { |
131 | 132 | unset($this->driverPools[$splHash]); |
132 | - } else { |
|
133 | + } |
|
134 | + else { |
|
133 | 135 | throw new PhpfastcacheLogicException('This pool was not aggregated !'); |
134 | 136 | } |
135 | 137 | } |
@@ -153,7 +155,8 @@ discard block |
||
153 | 155 | |
154 | 156 | $this->cluster->getEventManager()->dispatch(Event::CACHE_CLUSTER_BUILT, $this, $this->cluster); |
155 | 157 | } |
156 | - } else { |
|
158 | + } |
|
159 | + else { |
|
157 | 160 | throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy'); |
158 | 161 | } |
159 | 162 |
@@ -21,13 +21,13 @@ |
||
21 | 21 | |
22 | 22 | interface ClusterPoolInterface extends ExtendedCacheItemPoolInterface |
23 | 23 | { |
24 | - /** |
|
25 | - * @return AggregatablePoolInterface[] |
|
26 | - */ |
|
27 | - public function getClusterPools(): array; |
|
24 | + /** |
|
25 | + * @return AggregatablePoolInterface[] |
|
26 | + */ |
|
27 | + public function getClusterPools(): array; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @return ConfigurationOptionInterface[] |
|
31 | - */ |
|
32 | - public function getConfigs(): array; |
|
29 | + /** |
|
30 | + * @return ConfigurationOptionInterface[] |
|
31 | + */ |
|
32 | + public function getConfigs(): array; |
|
33 | 33 | } |