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\RandomReplication; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Cluster\Drivers\MasterSlaveReplication\MasterSlaveReplicationCluster; |
19
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
20
|
|
|
use ReflectionException; |
21
|
|
|
use ReflectionMethod; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class MasterSlaveReplicationCluster |
26
|
|
|
* @package Phpfastcache\Cluster\Drivers\MasterSlaveReplication |
27
|
|
|
*/ |
28
|
|
|
class RandomReplicationCluster extends MasterSlaveReplicationCluster |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* RandomReplicationCluster constructor. |
32
|
|
|
* @param string $clusterName |
33
|
|
|
* @param ExtendedCacheItemPoolInterface ...$driverPools |
34
|
|
|
* @throws ReflectionException |
35
|
|
|
*/ |
36
|
|
|
public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
37
|
|
|
{ |
38
|
|
|
(new ReflectionMethod(\get_parent_class(\get_parent_class($this)), __FUNCTION__)) |
39
|
|
|
->invoke($this, $clusterName, ...$driverPools); |
40
|
|
|
$randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)]; |
41
|
|
|
|
42
|
|
|
$this->eventManager->dispatch( |
43
|
|
|
'CacheReplicationRandomPoolChosen', |
44
|
|
|
$this, |
45
|
|
|
$randomPool |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$this->clusterPools = [$randomPool]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param callable $operation |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
protected function makeOperation(callable $operation) |
56
|
|
|
{ |
57
|
|
|
return $operation($this->getMasterPool()); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|