1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of phpFastCache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Cluster\Drivers\SemiReplication; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Cluster\ClusterPoolAbstract; |
19
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
20
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheExceptionInterface; |
21
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheReplicationException; |
22
|
|
|
use Psr\Cache\CacheItemInterface; |
23
|
|
|
use Psr\Cache\InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
class Driver extends ClusterPoolAbstract |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
* @throws PhpfastcacheReplicationException |
31
|
|
|
*/ |
32
|
|
|
public function getItem(string $key): ExtendedCacheItemInterface |
33
|
|
|
{ |
34
|
|
|
/** @var ?ExtendedCacheItemInterface $item */ |
35
|
|
|
$item = null; |
36
|
|
|
$eCount = 0; |
37
|
|
|
|
38
|
|
|
foreach ($this->clusterPools as $driverPool) { |
39
|
|
|
try { |
40
|
|
|
$poolItem = $driverPool->getItem($key); |
41
|
|
|
if (!$item && $poolItem->isHit()) { |
42
|
|
|
$item = $poolItem; |
43
|
|
|
break; |
44
|
|
|
} |
45
|
|
|
} catch (PhpfastcacheExceptionInterface) { |
46
|
|
|
$eCount++; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (\count($this->clusterPools) <= $eCount) { |
51
|
|
|
throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($item === null) { |
55
|
|
|
$item = new Item($this, $key, $this->getEventManager()); |
56
|
|
|
$item->expiresAfter(abs($this->getConfig()->getDefaultTtl())); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->getStandardizedItem($item, $this); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
* @throws PhpfastcacheReplicationException |
65
|
|
|
*/ |
66
|
|
|
public function hasItem(string $key): bool |
67
|
|
|
{ |
68
|
|
|
$eCount = 0; |
69
|
|
|
foreach ($this->clusterPools as $driverPool) { |
70
|
|
|
try { |
71
|
|
|
$poolItem = $driverPool->getItem($key); |
72
|
|
|
if ($poolItem->isHit()) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} catch (PhpfastcacheExceptionInterface) { |
76
|
|
|
$eCount++; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (\count($this->clusterPools) <= $eCount) { |
81
|
|
|
throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
* @throws PhpfastcacheReplicationException |
90
|
|
|
*/ |
91
|
|
|
public function clear(): bool |
92
|
|
|
{ |
93
|
|
|
$hasClearedOnce = false; |
94
|
|
|
$eCount = 0; |
95
|
|
|
|
96
|
|
|
foreach ($this->clusterPools as $driverPool) { |
97
|
|
|
try { |
98
|
|
|
if ($result = $driverPool->clear()) { |
99
|
|
|
$hasClearedOnce = $result; |
100
|
|
|
} |
101
|
|
|
} catch (PhpfastcacheExceptionInterface) { |
102
|
|
|
$eCount++; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (\count($this->clusterPools) <= $eCount) { |
107
|
|
|
throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Return true only if at least one backend confirmed the "clear" operation |
111
|
|
|
return $hasClearedOnce; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
* @throws PhpfastcacheReplicationException |
117
|
|
|
* @throws InvalidArgumentException |
118
|
|
|
*/ |
119
|
|
|
public function deleteItem(string $key): bool |
120
|
|
|
{ |
121
|
|
|
$hasDeletedOnce = false; |
122
|
|
|
$eCount = 0; |
123
|
|
|
|
124
|
|
|
foreach ($this->clusterPools as $driverPool) { |
125
|
|
|
try { |
126
|
|
|
if ($result = $driverPool->deleteItem($key)) { |
127
|
|
|
$hasDeletedOnce = $result; |
128
|
|
|
} |
129
|
|
|
} catch (PhpfastcacheExceptionInterface) { |
130
|
|
|
$eCount++; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (\count($this->clusterPools) <= $eCount) { |
135
|
|
|
throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
136
|
|
|
} |
137
|
|
|
// Return true only if at least one backend confirmed the "clear" operation |
138
|
|
|
return $hasDeletedOnce; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param CacheItemInterface $item |
143
|
|
|
* @return bool |
144
|
|
|
* @throws InvalidArgumentException |
145
|
|
|
* @throws PhpfastcacheReplicationException |
146
|
|
|
*/ |
147
|
|
|
public function save(CacheItemInterface $item): bool |
148
|
|
|
{ |
149
|
|
|
/** @var ExtendedCacheItemInterface $item */ |
150
|
|
|
$hasSavedOnce = false; |
151
|
|
|
$eCount = 0; |
152
|
|
|
|
153
|
|
|
foreach ($this->clusterPools as $driverPool) { |
154
|
|
|
try { |
155
|
|
|
$poolItem = $this->getStandardizedItem($item, $driverPool); |
156
|
|
|
if ($result = $driverPool->save($poolItem)) { |
157
|
|
|
$hasSavedOnce = $result; |
158
|
|
|
} |
159
|
|
|
} catch (PhpfastcacheExceptionInterface) { |
160
|
|
|
$eCount++; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (\count($this->clusterPools) <= $eCount) { |
165
|
|
|
throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
166
|
|
|
} |
167
|
|
|
// Return true only if at least one backend confirmed the "commit" operation |
168
|
|
|
return $hasSavedOnce; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritDoc |
173
|
|
|
* @throws PhpfastcacheReplicationException |
174
|
|
|
*/ |
175
|
|
|
public function commit(): bool |
176
|
|
|
{ |
177
|
|
|
$hasCommitOnce = false; |
178
|
|
|
$eCount = 0; |
179
|
|
|
|
180
|
|
|
foreach ($this->clusterPools as $driverPool) { |
181
|
|
|
try { |
182
|
|
|
if ($result = $driverPool->commit()) { |
183
|
|
|
$hasCommitOnce = $result; |
184
|
|
|
} |
185
|
|
|
} catch (PhpfastcacheExceptionInterface) { |
186
|
|
|
$eCount++; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (\count($this->clusterPools) <= $eCount) { |
191
|
|
|
throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
192
|
|
|
} |
193
|
|
|
// Return true only if at least one backend confirmed the "commit" operation |
194
|
|
|
return $hasCommitOnce; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|