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\MasterSlaveReplication; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Cluster\ClusterPoolAbstract; |
19
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
20
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; |
21
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException; |
22
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheExceptionInterface; |
23
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
24
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException; |
25
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheReplicationException; |
26
|
|
|
use Psr\Cache\CacheItemInterface; |
27
|
|
|
use ReflectionException; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class MasterSlaveReplicationCluster |
32
|
|
|
* @package Phpfastcache\Cluster\Drivers\MasterSlaveReplication |
33
|
|
|
*/ |
34
|
|
|
class MasterSlaveReplicationCluster extends ClusterPoolAbstract |
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
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|