@@ -23,126 +23,126 @@ |
||
| 23 | 23 | |
| 24 | 24 | class Directory |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Get the directory size |
|
| 28 | - * @param string $directory |
|
| 29 | - * @param bool $includeDirAllocSize |
|
| 30 | - * @return int |
|
| 31 | - */ |
|
| 32 | - public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
| 33 | - { |
|
| 34 | - $size = 0; |
|
| 35 | - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
| 36 | - /** |
|
| 37 | - * @var SplFileInfo $file |
|
| 38 | - */ |
|
| 39 | - if ($file->isFile()) { |
|
| 40 | - $size += filesize($file->getRealPath()); |
|
| 41 | - } elseif ($includeDirAllocSize) { |
|
| 42 | - $size += $file->getSize(); |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - return $size; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param string $path |
|
| 51 | - * @return int |
|
| 52 | - */ |
|
| 53 | - public static function getFileCount(string $path): int |
|
| 54 | - { |
|
| 55 | - $count = 0; |
|
| 56 | - $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
| 57 | - foreach ($objects as $object) { |
|
| 58 | - /** |
|
| 59 | - * @var SplFileInfo $object |
|
| 60 | - */ |
|
| 61 | - if ($object->isFile()) { |
|
| 62 | - $count++; |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return $count; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Recursively delete a directory and all of its contents - e.g.the equivalent of `rm -r` on the command-line. |
|
| 71 | - * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
| 72 | - * |
|
| 73 | - * @param string $source absolute path to directory or file to delete. |
|
| 74 | - * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
| 75 | - * |
|
| 76 | - * @return bool true on success; false on failure |
|
| 77 | - */ |
|
| 78 | - public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
| 79 | - { |
|
| 80 | - if (empty($source) || file_exists($source) === false) { |
|
| 81 | - return false; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - if (is_file($source) || is_link($source)) { |
|
| 85 | - clearstatcache(true, $source); |
|
| 86 | - return unlink($source); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $files = new RecursiveIteratorIterator( |
|
| 90 | - new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS), |
|
| 91 | - RecursiveIteratorIterator::CHILD_FIRST |
|
| 92 | - ); |
|
| 93 | - |
|
| 94 | - foreach ($files as $fileInfo) { |
|
| 95 | - /** |
|
| 96 | - * @var SplFileInfo $fileInfo |
|
| 97 | - */ |
|
| 98 | - $realpath = $fileInfo->getRealPath(); |
|
| 99 | - if ($realpath) { |
|
| 100 | - if ($fileInfo->isDir()) { |
|
| 101 | - if (self::rrmdir($fileInfo->getRealPath()) === false) { |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - } elseif (unlink($realpath) === false) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - } else { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ($removeOnlyChildren === false) { |
|
| 113 | - return rmdir($source); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return true; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Alias of realpath() but work |
|
| 121 | - * on non-existing files |
|
| 122 | - * |
|
| 123 | - * @param string $path |
|
| 124 | - * @return string |
|
| 125 | - */ |
|
| 126 | - public static function getAbsolutePath(string $path): string |
|
| 127 | - { |
|
| 128 | - $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
| 129 | - $absolutes = []; |
|
| 130 | - foreach ($parts as $part) { |
|
| 131 | - if ('.' === $part) { |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - if ('..' === $part) { |
|
| 135 | - array_pop($absolutes); |
|
| 136 | - } else { |
|
| 137 | - $absolutes[] = $part; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Allows dereferencing char |
|
| 143 | - */ |
|
| 144 | - $file = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 145 | - $prefix = $file[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
| 146 | - return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
| 147 | - } |
|
| 26 | + /** |
|
| 27 | + * Get the directory size |
|
| 28 | + * @param string $directory |
|
| 29 | + * @param bool $includeDirAllocSize |
|
| 30 | + * @return int |
|
| 31 | + */ |
|
| 32 | + public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
| 33 | + { |
|
| 34 | + $size = 0; |
|
| 35 | + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
| 36 | + /** |
|
| 37 | + * @var SplFileInfo $file |
|
| 38 | + */ |
|
| 39 | + if ($file->isFile()) { |
|
| 40 | + $size += filesize($file->getRealPath()); |
|
| 41 | + } elseif ($includeDirAllocSize) { |
|
| 42 | + $size += $file->getSize(); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + return $size; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param string $path |
|
| 51 | + * @return int |
|
| 52 | + */ |
|
| 53 | + public static function getFileCount(string $path): int |
|
| 54 | + { |
|
| 55 | + $count = 0; |
|
| 56 | + $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
| 57 | + foreach ($objects as $object) { |
|
| 58 | + /** |
|
| 59 | + * @var SplFileInfo $object |
|
| 60 | + */ |
|
| 61 | + if ($object->isFile()) { |
|
| 62 | + $count++; |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return $count; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Recursively delete a directory and all of its contents - e.g.the equivalent of `rm -r` on the command-line. |
|
| 71 | + * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
| 72 | + * |
|
| 73 | + * @param string $source absolute path to directory or file to delete. |
|
| 74 | + * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
| 75 | + * |
|
| 76 | + * @return bool true on success; false on failure |
|
| 77 | + */ |
|
| 78 | + public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
| 79 | + { |
|
| 80 | + if (empty($source) || file_exists($source) === false) { |
|
| 81 | + return false; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + if (is_file($source) || is_link($source)) { |
|
| 85 | + clearstatcache(true, $source); |
|
| 86 | + return unlink($source); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $files = new RecursiveIteratorIterator( |
|
| 90 | + new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS), |
|
| 91 | + RecursiveIteratorIterator::CHILD_FIRST |
|
| 92 | + ); |
|
| 93 | + |
|
| 94 | + foreach ($files as $fileInfo) { |
|
| 95 | + /** |
|
| 96 | + * @var SplFileInfo $fileInfo |
|
| 97 | + */ |
|
| 98 | + $realpath = $fileInfo->getRealPath(); |
|
| 99 | + if ($realpath) { |
|
| 100 | + if ($fileInfo->isDir()) { |
|
| 101 | + if (self::rrmdir($fileInfo->getRealPath()) === false) { |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + } elseif (unlink($realpath) === false) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + } else { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ($removeOnlyChildren === false) { |
|
| 113 | + return rmdir($source); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return true; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Alias of realpath() but work |
|
| 121 | + * on non-existing files |
|
| 122 | + * |
|
| 123 | + * @param string $path |
|
| 124 | + * @return string |
|
| 125 | + */ |
|
| 126 | + public static function getAbsolutePath(string $path): string |
|
| 127 | + { |
|
| 128 | + $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
| 129 | + $absolutes = []; |
|
| 130 | + foreach ($parts as $part) { |
|
| 131 | + if ('.' === $part) { |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + if ('..' === $part) { |
|
| 135 | + array_pop($absolutes); |
|
| 136 | + } else { |
|
| 137 | + $absolutes[] = $part; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Allows dereferencing char |
|
| 143 | + */ |
|
| 144 | + $file = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 145 | + $prefix = $file[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
| 146 | + return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
| 147 | + } |
|
| 148 | 148 | } |
@@ -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 | } |