@@ -18,16 +18,16 @@ |
||
| 18 | 18 | |
| 19 | 19 | class SapiDetector |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @SuppressWarnings(PHPMD.Superglobals) |
|
| 23 | - */ |
|
| 24 | - public static function isWebScript(): bool |
|
| 25 | - { |
|
| 26 | - return \PHP_SAPI === 'apache2handler' || str_contains(\PHP_SAPI, 'handler') || isset($_SERVER['REQUEST_METHOD']); |
|
| 27 | - } |
|
| 21 | + /** |
|
| 22 | + * @SuppressWarnings(PHPMD.Superglobals) |
|
| 23 | + */ |
|
| 24 | + public static function isWebScript(): bool |
|
| 25 | + { |
|
| 26 | + return \PHP_SAPI === 'apache2handler' || str_contains(\PHP_SAPI, 'handler') || isset($_SERVER['REQUEST_METHOD']); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public static function isCliScript(): bool |
|
| 30 | - { |
|
| 31 | - return (\PHP_SAPI === 'cli') || \defined('STDIN'); |
|
| 32 | - } |
|
| 29 | + public static function isCliScript(): bool |
|
| 30 | + { |
|
| 31 | + return (\PHP_SAPI === 'cli') || \defined('STDIN'); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -18,31 +18,31 @@ |
||
| 18 | 18 | |
| 19 | 19 | trait MemcacheDriverCollisionDetectorTrait |
| 20 | 20 | { |
| 21 | - protected static string $driverUsed; |
|
| 21 | + protected static string $driverUsed; |
|
| 22 | 22 | |
| 23 | - public static function checkCollision(string $driverName): bool |
|
| 24 | - { |
|
| 25 | - $constantName = __NAMESPACE__ . '\MEMCACHE_DRIVER_USED'; |
|
| 23 | + public static function checkCollision(string $driverName): bool |
|
| 24 | + { |
|
| 25 | + $constantName = __NAMESPACE__ . '\MEMCACHE_DRIVER_USED'; |
|
| 26 | 26 | |
| 27 | - if ($driverName) { |
|
| 28 | - if (!defined($constantName)) { |
|
| 29 | - define($constantName, $driverName); |
|
| 27 | + if ($driverName) { |
|
| 28 | + if (!defined($constantName)) { |
|
| 29 | + define($constantName, $driverName); |
|
| 30 | 30 | |
| 31 | - return true; |
|
| 32 | - } |
|
| 31 | + return true; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - if (constant($constantName) !== $driverName) { |
|
| 35 | - trigger_error( |
|
| 36 | - 'Memcache collision detected, you used both Memcache and Memcached driver in your script, this may leads to unexpected behaviours', |
|
| 37 | - E_USER_WARNING |
|
| 38 | - ); |
|
| 34 | + if (constant($constantName) !== $driverName) { |
|
| 35 | + trigger_error( |
|
| 36 | + 'Memcache collision detected, you used both Memcache and Memcached driver in your script, this may leads to unexpected behaviours', |
|
| 37 | + E_USER_WARNING |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - return false; |
|
| 41 | - } |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - return true; |
|
| 44 | - } |
|
| 43 | + return true; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - return false; |
|
| 47 | - } |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -20,81 +20,81 @@ |
||
| 20 | 20 | |
| 21 | 21 | interface AggregatorInterface |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Full replication mechanism |
|
| 25 | - * |
|
| 26 | - * Read on first working (and synchronize if needed, no failure allowed), |
|
| 27 | - * Write on all (no failure allowed), |
|
| 28 | - * Delete on all (no failure allowed) |
|
| 29 | - * |
|
| 30 | - * Conflict on multiple reads: Keep first found item (but sync the others) |
|
| 31 | - * Cluster size: 2 minimum, unlimited |
|
| 32 | - */ |
|
| 33 | - public const STRATEGY_FULL_REPLICATION = 1; |
|
| 23 | + /** |
|
| 24 | + * Full replication mechanism |
|
| 25 | + * |
|
| 26 | + * Read on first working (and synchronize if needed, no failure allowed), |
|
| 27 | + * Write on all (no failure allowed), |
|
| 28 | + * Delete on all (no failure allowed) |
|
| 29 | + * |
|
| 30 | + * Conflict on multiple reads: Keep first found item (but sync the others) |
|
| 31 | + * Cluster size: 2 minimum, unlimited |
|
| 32 | + */ |
|
| 33 | + public const STRATEGY_FULL_REPLICATION = 1; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Semi replication mechanism |
|
| 37 | - * |
|
| 38 | - * Read first working (but do not synchronize, with partial failure allowed), |
|
| 39 | - * Write on all (with partial failure allowed) |
|
| 40 | - * Delete on all (with partial failure allowed) |
|
| 41 | - * |
|
| 42 | - * Conflict on multiple reads: Keep first found item |
|
| 43 | - * Cluster size: 2 minimum, unlimited |
|
| 44 | - */ |
|
| 45 | - public const STRATEGY_SEMI_REPLICATION = 2; |
|
| 35 | + /** |
|
| 36 | + * Semi replication mechanism |
|
| 37 | + * |
|
| 38 | + * Read first working (but do not synchronize, with partial failure allowed), |
|
| 39 | + * Write on all (with partial failure allowed) |
|
| 40 | + * Delete on all (with partial failure allowed) |
|
| 41 | + * |
|
| 42 | + * Conflict on multiple reads: Keep first found item |
|
| 43 | + * Cluster size: 2 minimum, unlimited |
|
| 44 | + */ |
|
| 45 | + public const STRATEGY_SEMI_REPLICATION = 2; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * First pool is master, second is slave |
|
| 49 | - * |
|
| 50 | - * Read from master (but do not synchronize, with master failure only allowed) |
|
| 51 | - * Write on all (with master failure only allowed) |
|
| 52 | - * Delete on all (with master failure only allowed) |
|
| 53 | - * |
|
| 54 | - * Conflict on multiple reads: No, master is exclusive source except if it fails |
|
| 55 | - * Cluster size: 2 exactly: Master & Slave (Exception if more or less) |
|
| 56 | - */ |
|
| 57 | - public const STRATEGY_MASTER_SLAVE = 4; |
|
| 47 | + /** |
|
| 48 | + * First pool is master, second is slave |
|
| 49 | + * |
|
| 50 | + * Read from master (but do not synchronize, with master failure only allowed) |
|
| 51 | + * Write on all (with master failure only allowed) |
|
| 52 | + * Delete on all (with master failure only allowed) |
|
| 53 | + * |
|
| 54 | + * Conflict on multiple reads: No, master is exclusive source except if it fails |
|
| 55 | + * Cluster size: 2 exactly: Master & Slave (Exception if more or less) |
|
| 56 | + */ |
|
| 57 | + public const STRATEGY_MASTER_SLAVE = 4; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Mostly used for development testing |
|
| 61 | - * |
|
| 62 | - * CRUD operations are made on a random-chosen backend from a given cluster. |
|
| 63 | - * This means you have 1 chance out of (n count of pools) to find an existing cache item |
|
| 64 | - * but also to write/delete a non-existing item. |
|
| 65 | - */ |
|
| 66 | - public const STRATEGY_RANDOM_REPLICATION = 8; |
|
| 59 | + /** |
|
| 60 | + * Mostly used for development testing |
|
| 61 | + * |
|
| 62 | + * CRUD operations are made on a random-chosen backend from a given cluster. |
|
| 63 | + * This means you have 1 chance out of (n count of pools) to find an existing cache item |
|
| 64 | + * but also to write/delete a non-existing item. |
|
| 65 | + */ |
|
| 66 | + public const STRATEGY_RANDOM_REPLICATION = 8; |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * AggregatorInterface constructor. |
|
| 70 | - * |
|
| 71 | - * @param string $clusterAggregatorName |
|
| 72 | - * @param AggregatablePoolInterface ...$driverPools |
|
| 73 | - */ |
|
| 74 | - public function __construct(string $clusterAggregatorName, AggregatablePoolInterface ...$driverPools); |
|
| 68 | + /** |
|
| 69 | + * AggregatorInterface constructor. |
|
| 70 | + * |
|
| 71 | + * @param string $clusterAggregatorName |
|
| 72 | + * @param AggregatablePoolInterface ...$driverPools |
|
| 73 | + */ |
|
| 74 | + public function __construct(string $clusterAggregatorName, AggregatablePoolInterface ...$driverPools); |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param int $strategy |
|
| 78 | - * |
|
| 79 | - * @return ClusterPoolInterface |
|
| 80 | - */ |
|
| 81 | - public function getCluster(int $strategy): ClusterPoolInterface; |
|
| 76 | + /** |
|
| 77 | + * @param int $strategy |
|
| 78 | + * |
|
| 79 | + * @return ClusterPoolInterface |
|
| 80 | + */ |
|
| 81 | + public function getCluster(int $strategy): ClusterPoolInterface; |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @param string $driverName |
|
| 85 | - * @param ConfigurationOption|NULL $driverConfig |
|
| 86 | - * |
|
| 87 | - * @return void |
|
| 88 | - */ |
|
| 89 | - public function aggregateDriverByName(string $driverName, ConfigurationOption $driverConfig = null): void; |
|
| 83 | + /** |
|
| 84 | + * @param string $driverName |
|
| 85 | + * @param ConfigurationOption|NULL $driverConfig |
|
| 86 | + * |
|
| 87 | + * @return void |
|
| 88 | + */ |
|
| 89 | + public function aggregateDriverByName(string $driverName, ConfigurationOption $driverConfig = null): void; |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * @param AggregatablePoolInterface $driverPool |
|
| 93 | - */ |
|
| 94 | - public function aggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
| 91 | + /** |
|
| 92 | + * @param AggregatablePoolInterface $driverPool |
|
| 93 | + */ |
|
| 94 | + public function aggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @param AggregatablePoolInterface $driverPool |
|
| 98 | - */ |
|
| 99 | - public function disaggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
| 96 | + /** |
|
| 97 | + * @param AggregatablePoolInterface $driverPool |
|
| 98 | + */ |
|
| 99 | + public function disaggregateDriver(AggregatablePoolInterface $driverPool): void; |
|
| 100 | 100 | } |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | |
| 21 | 21 | interface AggregatablePoolInterface extends ExtendedCacheItemPoolInterface |
| 22 | 22 | { |
| 23 | - public function isAggregatedBy(): ?ClusterPoolInterface; |
|
| 23 | + public function isAggregatedBy(): ?ClusterPoolInterface; |
|
| 24 | 24 | |
| 25 | - public function setAggregatedBy(ClusterPoolInterface $clusterPool): static; |
|
| 25 | + public function setAggregatedBy(ClusterPoolInterface $clusterPool): static; |
|
| 26 | 26 | } |
@@ -20,8 +20,8 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Item extends ItemAbstract |
| 22 | 22 | { |
| 23 | - protected function getDriverClass(): string |
|
| 24 | - { |
|
| 25 | - return Driver::class; |
|
| 26 | - } |
|
| 23 | + protected function getDriverClass(): string |
|
| 24 | + { |
|
| 25 | + return Driver::class; |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -24,165 +24,165 @@ |
||
| 24 | 24 | |
| 25 | 25 | class Driver extends ClusterPoolAbstract |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * @inheritDoc |
|
| 29 | - */ |
|
| 30 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
| 31 | - { |
|
| 32 | - /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */ |
|
| 33 | - $poolsToResync = []; |
|
| 34 | - /** @var ?ExtendedCacheItemInterface $item */ |
|
| 35 | - $item = null; |
|
| 36 | - |
|
| 37 | - foreach ($this->clusterPools as $driverPool) { |
|
| 38 | - $poolItem = $driverPool->getItem($key); |
|
| 39 | - if ($poolItem->isHit()) { |
|
| 40 | - if (!$item) { |
|
| 41 | - $item = $poolItem; |
|
| 42 | - continue; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $itemData = $item->get(); |
|
| 46 | - $poolItemData = $poolItem->get(); |
|
| 47 | - |
|
| 48 | - if (\is_object($itemData)) { |
|
| 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 | - $this->resynchronizePool($poolsToResync, $key, $item); |
|
| 63 | - |
|
| 64 | - if ($item === null) { |
|
| 65 | - $item = new Item($this, $key, $this->getEventManager()); |
|
| 66 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return $this->getStandardizedItem($item, $this); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @inheritDoc |
|
| 74 | - */ |
|
| 75 | - public function hasItem(string $key): bool |
|
| 76 | - { |
|
| 77 | - foreach ($this->clusterPools as $driverPool) { |
|
| 78 | - $poolItem = $driverPool->getItem($key); |
|
| 79 | - if ($poolItem->isHit()) { |
|
| 80 | - return true; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @inheritDoc |
|
| 89 | - */ |
|
| 90 | - public function clear(): bool |
|
| 91 | - { |
|
| 92 | - $hasClearedOnce = false; |
|
| 93 | - foreach ($this->clusterPools as $driverPool) { |
|
| 94 | - if ($result = $driverPool->clear()) { |
|
| 95 | - $hasClearedOnce = $result; |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - // Return true only if at least one backend confirmed the "clear" operation |
|
| 99 | - return $hasClearedOnce; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @inheritDoc |
|
| 104 | - */ |
|
| 105 | - public function deleteItem(string $key): bool |
|
| 106 | - { |
|
| 107 | - $hasDeletedOnce = false; |
|
| 108 | - foreach ($this->clusterPools as $driverPool) { |
|
| 109 | - if ($result = $driverPool->deleteItem($key)) { |
|
| 110 | - $hasDeletedOnce = $result; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - // Return true only if at least one backend confirmed the "clear" operation |
|
| 114 | - return $hasDeletedOnce; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @inheritDoc |
|
| 119 | - */ |
|
| 120 | - public function save(CacheItemInterface $item): bool |
|
| 121 | - { |
|
| 122 | - /** @var ExtendedCacheItemInterface $item */ |
|
| 123 | - $hasSavedOnce = false; |
|
| 124 | - foreach ($this->clusterPools as $driverPool) { |
|
| 125 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
| 126 | - if ($result = $driverPool->save($poolItem)) { |
|
| 127 | - $hasSavedOnce = $result; |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - // Return true only if at least one backend confirmed the "commit" operation |
|
| 131 | - return $hasSavedOnce; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @inheritDoc |
|
| 136 | - */ |
|
| 137 | - public function saveDeferred(CacheItemInterface $item): bool |
|
| 138 | - { |
|
| 139 | - /** @var ExtendedCacheItemInterface $item */ |
|
| 140 | - $hasSavedOnce = false; |
|
| 141 | - foreach ($this->clusterPools as $driverPool) { |
|
| 142 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
| 143 | - if ($result = $driverPool->saveDeferred($poolItem)) { |
|
| 144 | - $hasSavedOnce = $result; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - // Return true only if at least one backend confirmed the "commit" operation |
|
| 148 | - return $hasSavedOnce; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @inheritDoc |
|
| 153 | - */ |
|
| 154 | - public function commit(): bool |
|
| 155 | - { |
|
| 156 | - $hasCommitOnce = false; |
|
| 157 | - foreach ($this->clusterPools as $driverPool) { |
|
| 158 | - if ($result = $driverPool->commit()) { |
|
| 159 | - $hasCommitOnce = $result; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - // Return true only if at least one backend confirmed the "commit" operation |
|
| 163 | - return $hasCommitOnce; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param ExtendedCacheItemPoolInterface[] $poolsToResynchronize |
|
| 168 | - * @param string $key |
|
| 169 | - * @param ?ExtendedCacheItemInterface $item |
|
| 170 | - * @return void |
|
| 171 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
|
| 172 | - */ |
|
| 173 | - protected function resynchronizePool(array $poolsToResynchronize, string $key, ?ExtendedCacheItemInterface $item): void |
|
| 174 | - { |
|
| 175 | - if ($item && $item->isHit() && \count($poolsToResynchronize) < \count($this->clusterPools)) { |
|
| 176 | - foreach ($poolsToResynchronize as $poolToResynchronize) { |
|
| 177 | - $poolItem = $poolToResynchronize->getItem($key); |
|
| 178 | - $poolItem->setEventManager($this->getEventManager()) |
|
| 179 | - ->set($item->get()) |
|
| 180 | - ->setHit($item->isHit()) |
|
| 181 | - ->setTags($item->getTags()) |
|
| 182 | - ->expiresAt($item->getExpirationDate()) |
|
| 183 | - ->setDriver($poolToResynchronize); |
|
| 184 | - $poolToResynchronize->save($poolItem); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } |
|
| 27 | + /** |
|
| 28 | + * @inheritDoc |
|
| 29 | + */ |
|
| 30 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
| 31 | + { |
|
| 32 | + /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */ |
|
| 33 | + $poolsToResync = []; |
|
| 34 | + /** @var ?ExtendedCacheItemInterface $item */ |
|
| 35 | + $item = null; |
|
| 36 | + |
|
| 37 | + foreach ($this->clusterPools as $driverPool) { |
|
| 38 | + $poolItem = $driverPool->getItem($key); |
|
| 39 | + if ($poolItem->isHit()) { |
|
| 40 | + if (!$item) { |
|
| 41 | + $item = $poolItem; |
|
| 42 | + continue; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $itemData = $item->get(); |
|
| 46 | + $poolItemData = $poolItem->get(); |
|
| 47 | + |
|
| 48 | + if (\is_object($itemData)) { |
|
| 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 | + $this->resynchronizePool($poolsToResync, $key, $item); |
|
| 63 | + |
|
| 64 | + if ($item === null) { |
|
| 65 | + $item = new Item($this, $key, $this->getEventManager()); |
|
| 66 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return $this->getStandardizedItem($item, $this); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @inheritDoc |
|
| 74 | + */ |
|
| 75 | + public function hasItem(string $key): bool |
|
| 76 | + { |
|
| 77 | + foreach ($this->clusterPools as $driverPool) { |
|
| 78 | + $poolItem = $driverPool->getItem($key); |
|
| 79 | + if ($poolItem->isHit()) { |
|
| 80 | + return true; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @inheritDoc |
|
| 89 | + */ |
|
| 90 | + public function clear(): bool |
|
| 91 | + { |
|
| 92 | + $hasClearedOnce = false; |
|
| 93 | + foreach ($this->clusterPools as $driverPool) { |
|
| 94 | + if ($result = $driverPool->clear()) { |
|
| 95 | + $hasClearedOnce = $result; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + // Return true only if at least one backend confirmed the "clear" operation |
|
| 99 | + return $hasClearedOnce; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @inheritDoc |
|
| 104 | + */ |
|
| 105 | + public function deleteItem(string $key): bool |
|
| 106 | + { |
|
| 107 | + $hasDeletedOnce = false; |
|
| 108 | + foreach ($this->clusterPools as $driverPool) { |
|
| 109 | + if ($result = $driverPool->deleteItem($key)) { |
|
| 110 | + $hasDeletedOnce = $result; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + // Return true only if at least one backend confirmed the "clear" operation |
|
| 114 | + return $hasDeletedOnce; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @inheritDoc |
|
| 119 | + */ |
|
| 120 | + public function save(CacheItemInterface $item): bool |
|
| 121 | + { |
|
| 122 | + /** @var ExtendedCacheItemInterface $item */ |
|
| 123 | + $hasSavedOnce = false; |
|
| 124 | + foreach ($this->clusterPools as $driverPool) { |
|
| 125 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
| 126 | + if ($result = $driverPool->save($poolItem)) { |
|
| 127 | + $hasSavedOnce = $result; |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + // Return true only if at least one backend confirmed the "commit" operation |
|
| 131 | + return $hasSavedOnce; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @inheritDoc |
|
| 136 | + */ |
|
| 137 | + public function saveDeferred(CacheItemInterface $item): bool |
|
| 138 | + { |
|
| 139 | + /** @var ExtendedCacheItemInterface $item */ |
|
| 140 | + $hasSavedOnce = false; |
|
| 141 | + foreach ($this->clusterPools as $driverPool) { |
|
| 142 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
| 143 | + if ($result = $driverPool->saveDeferred($poolItem)) { |
|
| 144 | + $hasSavedOnce = $result; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + // Return true only if at least one backend confirmed the "commit" operation |
|
| 148 | + return $hasSavedOnce; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @inheritDoc |
|
| 153 | + */ |
|
| 154 | + public function commit(): bool |
|
| 155 | + { |
|
| 156 | + $hasCommitOnce = false; |
|
| 157 | + foreach ($this->clusterPools as $driverPool) { |
|
| 158 | + if ($result = $driverPool->commit()) { |
|
| 159 | + $hasCommitOnce = $result; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + // Return true only if at least one backend confirmed the "commit" operation |
|
| 163 | + return $hasCommitOnce; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param ExtendedCacheItemPoolInterface[] $poolsToResynchronize |
|
| 168 | + * @param string $key |
|
| 169 | + * @param ?ExtendedCacheItemInterface $item |
|
| 170 | + * @return void |
|
| 171 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
|
| 172 | + */ |
|
| 173 | + protected function resynchronizePool(array $poolsToResynchronize, string $key, ?ExtendedCacheItemInterface $item): void |
|
| 174 | + { |
|
| 175 | + if ($item && $item->isHit() && \count($poolsToResynchronize) < \count($this->clusterPools)) { |
|
| 176 | + foreach ($poolsToResynchronize as $poolToResynchronize) { |
|
| 177 | + $poolItem = $poolToResynchronize->getItem($key); |
|
| 178 | + $poolItem->setEventManager($this->getEventManager()) |
|
| 179 | + ->set($item->get()) |
|
| 180 | + ->setHit($item->isHit()) |
|
| 181 | + ->setTags($item->getTags()) |
|
| 182 | + ->expiresAt($item->getExpirationDate()) |
|
| 183 | + ->setDriver($poolToResynchronize); |
|
| 184 | + $poolToResynchronize->save($poolItem); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | 188 | } |
@@ -25,41 +25,41 @@ |
||
| 25 | 25 | |
| 26 | 26 | class Driver extends MasterSlaveReplicationDriver |
| 27 | 27 | { |
| 28 | - /** |
|
| 29 | - * RandomReplicationCluster constructor. |
|
| 30 | - * @param string $clusterName |
|
| 31 | - * @param EventManagerInterface $em |
|
| 32 | - * @param AggregatablePoolInterface ...$driverPools |
|
| 33 | - * @throws ReflectionException |
|
| 34 | - */ |
|
| 35 | - public function __construct( |
|
| 36 | - string $clusterName, |
|
| 37 | - EventManagerInterface $em, |
|
| 38 | - AggregatablePoolInterface ...$driverPools |
|
| 39 | - ) { |
|
| 40 | - /** Straight call to ClusterPoolAbstract constructor */ |
|
| 41 | - (new ReflectionMethod( |
|
| 42 | - \get_parent_class(\get_parent_class($this)), |
|
| 43 | - __FUNCTION__ |
|
| 44 | - ))->invoke($this, $clusterName, $em, ...$driverPools); |
|
| 28 | + /** |
|
| 29 | + * RandomReplicationCluster constructor. |
|
| 30 | + * @param string $clusterName |
|
| 31 | + * @param EventManagerInterface $em |
|
| 32 | + * @param AggregatablePoolInterface ...$driverPools |
|
| 33 | + * @throws ReflectionException |
|
| 34 | + */ |
|
| 35 | + public function __construct( |
|
| 36 | + string $clusterName, |
|
| 37 | + EventManagerInterface $em, |
|
| 38 | + AggregatablePoolInterface ...$driverPools |
|
| 39 | + ) { |
|
| 40 | + /** Straight call to ClusterPoolAbstract constructor */ |
|
| 41 | + (new ReflectionMethod( |
|
| 42 | + \get_parent_class(\get_parent_class($this)), |
|
| 43 | + __FUNCTION__ |
|
| 44 | + ))->invoke($this, $clusterName, $em, ...$driverPools); |
|
| 45 | 45 | |
| 46 | - $randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)]; |
|
| 46 | + $randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)]; |
|
| 47 | 47 | |
| 48 | - $this->eventManager->dispatch( |
|
| 49 | - Event::CACHE_REPLICATION_RANDOM_POOL_CHOSEN, |
|
| 50 | - $this, |
|
| 51 | - $randomPool |
|
| 52 | - ); |
|
| 48 | + $this->eventManager->dispatch( |
|
| 49 | + Event::CACHE_REPLICATION_RANDOM_POOL_CHOSEN, |
|
| 50 | + $this, |
|
| 51 | + $randomPool |
|
| 52 | + ); |
|
| 53 | 53 | |
| 54 | - $this->clusterPools = [$randomPool]; |
|
| 55 | - } |
|
| 54 | + $this->clusterPools = [$randomPool]; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @param callable $operation |
|
| 59 | - * @return mixed |
|
| 60 | - */ |
|
| 61 | - protected function makeOperation(callable $operation) |
|
| 62 | - { |
|
| 63 | - return $operation($this->getMasterPool()); |
|
| 64 | - } |
|
| 57 | + /** |
|
| 58 | + * @param callable $operation |
|
| 59 | + * @return mixed |
|
| 60 | + */ |
|
| 61 | + protected function makeOperation(callable $operation) |
|
| 62 | + { |
|
| 63 | + return $operation($this->getMasterPool()); |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -25,173 +25,173 @@ |
||
| 25 | 25 | |
| 26 | 26 | class Driver extends ClusterPoolAbstract |
| 27 | 27 | { |
| 28 | - /** |
|
| 29 | - * @inheritDoc |
|
| 30 | - * @throws InvalidArgumentException |
|
| 31 | - * @throws PhpfastcacheReplicationException |
|
| 32 | - */ |
|
| 33 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
| 34 | - { |
|
| 35 | - /** @var ?ExtendedCacheItemInterface $item */ |
|
| 36 | - $item = null; |
|
| 37 | - $eCount = 0; |
|
| 38 | - |
|
| 39 | - foreach ($this->clusterPools as $driverPool) { |
|
| 40 | - try { |
|
| 41 | - $poolItem = $driverPool->getItem($key); |
|
| 42 | - if (!$item && $poolItem->isHit()) { |
|
| 43 | - $item = $poolItem; |
|
| 44 | - break; |
|
| 45 | - } |
|
| 46 | - } catch (PhpfastcacheExceptionInterface) { |
|
| 47 | - $eCount++; |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - if (\count($this->clusterPools) <= $eCount) { |
|
| 52 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - if ($item === null) { |
|
| 56 | - $item = new Item($this, $key, $this->getEventManager()); |
|
| 57 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $this->getStandardizedItem($item, $this); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @inheritDoc |
|
| 65 | - * @throws PhpfastcacheReplicationException |
|
| 66 | - */ |
|
| 67 | - public function hasItem(string $key): bool |
|
| 68 | - { |
|
| 69 | - $eCount = 0; |
|
| 70 | - foreach ($this->clusterPools as $driverPool) { |
|
| 71 | - try { |
|
| 72 | - $poolItem = $driverPool->getItem($key); |
|
| 73 | - if ($poolItem->isHit()) { |
|
| 74 | - return true; |
|
| 75 | - } |
|
| 76 | - } catch (PhpfastcacheExceptionInterface) { |
|
| 77 | - $eCount++; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if (\count($this->clusterPools) <= $eCount) { |
|
| 82 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return false; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @inheritDoc |
|
| 90 | - * @throws PhpfastcacheReplicationException |
|
| 91 | - */ |
|
| 92 | - public function clear(): bool |
|
| 93 | - { |
|
| 94 | - $hasClearedOnce = false; |
|
| 95 | - $eCount = 0; |
|
| 96 | - |
|
| 97 | - foreach ($this->clusterPools as $driverPool) { |
|
| 98 | - try { |
|
| 99 | - if ($result = $driverPool->clear()) { |
|
| 100 | - $hasClearedOnce = $result; |
|
| 101 | - } |
|
| 102 | - } catch (PhpfastcacheExceptionInterface) { |
|
| 103 | - $eCount++; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if (\count($this->clusterPools) <= $eCount) { |
|
| 108 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // Return true only if at least one backend confirmed the "clear" operation |
|
| 112 | - return $hasClearedOnce; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @inheritDoc |
|
| 117 | - * @throws PhpfastcacheReplicationException |
|
| 118 | - * @throws InvalidArgumentException |
|
| 119 | - */ |
|
| 120 | - public function deleteItem(string $key): bool |
|
| 121 | - { |
|
| 122 | - $hasDeletedOnce = false; |
|
| 123 | - $eCount = 0; |
|
| 124 | - |
|
| 125 | - foreach ($this->clusterPools as $driverPool) { |
|
| 126 | - try { |
|
| 127 | - if ($result = $driverPool->deleteItem($key)) { |
|
| 128 | - $hasDeletedOnce = $result; |
|
| 129 | - } |
|
| 130 | - } catch (PhpfastcacheExceptionInterface) { |
|
| 131 | - $eCount++; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - if (\count($this->clusterPools) <= $eCount) { |
|
| 136 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 137 | - } |
|
| 138 | - // Return true only if at least one backend confirmed the "clear" operation |
|
| 139 | - return $hasDeletedOnce; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param CacheItemInterface $item |
|
| 144 | - * @return bool |
|
| 145 | - * @throws InvalidArgumentException |
|
| 146 | - * @throws PhpfastcacheReplicationException |
|
| 147 | - */ |
|
| 148 | - public function save(CacheItemInterface $item): bool |
|
| 149 | - { |
|
| 150 | - /** @var ExtendedCacheItemInterface $item */ |
|
| 151 | - $hasSavedOnce = false; |
|
| 152 | - $eCount = 0; |
|
| 153 | - |
|
| 154 | - foreach ($this->clusterPools as $driverPool) { |
|
| 155 | - try { |
|
| 156 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
| 157 | - if ($result = $driverPool->save($poolItem)) { |
|
| 158 | - $hasSavedOnce = $result; |
|
| 159 | - } |
|
| 160 | - } catch (PhpfastcacheExceptionInterface) { |
|
| 161 | - $eCount++; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if (\count($this->clusterPools) <= $eCount) { |
|
| 166 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 167 | - } |
|
| 168 | - // Return true only if at least one backend confirmed the "commit" operation |
|
| 169 | - return $hasSavedOnce; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @inheritDoc |
|
| 174 | - * @throws PhpfastcacheReplicationException |
|
| 175 | - */ |
|
| 176 | - public function commit(): bool |
|
| 177 | - { |
|
| 178 | - $hasCommitOnce = false; |
|
| 179 | - $eCount = 0; |
|
| 180 | - |
|
| 181 | - foreach ($this->clusterPools as $driverPool) { |
|
| 182 | - try { |
|
| 183 | - if ($result = $driverPool->commit()) { |
|
| 184 | - $hasCommitOnce = $result; |
|
| 185 | - } |
|
| 186 | - } catch (PhpfastcacheExceptionInterface) { |
|
| 187 | - $eCount++; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if (\count($this->clusterPools) <= $eCount) { |
|
| 192 | - throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 193 | - } |
|
| 194 | - // Return true only if at least one backend confirmed the "commit" operation |
|
| 195 | - return $hasCommitOnce; |
|
| 196 | - } |
|
| 28 | + /** |
|
| 29 | + * @inheritDoc |
|
| 30 | + * @throws InvalidArgumentException |
|
| 31 | + * @throws PhpfastcacheReplicationException |
|
| 32 | + */ |
|
| 33 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
| 34 | + { |
|
| 35 | + /** @var ?ExtendedCacheItemInterface $item */ |
|
| 36 | + $item = null; |
|
| 37 | + $eCount = 0; |
|
| 38 | + |
|
| 39 | + foreach ($this->clusterPools as $driverPool) { |
|
| 40 | + try { |
|
| 41 | + $poolItem = $driverPool->getItem($key); |
|
| 42 | + if (!$item && $poolItem->isHit()) { |
|
| 43 | + $item = $poolItem; |
|
| 44 | + break; |
|
| 45 | + } |
|
| 46 | + } catch (PhpfastcacheExceptionInterface) { |
|
| 47 | + $eCount++; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + if (\count($this->clusterPools) <= $eCount) { |
|
| 52 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + if ($item === null) { |
|
| 56 | + $item = new Item($this, $key, $this->getEventManager()); |
|
| 57 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $this->getStandardizedItem($item, $this); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @inheritDoc |
|
| 65 | + * @throws PhpfastcacheReplicationException |
|
| 66 | + */ |
|
| 67 | + public function hasItem(string $key): bool |
|
| 68 | + { |
|
| 69 | + $eCount = 0; |
|
| 70 | + foreach ($this->clusterPools as $driverPool) { |
|
| 71 | + try { |
|
| 72 | + $poolItem = $driverPool->getItem($key); |
|
| 73 | + if ($poolItem->isHit()) { |
|
| 74 | + return true; |
|
| 75 | + } |
|
| 76 | + } catch (PhpfastcacheExceptionInterface) { |
|
| 77 | + $eCount++; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if (\count($this->clusterPools) <= $eCount) { |
|
| 82 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return false; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @inheritDoc |
|
| 90 | + * @throws PhpfastcacheReplicationException |
|
| 91 | + */ |
|
| 92 | + public function clear(): bool |
|
| 93 | + { |
|
| 94 | + $hasClearedOnce = false; |
|
| 95 | + $eCount = 0; |
|
| 96 | + |
|
| 97 | + foreach ($this->clusterPools as $driverPool) { |
|
| 98 | + try { |
|
| 99 | + if ($result = $driverPool->clear()) { |
|
| 100 | + $hasClearedOnce = $result; |
|
| 101 | + } |
|
| 102 | + } catch (PhpfastcacheExceptionInterface) { |
|
| 103 | + $eCount++; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if (\count($this->clusterPools) <= $eCount) { |
|
| 108 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // Return true only if at least one backend confirmed the "clear" operation |
|
| 112 | + return $hasClearedOnce; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @inheritDoc |
|
| 117 | + * @throws PhpfastcacheReplicationException |
|
| 118 | + * @throws InvalidArgumentException |
|
| 119 | + */ |
|
| 120 | + public function deleteItem(string $key): bool |
|
| 121 | + { |
|
| 122 | + $hasDeletedOnce = false; |
|
| 123 | + $eCount = 0; |
|
| 124 | + |
|
| 125 | + foreach ($this->clusterPools as $driverPool) { |
|
| 126 | + try { |
|
| 127 | + if ($result = $driverPool->deleteItem($key)) { |
|
| 128 | + $hasDeletedOnce = $result; |
|
| 129 | + } |
|
| 130 | + } catch (PhpfastcacheExceptionInterface) { |
|
| 131 | + $eCount++; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + if (\count($this->clusterPools) <= $eCount) { |
|
| 136 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 137 | + } |
|
| 138 | + // Return true only if at least one backend confirmed the "clear" operation |
|
| 139 | + return $hasDeletedOnce; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param CacheItemInterface $item |
|
| 144 | + * @return bool |
|
| 145 | + * @throws InvalidArgumentException |
|
| 146 | + * @throws PhpfastcacheReplicationException |
|
| 147 | + */ |
|
| 148 | + public function save(CacheItemInterface $item): bool |
|
| 149 | + { |
|
| 150 | + /** @var ExtendedCacheItemInterface $item */ |
|
| 151 | + $hasSavedOnce = false; |
|
| 152 | + $eCount = 0; |
|
| 153 | + |
|
| 154 | + foreach ($this->clusterPools as $driverPool) { |
|
| 155 | + try { |
|
| 156 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
| 157 | + if ($result = $driverPool->save($poolItem)) { |
|
| 158 | + $hasSavedOnce = $result; |
|
| 159 | + } |
|
| 160 | + } catch (PhpfastcacheExceptionInterface) { |
|
| 161 | + $eCount++; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if (\count($this->clusterPools) <= $eCount) { |
|
| 166 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 167 | + } |
|
| 168 | + // Return true only if at least one backend confirmed the "commit" operation |
|
| 169 | + return $hasSavedOnce; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @inheritDoc |
|
| 174 | + * @throws PhpfastcacheReplicationException |
|
| 175 | + */ |
|
| 176 | + public function commit(): bool |
|
| 177 | + { |
|
| 178 | + $hasCommitOnce = false; |
|
| 179 | + $eCount = 0; |
|
| 180 | + |
|
| 181 | + foreach ($this->clusterPools as $driverPool) { |
|
| 182 | + try { |
|
| 183 | + if ($result = $driverPool->commit()) { |
|
| 184 | + $hasCommitOnce = $result; |
|
| 185 | + } |
|
| 186 | + } catch (PhpfastcacheExceptionInterface) { |
|
| 187 | + $eCount++; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if (\count($this->clusterPools) <= $eCount) { |
|
| 192 | + throw new PhpfastcacheReplicationException('Every pools thrown an exception'); |
|
| 193 | + } |
|
| 194 | + // Return true only if at least one backend confirmed the "commit" operation |
|
| 195 | + return $hasCommitOnce; |
|
| 196 | + } |
|
| 197 | 197 | } |
@@ -34,131 +34,131 @@ |
||
| 34 | 34 | |
| 35 | 35 | class Driver extends ClusterPoolAbstract |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * MasterSlaveReplicationCluster constructor. |
|
| 39 | - * @param string $clusterName |
|
| 40 | - * @param EventManagerInterface $em |
|
| 41 | - * @param AggregatablePoolInterface ...$driverPools |
|
| 42 | - * @throws PhpfastcacheDriverCheckException |
|
| 43 | - * @throws PhpfastcacheDriverConnectException |
|
| 44 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 45 | - * @throws PhpfastcacheCoreException |
|
| 46 | - * @throws PhpfastcacheDriverException |
|
| 47 | - * @throws PhpfastcacheIOException |
|
| 48 | - */ |
|
| 49 | - public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
| 50 | - { |
|
| 51 | - if (\count($driverPools) !== 2) { |
|
| 52 | - throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - parent::__construct($clusterName, $em, ...$driverPools); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @inheritDoc |
|
| 60 | - */ |
|
| 61 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
| 62 | - { |
|
| 63 | - return $this->getStandardizedItem( |
|
| 64 | - $this->makeOperation(static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
| 65 | - return $pool->getItem($key); |
|
| 66 | - }) ?? (new Item($this, $key, $this->getEventManager()))->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())), |
|
| 67 | - $this |
|
| 68 | - ); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @param callable $operation |
|
| 73 | - * @return mixed |
|
| 74 | - * @throws PhpfastcacheReplicationException |
|
| 75 | - */ |
|
| 76 | - protected function makeOperation(callable $operation) |
|
| 77 | - { |
|
| 78 | - try { |
|
| 79 | - return $operation($this->getMasterPool()); |
|
| 80 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
| 81 | - try { |
|
| 82 | - $this->eventManager->dispatch( |
|
| 83 | - Event::CACHE_REPLICATION_SLAVE_FALLBACK, |
|
| 84 | - $this, |
|
| 85 | - \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
| 86 | - ); |
|
| 87 | - return $operation($this->getSlavePool()); |
|
| 88 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
| 89 | - throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @return AggregatablePoolInterface |
|
| 96 | - */ |
|
| 97 | - protected function getMasterPool(): AggregatablePoolInterface |
|
| 98 | - { |
|
| 99 | - return $this->clusterPools[0]; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return AggregatablePoolInterface |
|
| 104 | - */ |
|
| 105 | - protected function getSlavePool(): AggregatablePoolInterface |
|
| 106 | - { |
|
| 107 | - return $this->clusterPools[1]; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @inheritDoc |
|
| 112 | - */ |
|
| 113 | - public function hasItem(string $key): bool |
|
| 114 | - { |
|
| 115 | - return $this->makeOperation( |
|
| 116 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->hasItem($key) |
|
| 117 | - ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @inheritDoc |
|
| 122 | - */ |
|
| 123 | - public function clear(): bool |
|
| 124 | - { |
|
| 125 | - return $this->makeOperation( |
|
| 126 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->clear() |
|
| 127 | - ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @inheritDoc |
|
| 132 | - */ |
|
| 133 | - public function deleteItem(string $key): bool |
|
| 134 | - { |
|
| 135 | - return $this->makeOperation( |
|
| 136 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->deleteItem($key) |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @inheritDoc |
|
| 142 | - */ |
|
| 143 | - public function save(CacheItemInterface $item): bool |
|
| 144 | - { |
|
| 145 | - return $this->makeOperation( |
|
| 146 | - function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
| 147 | - /** @var ExtendedCacheItemInterface $item */ |
|
| 148 | - $item->setHit(true); |
|
| 149 | - return $pool->save($this->getStandardizedItem($item, $pool)); |
|
| 150 | - } |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @inheritDoc |
|
| 157 | - */ |
|
| 158 | - public function commit(): bool |
|
| 159 | - { |
|
| 160 | - return $this->makeOperation( |
|
| 161 | - static fn (ExtendedCacheItemPoolInterface $pool) => $pool->commit() |
|
| 162 | - ); |
|
| 163 | - } |
|
| 37 | + /** |
|
| 38 | + * MasterSlaveReplicationCluster constructor. |
|
| 39 | + * @param string $clusterName |
|
| 40 | + * @param EventManagerInterface $em |
|
| 41 | + * @param AggregatablePoolInterface ...$driverPools |
|
| 42 | + * @throws PhpfastcacheDriverCheckException |
|
| 43 | + * @throws PhpfastcacheDriverConnectException |
|
| 44 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 45 | + * @throws PhpfastcacheCoreException |
|
| 46 | + * @throws PhpfastcacheDriverException |
|
| 47 | + * @throws PhpfastcacheIOException |
|
| 48 | + */ |
|
| 49 | + public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools) |
|
| 50 | + { |
|
| 51 | + if (\count($driverPools) !== 2) { |
|
| 52 | + throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + parent::__construct($clusterName, $em, ...$driverPools); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @inheritDoc |
|
| 60 | + */ |
|
| 61 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
| 62 | + { |
|
| 63 | + return $this->getStandardizedItem( |
|
| 64 | + $this->makeOperation(static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
| 65 | + return $pool->getItem($key); |
|
| 66 | + }) ?? (new Item($this, $key, $this->getEventManager()))->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())), |
|
| 67 | + $this |
|
| 68 | + ); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @param callable $operation |
|
| 73 | + * @return mixed |
|
| 74 | + * @throws PhpfastcacheReplicationException |
|
| 75 | + */ |
|
| 76 | + protected function makeOperation(callable $operation) |
|
| 77 | + { |
|
| 78 | + try { |
|
| 79 | + return $operation($this->getMasterPool()); |
|
| 80 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
| 81 | + try { |
|
| 82 | + $this->eventManager->dispatch( |
|
| 83 | + Event::CACHE_REPLICATION_SLAVE_FALLBACK, |
|
| 84 | + $this, |
|
| 85 | + \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
| 86 | + ); |
|
| 87 | + return $operation($this->getSlavePool()); |
|
| 88 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
| 89 | + throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @return AggregatablePoolInterface |
|
| 96 | + */ |
|
| 97 | + protected function getMasterPool(): AggregatablePoolInterface |
|
| 98 | + { |
|
| 99 | + return $this->clusterPools[0]; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return AggregatablePoolInterface |
|
| 104 | + */ |
|
| 105 | + protected function getSlavePool(): AggregatablePoolInterface |
|
| 106 | + { |
|
| 107 | + return $this->clusterPools[1]; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @inheritDoc |
|
| 112 | + */ |
|
| 113 | + public function hasItem(string $key): bool |
|
| 114 | + { |
|
| 115 | + return $this->makeOperation( |
|
| 116 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->hasItem($key) |
|
| 117 | + ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @inheritDoc |
|
| 122 | + */ |
|
| 123 | + public function clear(): bool |
|
| 124 | + { |
|
| 125 | + return $this->makeOperation( |
|
| 126 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->clear() |
|
| 127 | + ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @inheritDoc |
|
| 132 | + */ |
|
| 133 | + public function deleteItem(string $key): bool |
|
| 134 | + { |
|
| 135 | + return $this->makeOperation( |
|
| 136 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->deleteItem($key) |
|
| 137 | + ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @inheritDoc |
|
| 142 | + */ |
|
| 143 | + public function save(CacheItemInterface $item): bool |
|
| 144 | + { |
|
| 145 | + return $this->makeOperation( |
|
| 146 | + function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
| 147 | + /** @var ExtendedCacheItemInterface $item */ |
|
| 148 | + $item->setHit(true); |
|
| 149 | + return $pool->save($this->getStandardizedItem($item, $pool)); |
|
| 150 | + } |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @inheritDoc |
|
| 157 | + */ |
|
| 158 | + public function commit(): bool |
|
| 159 | + { |
|
| 160 | + return $this->makeOperation( |
|
| 161 | + static fn (ExtendedCacheItemPoolInterface $pool) => $pool->commit() |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | 164 | } |