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\FullReplication; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Cluster\ClusterPoolAbstract; |
19
|
|
|
use Phpfastcache\Config\ConfigurationOption; |
20
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
21
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
22
|
|
|
use Psr\Cache\CacheItemInterface; |
23
|
|
|
|
24
|
|
|
class Driver extends ClusterPoolAbstract |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function getItem(string $key): ExtendedCacheItemInterface |
30
|
|
|
{ |
31
|
|
|
/** @var ExtendedCacheItemPoolInterface[] $poolsToResync */ |
32
|
|
|
$poolsToResync = []; |
33
|
|
|
/** @var ExtendedCacheItemInterface $item */ |
34
|
|
|
$item = null; |
35
|
|
|
|
36
|
|
|
foreach ($this->clusterPools as $driverPool) { |
37
|
|
|
$poolItem = $driverPool->getItem($key); |
38
|
|
|
if ($poolItem->isHit()) { |
39
|
|
|
if (!$item) { |
40
|
|
|
$item = $poolItem; |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$itemData = $item->get(); |
45
|
|
|
$poolItemData = $poolItem->get(); |
46
|
|
|
|
47
|
|
|
if (\is_object($itemData) |
48
|
|
|
) { |
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
|
|
|
if ($item && $item->isHit() && \count($poolsToResync) < \count($this->clusterPools)) { |
63
|
|
|
foreach ($poolsToResync as $poolToResync) { |
64
|
|
|
$poolItem = $poolToResync->getItem($key); |
65
|
|
|
$poolItem->setEventManager($this->getEventManager()) |
66
|
|
|
->set($item->get()) |
67
|
|
|
->setHit($item->isHit()) |
68
|
|
|
->setTags($item->getTags()) |
69
|
|
|
->expiresAt($item->getExpirationDate()) |
70
|
|
|
->setDriver($poolToResync); |
71
|
|
|
$poolToResync->save($poolItem); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->getStandardizedItem($item ?? new Item($this, $key, $this->getEventManager()), $this); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function hasItem(string $key): bool |
82
|
|
|
{ |
83
|
|
|
foreach ($this->clusterPools as $driverPool) { |
84
|
|
|
$poolItem = $driverPool->getItem($key); |
85
|
|
|
if ($poolItem->isHit()) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function clear(): bool |
97
|
|
|
{ |
98
|
|
|
$hasClearedOnce = false; |
99
|
|
|
foreach ($this->clusterPools as $driverPool) { |
100
|
|
|
if ($result = $driverPool->clear()) { |
101
|
|
|
$hasClearedOnce = $result; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
// Return true only if at least one backend confirmed the "clear" operation |
105
|
|
|
return $hasClearedOnce; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
|
|
public function deleteItem(string $key): bool |
112
|
|
|
{ |
113
|
|
|
$hasDeletedOnce = false; |
114
|
|
|
foreach ($this->clusterPools as $driverPool) { |
115
|
|
|
if ($result = $driverPool->deleteItem($key)) { |
116
|
|
|
$hasDeletedOnce = $result; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
// Return true only if at least one backend confirmed the "clear" operation |
120
|
|
|
return $hasDeletedOnce; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritDoc |
125
|
|
|
*/ |
126
|
|
|
public function save(CacheItemInterface $item): bool |
127
|
|
|
{ |
128
|
|
|
/** @var ExtendedCacheItemInterface $item */ |
129
|
|
|
$hasSavedOnce = false; |
130
|
|
|
foreach ($this->clusterPools as $driverPool) { |
131
|
|
|
$poolItem = $this->getStandardizedItem($item, $driverPool); |
132
|
|
|
if ($result = $driverPool->save($poolItem)) { |
133
|
|
|
$hasSavedOnce = $result; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
// Return true only if at least one backend confirmed the "commit" operation |
137
|
|
|
return $hasSavedOnce; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
|
|
public function saveDeferred(CacheItemInterface $item): bool |
144
|
|
|
{ |
145
|
|
|
/** @var ExtendedCacheItemInterface $item */ |
146
|
|
|
$hasSavedOnce = false; |
147
|
|
|
foreach ($this->clusterPools as $driverPool) { |
148
|
|
|
$poolItem = $this->getStandardizedItem($item, $driverPool); |
149
|
|
|
if ($result = $driverPool->saveDeferred($poolItem)) { |
150
|
|
|
$hasSavedOnce = $result; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
// Return true only if at least one backend confirmed the "commit" operation |
154
|
|
|
return $hasSavedOnce; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritDoc |
159
|
|
|
*/ |
160
|
|
|
public function commit(): bool |
161
|
|
|
{ |
162
|
|
|
$hasCommitOnce = false; |
163
|
|
|
foreach ($this->clusterPools as $driverPool) { |
164
|
|
|
if ($result = $driverPool->commit()) { |
165
|
|
|
$hasCommitOnce = $result; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
// Return true only if at least one backend confirmed the "commit" operation |
169
|
|
|
return $hasCommitOnce; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|