Skip to content

Commit 56991e0

Browse files
committed
Increased API up to 4.2.0 by updating \Phpfastcache\Cluster\AggregatablePoolInterface
1 parent 39dd2f0 commit 56991e0

10 files changed

Lines changed: 59 additions & 13 deletions

File tree

CHANGELOG_API.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.2.0
2+
- Created method `\Phpfastcache\Cluster\AggregatablePoolInterface::isAggregatedBy(): ?ClusterPoolInterface` which will return the aggregator object for Cluster aggregators
3+
- Created method `\Phpfastcache\Cluster\AggregatablePoolInterface::setAggregatedBy(ClusterPoolInterface $clusterPool): static` which will allow to set the aggregator object
4+
15
## 4.1.0
26
- Created `\Phpfastcache\Event\EventInterface` which will be used for `Phpfastcache\Event\Event` and any `Phpfastcache\Drivers\xxxxxxx\Event` classes
37
- Extended `CacheItemPoolInterface::save()` with `ExtendedCacheItemPoolInterface::save()` for re-typing

lib/Phpfastcache/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Api
2828
{
2929
use UninstanciableObjectTrait;
3030

31-
protected static string $version = '4.1.0';
31+
protected static string $version = '4.2.0';
3232

3333
/**
3434
* This method will return the current

lib/Phpfastcache/Cluster/AggregatablePoolInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@
2020

2121
interface AggregatablePoolInterface extends ExtendedCacheItemPoolInterface
2222
{
23+
public function isAggregatedBy(): ?ClusterPoolInterface;
24+
25+
public function setAggregatedBy(ClusterPoolInterface $clusterPool): static;
2326
}

lib/Phpfastcache/Cluster/ClusterPoolAbstract.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ abstract class ClusterPoolAbstract implements ClusterPoolInterface
5353
];
5454

5555
/**
56-
* @var ExtendedCacheItemPoolInterface[]
56+
* @var AggregatablePoolInterface[]
5757
*/
5858
protected array $clusterPools;
5959

@@ -69,14 +69,22 @@ abstract class ClusterPoolAbstract implements ClusterPoolInterface
6969
* @throws PhpfastcacheDriverException
7070
* @throws PhpfastcacheIOException
7171
*/
72-
public function __construct(string $clusterName, EventManagerInterface $em, ExtendedCacheItemPoolInterface ...$driverPools)
72+
public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools)
7373
{
7474
if (count($driverPools) < 2) {
7575
throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.');
7676
}
7777
$this->clusterPools = $driverPools;
7878
$this->__parentConstruct(new ConfigurationOption(), $clusterName, $em);
7979
$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+
}
8088
}
8189

8290
/**

lib/Phpfastcache/Cluster/ClusterPoolInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
interface ClusterPoolInterface extends ExtendedCacheItemPoolInterface
2323
{
2424
/**
25-
* @return ExtendedCacheItemPoolInterface[]
25+
* @return AggregatablePoolInterface[]
2626
*/
2727
public function getClusterPools(): array;
2828

lib/Phpfastcache/Cluster/Drivers/MasterSlaveReplication/Driver.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace Phpfastcache\Cluster\Drivers\MasterSlaveReplication;
1818

19+
use Phpfastcache\Cluster\AggregatablePoolInterface;
1920
use Phpfastcache\Cluster\ClusterPoolAbstract;
2021
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
2122
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
@@ -37,15 +38,15 @@ class Driver extends ClusterPoolAbstract
3738
* MasterSlaveReplicationCluster constructor.
3839
* @param string $clusterName
3940
* @param EventManagerInterface $em
40-
* @param ExtendedCacheItemPoolInterface ...$driverPools
41+
* @param AggregatablePoolInterface ...$driverPools
4142
* @throws PhpfastcacheDriverCheckException
4243
* @throws PhpfastcacheDriverConnectException
4344
* @throws PhpfastcacheInvalidArgumentException
4445
* @throws PhpfastcacheCoreException
4546
* @throws PhpfastcacheDriverException
4647
* @throws PhpfastcacheIOException
4748
*/
48-
public function __construct(string $clusterName, EventManagerInterface $em, ExtendedCacheItemPoolInterface ...$driverPools)
49+
public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools)
4950
{
5051
if (\count($driverPools) !== 2) {
5152
throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.');
@@ -91,17 +92,17 @@ protected function makeOperation(callable $operation)
9192
}
9293

9394
/**
94-
* @return ExtendedCacheItemPoolInterface
95+
* @return AggregatablePoolInterface
9596
*/
96-
protected function getMasterPool(): ExtendedCacheItemPoolInterface
97+
protected function getMasterPool(): AggregatablePoolInterface
9798
{
9899
return $this->clusterPools[0];
99100
}
100101

101102
/**
102-
* @return ExtendedCacheItemPoolInterface
103+
* @return AggregatablePoolInterface
103104
*/
104-
protected function getSlavePool(): ExtendedCacheItemPoolInterface
105+
protected function getSlavePool(): AggregatablePoolInterface
105106
{
106107
return $this->clusterPools[1];
107108
}

lib/Phpfastcache/Cluster/Drivers/RandomReplication/Driver.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
namespace Phpfastcache\Cluster\Drivers\RandomReplication;
1818

19+
use Phpfastcache\Cluster\AggregatablePoolInterface;
1920
use Phpfastcache\Cluster\Drivers\MasterSlaveReplication\Driver as MasterSlaveReplicationDriver;
20-
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
2121
use Phpfastcache\Event\Event;
2222
use Phpfastcache\Event\EventManagerInterface;
2323
use ReflectionException;
@@ -29,11 +29,12 @@ class Driver extends MasterSlaveReplicationDriver
2929
* RandomReplicationCluster constructor.
3030
* @param string $clusterName
3131
* @param EventManagerInterface $em
32-
* @param ExtendedCacheItemPoolInterface ...$driverPools
32+
* @param AggregatablePoolInterface ...$driverPools
3333
* @throws ReflectionException
3434
*/
35-
public function __construct(string $clusterName, EventManagerInterface $em, ExtendedCacheItemPoolInterface ...$driverPools)
35+
public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools)
3636
{
37+
/** Straight call to ClusterPoolAbstract constructor */
3738
(new ReflectionMethod(\get_parent_class(\get_parent_class($this)), __FUNCTION__))
3839
->invoke($this, $clusterName, $em, ...$driverPools);
3940
$randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Phpfastcache\Core\Pool;
4+
5+
use Phpfastcache\Cluster\ClusterPoolInterface;
6+
7+
trait AggregatablePoolTrait
8+
{
9+
protected ?ClusterPoolInterface $clusterPool = null;
10+
11+
public function isAggregatedBy(): ?ClusterPoolInterface
12+
{
13+
return $this->clusterPool;
14+
}
15+
16+
public function setAggregatedBy(ClusterPoolInterface $clusterPool): static
17+
{
18+
$this->clusterPool = $clusterPool;
19+
20+
return $this;
21+
}
22+
}

lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
trait ExtendedCacheItemPoolTrait
2929
{
3030
use CacheItemPoolTrait;
31+
use AggregatablePoolTrait;
3132

3233
/**
3334
* @inheritDoc

tests/ClusterRandomReplication.test.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@
3535

3636
$testHelper->runCRUDTests($cluster);
3737

38+
if($cluster->getClusterPools()[0]->isAggregatedBy() === $cluster){
39+
$testHelper->assertPass('The cluster aggregator set is the same as the cluster container.');
40+
} else {
41+
$testHelper->assertFail('The cluster aggregator set is NOT the same as the cluster container.');
42+
}
43+
3844
$testHelper->terminateTest();

0 commit comments

Comments
 (0)