@@ -20,7 +20,7 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Wiki |
| 22 | 22 | { |
| 23 | - use UninstanciableObjectTrait; |
|
| 23 | + use UninstanciableObjectTrait; |
|
| 24 | 24 | |
| 25 | - public const FETCH_ALL_KEY_URL = 'https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV5%CB%96%5D-Fetching-all-keys'; |
|
| 25 | + public const FETCH_ALL_KEY_URL = 'https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV5%CB%96%5D-Fetching-all-keys'; |
|
| 26 | 26 | } |
@@ -21,10 +21,10 @@ |
||
| 21 | 21 | |
| 22 | 22 | class Item implements ExtendedCacheItemInterface |
| 23 | 23 | { |
| 24 | - use TaggableCacheItemTrait; |
|
| 24 | + use TaggableCacheItemTrait; |
|
| 25 | 25 | |
| 26 | - protected function getDriverClass(): string |
|
| 27 | - { |
|
| 28 | - return Driver::class; |
|
| 29 | - } |
|
| 26 | + protected function getDriverClass(): string |
|
| 27 | + { |
|
| 28 | + return Driver::class; |
|
| 29 | + } |
|
| 30 | 30 | } |
@@ -30,85 +30,85 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class Driver implements ExtendedCacheItemPoolInterface |
| 32 | 32 | { |
| 33 | - use TaggableCacheItemPoolTrait; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public function driverCheck(): bool |
|
| 39 | - { |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @return DriverStatistic |
|
| 45 | - */ |
|
| 46 | - public function getStats(): DriverStatistic |
|
| 47 | - { |
|
| 48 | - $stat = new DriverStatistic(); |
|
| 49 | - $stat->setInfo('[Devrandom] A void info string') |
|
| 50 | - ->setSize(0) |
|
| 51 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 52 | - ->setRawData(false); |
|
| 53 | - |
|
| 54 | - return $stat; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param ExtendedCacheItemInterface $item |
|
| 59 | - * @return bool |
|
| 60 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 61 | - */ |
|
| 62 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 63 | - { |
|
| 64 | - |
|
| 65 | - return true; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param ExtendedCacheItemInterface $item |
|
| 70 | - * @return ?array<string, mixed> |
|
| 71 | - * @throws \Exception |
|
| 72 | - */ |
|
| 73 | - protected function driverRead(CacheItemInterface $item): ?array |
|
| 74 | - { |
|
| 75 | - $chanceOfRetrieval = $this->getConfig()->getChanceOfRetrieval(); |
|
| 76 | - $ttl = $this->getConfig()->getDefaultTtl(); |
|
| 77 | - |
|
| 78 | - if (\random_int(0, 100) < $chanceOfRetrieval) { |
|
| 79 | - return [ |
|
| 80 | - self::DRIVER_DATA_WRAPPER_INDEX => \bin2hex(\random_bytes($this->getConfig()->getDataLength())), |
|
| 81 | - self::DRIVER_TAGS_WRAPPER_INDEX => [], |
|
| 82 | - self::DRIVER_EDATE_WRAPPER_INDEX => (new DateTime())->add(new DateInterval("PT{$ttl}S")), |
|
| 83 | - ]; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param string $key |
|
| 91 | - * @param string $encodedKey |
|
| 92 | - * @return bool |
|
| 93 | - */ |
|
| 94 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
| 95 | - { |
|
| 96 | - return true; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @return bool |
|
| 101 | - */ |
|
| 102 | - protected function driverClear(): bool |
|
| 103 | - { |
|
| 104 | - return true; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @return bool |
|
| 109 | - */ |
|
| 110 | - protected function driverConnect(): bool |
|
| 111 | - { |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 33 | + use TaggableCacheItemPoolTrait; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public function driverCheck(): bool |
|
| 39 | + { |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @return DriverStatistic |
|
| 45 | + */ |
|
| 46 | + public function getStats(): DriverStatistic |
|
| 47 | + { |
|
| 48 | + $stat = new DriverStatistic(); |
|
| 49 | + $stat->setInfo('[Devrandom] A void info string') |
|
| 50 | + ->setSize(0) |
|
| 51 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 52 | + ->setRawData(false); |
|
| 53 | + |
|
| 54 | + return $stat; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param ExtendedCacheItemInterface $item |
|
| 59 | + * @return bool |
|
| 60 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 61 | + */ |
|
| 62 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 63 | + { |
|
| 64 | + |
|
| 65 | + return true; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param ExtendedCacheItemInterface $item |
|
| 70 | + * @return ?array<string, mixed> |
|
| 71 | + * @throws \Exception |
|
| 72 | + */ |
|
| 73 | + protected function driverRead(CacheItemInterface $item): ?array |
|
| 74 | + { |
|
| 75 | + $chanceOfRetrieval = $this->getConfig()->getChanceOfRetrieval(); |
|
| 76 | + $ttl = $this->getConfig()->getDefaultTtl(); |
|
| 77 | + |
|
| 78 | + if (\random_int(0, 100) < $chanceOfRetrieval) { |
|
| 79 | + return [ |
|
| 80 | + self::DRIVER_DATA_WRAPPER_INDEX => \bin2hex(\random_bytes($this->getConfig()->getDataLength())), |
|
| 81 | + self::DRIVER_TAGS_WRAPPER_INDEX => [], |
|
| 82 | + self::DRIVER_EDATE_WRAPPER_INDEX => (new DateTime())->add(new DateInterval("PT{$ttl}S")), |
|
| 83 | + ]; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param string $key |
|
| 91 | + * @param string $encodedKey |
|
| 92 | + * @return bool |
|
| 93 | + */ |
|
| 94 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
| 95 | + { |
|
| 96 | + return true; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @return bool |
|
| 101 | + */ |
|
| 102 | + protected function driverClear(): bool |
|
| 103 | + { |
|
| 104 | + return true; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @return bool |
|
| 109 | + */ |
|
| 110 | + protected function driverConnect(): bool |
|
| 111 | + { |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | 114 | } |
@@ -22,45 +22,45 @@ |
||
| 22 | 22 | |
| 23 | 23 | class Config extends ConfigurationOption |
| 24 | 24 | { |
| 25 | - protected int $dataLength = 16; |
|
| 26 | - protected int $chanceOfRetrieval = 50; |
|
| 25 | + protected int $dataLength = 16; |
|
| 26 | + protected int $chanceOfRetrieval = 50; |
|
| 27 | 27 | /** |
| 28 | - * @return int |
|
| 29 | - */ |
|
| 30 | - public function getDataLength(): int |
|
| 31 | - { |
|
| 32 | - return $this->dataLength; |
|
| 33 | - } |
|
| 28 | + * @return int |
|
| 29 | + */ |
|
| 30 | + public function getDataLength(): int |
|
| 31 | + { |
|
| 32 | + return $this->dataLength; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param int $dataLength |
|
| 37 | - * @return Config |
|
| 38 | - * @throws PhpfastcacheLogicException |
|
| 39 | - */ |
|
| 40 | - public function setDataLength(int $dataLength): Config |
|
| 41 | - { |
|
| 42 | - return $this->setProperty('dataLength', $dataLength); |
|
| 43 | - } |
|
| 35 | + /** |
|
| 36 | + * @param int $dataLength |
|
| 37 | + * @return Config |
|
| 38 | + * @throws PhpfastcacheLogicException |
|
| 39 | + */ |
|
| 40 | + public function setDataLength(int $dataLength): Config |
|
| 41 | + { |
|
| 42 | + return $this->setProperty('dataLength', $dataLength); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return int |
|
| 47 | - */ |
|
| 48 | - public function getChanceOfRetrieval(): int |
|
| 49 | - { |
|
| 50 | - return $this->chanceOfRetrieval; |
|
| 51 | - } |
|
| 45 | + /** |
|
| 46 | + * @return int |
|
| 47 | + */ |
|
| 48 | + public function getChanceOfRetrieval(): int |
|
| 49 | + { |
|
| 50 | + return $this->chanceOfRetrieval; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @param int $chanceOfRetrieval |
|
| 55 | - * @return Config |
|
| 56 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 57 | - * @throws PhpfastcacheLogicException |
|
| 58 | - */ |
|
| 59 | - public function setChanceOfRetrieval(int $chanceOfRetrieval): Config |
|
| 60 | - { |
|
| 61 | - if ($chanceOfRetrieval < 0 || $chanceOfRetrieval > 100) { |
|
| 62 | - throw new PhpfastcacheInvalidArgumentException('Chance of retrieval must be between 0 and 100'); |
|
| 63 | - } |
|
| 64 | - return $this->setProperty('chanceOfRetrieval', $chanceOfRetrieval); |
|
| 65 | - } |
|
| 53 | + /** |
|
| 54 | + * @param int $chanceOfRetrieval |
|
| 55 | + * @return Config |
|
| 56 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 57 | + * @throws PhpfastcacheLogicException |
|
| 58 | + */ |
|
| 59 | + public function setChanceOfRetrieval(int $chanceOfRetrieval): Config |
|
| 60 | + { |
|
| 61 | + if ($chanceOfRetrieval < 0 || $chanceOfRetrieval > 100) { |
|
| 62 | + throw new PhpfastcacheInvalidArgumentException('Chance of retrieval must be between 0 and 100'); |
|
| 63 | + } |
|
| 64 | + return $this->setProperty('chanceOfRetrieval', $chanceOfRetrieval); |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -35,307 +35,307 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class Driver implements AggregatablePoolInterface |
| 37 | 37 | { |
| 38 | - use IOHelperTrait; |
|
| 39 | - |
|
| 40 | - /** @var array<PDO> */ |
|
| 41 | - protected array $dbInstances = []; |
|
| 42 | - protected const INDEXING_FILE = 'indexing'; |
|
| 43 | - |
|
| 44 | - protected int $maxSize = 10; |
|
| 45 | - |
|
| 46 | - protected int $currentDB = 1; |
|
| 47 | - |
|
| 48 | - protected string $sqliteDir = ''; |
|
| 49 | - |
|
| 50 | - protected ?PDO $indexing; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return bool |
|
| 54 | - * @throws PhpfastcacheCoreException |
|
| 55 | - */ |
|
| 56 | - public function driverCheck(): bool |
|
| 57 | - { |
|
| 58 | - return extension_loaded('pdo_sqlite') && (is_writable($this->getSqliteDir()) || @mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @return string |
|
| 63 | - * @throws PhpfastcacheCoreException |
|
| 64 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 65 | - */ |
|
| 66 | - public function getSqliteDir(): string |
|
| 67 | - { |
|
| 68 | - return $this->sqliteDir ?: $this->getPath(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - public function __sleep(): array |
|
| 75 | - { |
|
| 76 | - return array_diff(array_keys(get_object_vars($this)), ['indexing', 'instance']); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @return bool |
|
| 81 | - * @throws PhpfastcacheIOException |
|
| 82 | - */ |
|
| 83 | - protected function driverConnect(): bool |
|
| 84 | - { |
|
| 85 | - if (!file_exists($this->getSqliteDir()) && !@mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)) { |
|
| 86 | - throw new PhpfastcacheIOException(sprintf('Sqlite cannot write in "%s", aborting...', $this->getPath())); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $this->sqliteDir = $this->getPath(); |
|
| 90 | - |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param ExtendedCacheItemInterface $item |
|
| 96 | - * @return ?array<string, mixed> |
|
| 97 | - */ |
|
| 98 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 99 | - { |
|
| 100 | - try { |
|
| 101 | - $stm = $this->getDb($item->getEncodedKey()) |
|
| 102 | - ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 103 | - $stm->execute( |
|
| 104 | - [ |
|
| 105 | - ':keyword' => $item->getEncodedKey(), |
|
| 106 | - ] |
|
| 107 | - ); |
|
| 108 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 109 | - } catch (PDOException $e) { |
|
| 110 | - try { |
|
| 111 | - $stm = $this->getDb($item->getEncodedKey(), true) |
|
| 112 | - ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 113 | - $stm->execute( |
|
| 114 | - [ |
|
| 115 | - ':keyword' => $item->getEncodedKey(), |
|
| 116 | - ] |
|
| 117 | - ); |
|
| 118 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 119 | - } catch (PDOException $e) { |
|
| 120 | - return null; |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - if (isset($row['object'])) { |
|
| 125 | - return $this->decode($row['object']); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - return null; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param string $keyword |
|
| 133 | - * @param bool $reset |
|
| 134 | - * @return PDO |
|
| 135 | - */ |
|
| 136 | - public function getDb(string $keyword, bool $reset = false): PDO |
|
| 137 | - { |
|
| 138 | - /** |
|
| 139 | - * Default is phpfastcache |
|
| 140 | - */ |
|
| 141 | - $instant = $this->getDbIndex($keyword); |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * init instant |
|
| 145 | - */ |
|
| 146 | - if (!isset($this->dbInstances[$instant])) { |
|
| 147 | - // check DB Files ready or not |
|
| 148 | - $tableCreated = false; |
|
| 149 | - if ($reset || !file_exists($this->sqliteDir . '/db' . $instant)) { |
|
| 150 | - $tableCreated = true; |
|
| 151 | - } |
|
| 152 | - $pdo = new PDO('sqlite:' . $this->sqliteDir . '/db' . $instant); |
|
| 153 | - $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 154 | - |
|
| 155 | - if ($tableCreated) { |
|
| 156 | - $this->initDB($pdo); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $this->dbInstances[$instant] = $pdo; |
|
| 160 | - unset($pdo); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $this->dbInstances[$instant]; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Return Database of Keyword |
|
| 168 | - * @param string $keyword |
|
| 169 | - * @return int |
|
| 170 | - */ |
|
| 171 | - public function getDbIndex(string $keyword) |
|
| 172 | - { |
|
| 173 | - if (!isset($this->indexing)) { |
|
| 174 | - $tableCreated = false; |
|
| 175 | - if (!file_exists($this->sqliteDir . '/indexing')) { |
|
| 176 | - $tableCreated = true; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $pdo = new PDO("sqlite:" . $this->sqliteDir . '/' . self::INDEXING_FILE); |
|
| 180 | - $pdo->setAttribute( |
|
| 181 | - PDO::ATTR_ERRMODE, |
|
| 182 | - PDO::ERRMODE_EXCEPTION |
|
| 183 | - ); |
|
| 184 | - |
|
| 185 | - if ($tableCreated) { |
|
| 186 | - $this->initIndexing($pdo); |
|
| 187 | - } |
|
| 188 | - $this->indexing = $pdo; |
|
| 189 | - unset($pdo); |
|
| 190 | - |
|
| 191 | - $stm = $this->indexing->prepare("SELECT MAX(`db`) as `db` FROM `balancing`"); |
|
| 192 | - $stm->execute(); |
|
| 193 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 194 | - if (!isset($row['db'])) { |
|
| 195 | - $db = 1; |
|
| 196 | - } elseif ($row['db'] <= 1) { |
|
| 197 | - $db = 1; |
|
| 198 | - } else { |
|
| 199 | - $db = $row['db']; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // check file size |
|
| 203 | - |
|
| 204 | - $size = file_exists($this->sqliteDir . '/db' . $db) ? filesize($this->sqliteDir . '/db' . $db) : 1; |
|
| 205 | - $size = round($size / 1024 / 1024, 1); |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - if ($size > $this->maxSize) { |
|
| 209 | - $db++; |
|
| 210 | - } |
|
| 211 | - $this->currentDB = $db; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - // look for keyword |
|
| 215 | - $stm = $this->indexing->prepare("SELECT * FROM `balancing` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 216 | - $stm->execute( |
|
| 217 | - [ |
|
| 218 | - ':keyword' => $keyword, |
|
| 219 | - ] |
|
| 220 | - ); |
|
| 221 | - $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 222 | - if (isset($row['db']) && $row['db'] != '') { |
|
| 223 | - $db = $row['db']; |
|
| 224 | - } else { |
|
| 225 | - /* |
|
| 38 | + use IOHelperTrait; |
|
| 39 | + |
|
| 40 | + /** @var array<PDO> */ |
|
| 41 | + protected array $dbInstances = []; |
|
| 42 | + protected const INDEXING_FILE = 'indexing'; |
|
| 43 | + |
|
| 44 | + protected int $maxSize = 10; |
|
| 45 | + |
|
| 46 | + protected int $currentDB = 1; |
|
| 47 | + |
|
| 48 | + protected string $sqliteDir = ''; |
|
| 49 | + |
|
| 50 | + protected ?PDO $indexing; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return bool |
|
| 54 | + * @throws PhpfastcacheCoreException |
|
| 55 | + */ |
|
| 56 | + public function driverCheck(): bool |
|
| 57 | + { |
|
| 58 | + return extension_loaded('pdo_sqlite') && (is_writable($this->getSqliteDir()) || @mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @return string |
|
| 63 | + * @throws PhpfastcacheCoreException |
|
| 64 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 65 | + */ |
|
| 66 | + public function getSqliteDir(): string |
|
| 67 | + { |
|
| 68 | + return $this->sqliteDir ?: $this->getPath(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + public function __sleep(): array |
|
| 75 | + { |
|
| 76 | + return array_diff(array_keys(get_object_vars($this)), ['indexing', 'instance']); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @return bool |
|
| 81 | + * @throws PhpfastcacheIOException |
|
| 82 | + */ |
|
| 83 | + protected function driverConnect(): bool |
|
| 84 | + { |
|
| 85 | + if (!file_exists($this->getSqliteDir()) && !@mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)) { |
|
| 86 | + throw new PhpfastcacheIOException(sprintf('Sqlite cannot write in "%s", aborting...', $this->getPath())); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $this->sqliteDir = $this->getPath(); |
|
| 90 | + |
|
| 91 | + return true; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param ExtendedCacheItemInterface $item |
|
| 96 | + * @return ?array<string, mixed> |
|
| 97 | + */ |
|
| 98 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 99 | + { |
|
| 100 | + try { |
|
| 101 | + $stm = $this->getDb($item->getEncodedKey()) |
|
| 102 | + ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 103 | + $stm->execute( |
|
| 104 | + [ |
|
| 105 | + ':keyword' => $item->getEncodedKey(), |
|
| 106 | + ] |
|
| 107 | + ); |
|
| 108 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 109 | + } catch (PDOException $e) { |
|
| 110 | + try { |
|
| 111 | + $stm = $this->getDb($item->getEncodedKey(), true) |
|
| 112 | + ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 113 | + $stm->execute( |
|
| 114 | + [ |
|
| 115 | + ':keyword' => $item->getEncodedKey(), |
|
| 116 | + ] |
|
| 117 | + ); |
|
| 118 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 119 | + } catch (PDOException $e) { |
|
| 120 | + return null; |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + if (isset($row['object'])) { |
|
| 125 | + return $this->decode($row['object']); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + return null; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param string $keyword |
|
| 133 | + * @param bool $reset |
|
| 134 | + * @return PDO |
|
| 135 | + */ |
|
| 136 | + public function getDb(string $keyword, bool $reset = false): PDO |
|
| 137 | + { |
|
| 138 | + /** |
|
| 139 | + * Default is phpfastcache |
|
| 140 | + */ |
|
| 141 | + $instant = $this->getDbIndex($keyword); |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * init instant |
|
| 145 | + */ |
|
| 146 | + if (!isset($this->dbInstances[$instant])) { |
|
| 147 | + // check DB Files ready or not |
|
| 148 | + $tableCreated = false; |
|
| 149 | + if ($reset || !file_exists($this->sqliteDir . '/db' . $instant)) { |
|
| 150 | + $tableCreated = true; |
|
| 151 | + } |
|
| 152 | + $pdo = new PDO('sqlite:' . $this->sqliteDir . '/db' . $instant); |
|
| 153 | + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 154 | + |
|
| 155 | + if ($tableCreated) { |
|
| 156 | + $this->initDB($pdo); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $this->dbInstances[$instant] = $pdo; |
|
| 160 | + unset($pdo); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $this->dbInstances[$instant]; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Return Database of Keyword |
|
| 168 | + * @param string $keyword |
|
| 169 | + * @return int |
|
| 170 | + */ |
|
| 171 | + public function getDbIndex(string $keyword) |
|
| 172 | + { |
|
| 173 | + if (!isset($this->indexing)) { |
|
| 174 | + $tableCreated = false; |
|
| 175 | + if (!file_exists($this->sqliteDir . '/indexing')) { |
|
| 176 | + $tableCreated = true; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $pdo = new PDO("sqlite:" . $this->sqliteDir . '/' . self::INDEXING_FILE); |
|
| 180 | + $pdo->setAttribute( |
|
| 181 | + PDO::ATTR_ERRMODE, |
|
| 182 | + PDO::ERRMODE_EXCEPTION |
|
| 183 | + ); |
|
| 184 | + |
|
| 185 | + if ($tableCreated) { |
|
| 186 | + $this->initIndexing($pdo); |
|
| 187 | + } |
|
| 188 | + $this->indexing = $pdo; |
|
| 189 | + unset($pdo); |
|
| 190 | + |
|
| 191 | + $stm = $this->indexing->prepare("SELECT MAX(`db`) as `db` FROM `balancing`"); |
|
| 192 | + $stm->execute(); |
|
| 193 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 194 | + if (!isset($row['db'])) { |
|
| 195 | + $db = 1; |
|
| 196 | + } elseif ($row['db'] <= 1) { |
|
| 197 | + $db = 1; |
|
| 198 | + } else { |
|
| 199 | + $db = $row['db']; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // check file size |
|
| 203 | + |
|
| 204 | + $size = file_exists($this->sqliteDir . '/db' . $db) ? filesize($this->sqliteDir . '/db' . $db) : 1; |
|
| 205 | + $size = round($size / 1024 / 1024, 1); |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + if ($size > $this->maxSize) { |
|
| 209 | + $db++; |
|
| 210 | + } |
|
| 211 | + $this->currentDB = $db; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + // look for keyword |
|
| 215 | + $stm = $this->indexing->prepare("SELECT * FROM `balancing` WHERE `keyword`=:keyword LIMIT 1"); |
|
| 216 | + $stm->execute( |
|
| 217 | + [ |
|
| 218 | + ':keyword' => $keyword, |
|
| 219 | + ] |
|
| 220 | + ); |
|
| 221 | + $row = $stm->fetch(PDO::FETCH_ASSOC); |
|
| 222 | + if (isset($row['db']) && $row['db'] != '') { |
|
| 223 | + $db = $row['db']; |
|
| 224 | + } else { |
|
| 225 | + /* |
|
| 226 | 226 | * Insert new to Indexing |
| 227 | 227 | */ |
| 228 | - $db = $this->currentDB; |
|
| 229 | - $stm = $this->indexing->prepare("INSERT INTO `balancing` (`keyword`,`db`) VALUES(:keyword, :db)"); |
|
| 230 | - $stm->execute( |
|
| 231 | - [ |
|
| 232 | - ':keyword' => $keyword, |
|
| 233 | - ':db' => $db, |
|
| 234 | - ] |
|
| 235 | - ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return $db; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * INIT Indexing DB |
|
| 243 | - * @param PDO $db |
|
| 244 | - */ |
|
| 245 | - public function initIndexing(PDO $db): void |
|
| 246 | - { |
|
| 247 | - // delete everything before reset indexing |
|
| 248 | - $dir = opendir($this->sqliteDir); |
|
| 249 | - while ($file = readdir($dir)) { |
|
| 250 | - if ($file !== '.' && $file !== '..' && $file !== 'indexing' && $file !== 'dbfastcache') { |
|
| 251 | - unlink($this->sqliteDir . '/' . $file); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $db->exec('DROP TABLE if exists "balancing"'); |
|
| 256 | - $db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)'); |
|
| 257 | - $db->exec('CREATE INDEX "db" ON "balancing" ("db")'); |
|
| 258 | - $db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")'); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * INIT NEW DB |
|
| 263 | - * @param PDO $db |
|
| 264 | - */ |
|
| 265 | - protected function initDB(PDO $db): void |
|
| 266 | - { |
|
| 267 | - $db->exec('drop table if exists "caching"'); |
|
| 268 | - $db->exec('CREATE TABLE "caching" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "keyword" VARCHAR UNIQUE, "object" BLOB, "exp" INTEGER)'); |
|
| 269 | - $db->exec('CREATE UNIQUE INDEX "cleanup" ON "caching" ("keyword","exp")'); |
|
| 270 | - $db->exec('CREATE INDEX "exp" ON "caching" ("exp")'); |
|
| 271 | - $db->exec('CREATE UNIQUE INDEX "keyword" ON "caching" ("keyword")'); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @param ExtendedCacheItemInterface $item |
|
| 276 | - * @return mixed |
|
| 277 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 278 | - * @throws PhpfastcacheLogicException |
|
| 279 | - */ |
|
| 280 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 281 | - { |
|
| 282 | - |
|
| 283 | - try { |
|
| 284 | - $stm = $this->getDb($item->getEncodedKey()) |
|
| 285 | - ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
|
| 286 | - $stm->execute( |
|
| 287 | - [ |
|
| 288 | - ':keyword' => $item->getEncodedKey(), |
|
| 289 | - ':object' => $this->encode($this->driverPreWrap($item)), |
|
| 290 | - ':exp' => $item->getExpirationDate()->getTimestamp(), |
|
| 291 | - ] |
|
| 292 | - ); |
|
| 293 | - |
|
| 294 | - return true; |
|
| 295 | - } catch (PDOException $e) { |
|
| 296 | - return false; |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * @param string $key |
|
| 302 | - * @param string $encodedKey |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
| 306 | - { |
|
| 307 | - try { |
|
| 308 | - $stm = $this->getDb($encodedKey) |
|
| 309 | - ->prepare("DELETE FROM `caching` WHERE (`exp` <= :exp) OR (`keyword`=:keyword) "); |
|
| 310 | - |
|
| 311 | - return $stm->execute( |
|
| 312 | - [ |
|
| 313 | - ':keyword' => $encodedKey, |
|
| 314 | - ':exp' => time(), |
|
| 315 | - ] |
|
| 316 | - ); |
|
| 317 | - } catch (PDOException $e) { |
|
| 318 | - return false; |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * @return bool |
|
| 324 | - * @throws PhpfastcacheCoreException |
|
| 325 | - */ |
|
| 326 | - protected function driverClear(): bool |
|
| 327 | - { |
|
| 328 | - $this->dbInstances = []; |
|
| 329 | - $this->indexing = null; |
|
| 330 | - |
|
| 331 | - // delete everything before reset indexing |
|
| 332 | - $dir = opendir($this->getSqliteDir()); |
|
| 333 | - while ($file = readdir($dir)) { |
|
| 334 | - if ($file !== '.' && $file !== '..') { |
|
| 335 | - unlink($this->getSqliteDir() . '/' . $file); |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - return true; |
|
| 340 | - } |
|
| 228 | + $db = $this->currentDB; |
|
| 229 | + $stm = $this->indexing->prepare("INSERT INTO `balancing` (`keyword`,`db`) VALUES(:keyword, :db)"); |
|
| 230 | + $stm->execute( |
|
| 231 | + [ |
|
| 232 | + ':keyword' => $keyword, |
|
| 233 | + ':db' => $db, |
|
| 234 | + ] |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return $db; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * INIT Indexing DB |
|
| 243 | + * @param PDO $db |
|
| 244 | + */ |
|
| 245 | + public function initIndexing(PDO $db): void |
|
| 246 | + { |
|
| 247 | + // delete everything before reset indexing |
|
| 248 | + $dir = opendir($this->sqliteDir); |
|
| 249 | + while ($file = readdir($dir)) { |
|
| 250 | + if ($file !== '.' && $file !== '..' && $file !== 'indexing' && $file !== 'dbfastcache') { |
|
| 251 | + unlink($this->sqliteDir . '/' . $file); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $db->exec('DROP TABLE if exists "balancing"'); |
|
| 256 | + $db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)'); |
|
| 257 | + $db->exec('CREATE INDEX "db" ON "balancing" ("db")'); |
|
| 258 | + $db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")'); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * INIT NEW DB |
|
| 263 | + * @param PDO $db |
|
| 264 | + */ |
|
| 265 | + protected function initDB(PDO $db): void |
|
| 266 | + { |
|
| 267 | + $db->exec('drop table if exists "caching"'); |
|
| 268 | + $db->exec('CREATE TABLE "caching" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "keyword" VARCHAR UNIQUE, "object" BLOB, "exp" INTEGER)'); |
|
| 269 | + $db->exec('CREATE UNIQUE INDEX "cleanup" ON "caching" ("keyword","exp")'); |
|
| 270 | + $db->exec('CREATE INDEX "exp" ON "caching" ("exp")'); |
|
| 271 | + $db->exec('CREATE UNIQUE INDEX "keyword" ON "caching" ("keyword")'); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @param ExtendedCacheItemInterface $item |
|
| 276 | + * @return mixed |
|
| 277 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 278 | + * @throws PhpfastcacheLogicException |
|
| 279 | + */ |
|
| 280 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 281 | + { |
|
| 282 | + |
|
| 283 | + try { |
|
| 284 | + $stm = $this->getDb($item->getEncodedKey()) |
|
| 285 | + ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
|
| 286 | + $stm->execute( |
|
| 287 | + [ |
|
| 288 | + ':keyword' => $item->getEncodedKey(), |
|
| 289 | + ':object' => $this->encode($this->driverPreWrap($item)), |
|
| 290 | + ':exp' => $item->getExpirationDate()->getTimestamp(), |
|
| 291 | + ] |
|
| 292 | + ); |
|
| 293 | + |
|
| 294 | + return true; |
|
| 295 | + } catch (PDOException $e) { |
|
| 296 | + return false; |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * @param string $key |
|
| 302 | + * @param string $encodedKey |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
| 306 | + { |
|
| 307 | + try { |
|
| 308 | + $stm = $this->getDb($encodedKey) |
|
| 309 | + ->prepare("DELETE FROM `caching` WHERE (`exp` <= :exp) OR (`keyword`=:keyword) "); |
|
| 310 | + |
|
| 311 | + return $stm->execute( |
|
| 312 | + [ |
|
| 313 | + ':keyword' => $encodedKey, |
|
| 314 | + ':exp' => time(), |
|
| 315 | + ] |
|
| 316 | + ); |
|
| 317 | + } catch (PDOException $e) { |
|
| 318 | + return false; |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @return bool |
|
| 324 | + * @throws PhpfastcacheCoreException |
|
| 325 | + */ |
|
| 326 | + protected function driverClear(): bool |
|
| 327 | + { |
|
| 328 | + $this->dbInstances = []; |
|
| 329 | + $this->indexing = null; |
|
| 330 | + |
|
| 331 | + // delete everything before reset indexing |
|
| 332 | + $dir = opendir($this->getSqliteDir()); |
|
| 333 | + while ($file = readdir($dir)) { |
|
| 334 | + if ($file !== '.' && $file !== '..') { |
|
| 335 | + unlink($this->getSqliteDir() . '/' . $file); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + return true; |
|
| 340 | + } |
|
| 341 | 341 | } |
@@ -106,7 +106,8 @@ discard block |
||
| 106 | 106 | ] |
| 107 | 107 | ); |
| 108 | 108 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
| 109 | - } catch (PDOException $e) { |
|
| 109 | + } |
|
| 110 | + catch (PDOException $e) { |
|
| 110 | 111 | try { |
| 111 | 112 | $stm = $this->getDb($item->getEncodedKey(), true) |
| 112 | 113 | ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
@@ -116,7 +117,8 @@ discard block |
||
| 116 | 117 | ] |
| 117 | 118 | ); |
| 118 | 119 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
| 119 | - } catch (PDOException $e) { |
|
| 120 | + } |
|
| 121 | + catch (PDOException $e) { |
|
| 120 | 122 | return null; |
| 121 | 123 | } |
| 122 | 124 | } |
@@ -168,8 +170,7 @@ discard block |
||
| 168 | 170 | * @param string $keyword |
| 169 | 171 | * @return int |
| 170 | 172 | */ |
| 171 | - public function getDbIndex(string $keyword) |
|
| 172 | - { |
|
| 173 | + public function getDbIndex(string $keyword) { |
|
| 173 | 174 | if (!isset($this->indexing)) { |
| 174 | 175 | $tableCreated = false; |
| 175 | 176 | if (!file_exists($this->sqliteDir . '/indexing')) { |
@@ -193,9 +194,11 @@ discard block |
||
| 193 | 194 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
| 194 | 195 | if (!isset($row['db'])) { |
| 195 | 196 | $db = 1; |
| 196 | - } elseif ($row['db'] <= 1) { |
|
| 197 | + } |
|
| 198 | + elseif ($row['db'] <= 1) { |
|
| 197 | 199 | $db = 1; |
| 198 | - } else { |
|
| 200 | + } |
|
| 201 | + else { |
|
| 199 | 202 | $db = $row['db']; |
| 200 | 203 | } |
| 201 | 204 | |
@@ -221,7 +224,8 @@ discard block |
||
| 221 | 224 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
| 222 | 225 | if (isset($row['db']) && $row['db'] != '') { |
| 223 | 226 | $db = $row['db']; |
| 224 | - } else { |
|
| 227 | + } |
|
| 228 | + else { |
|
| 225 | 229 | /* |
| 226 | 230 | * Insert new to Indexing |
| 227 | 231 | */ |
@@ -292,7 +296,8 @@ discard block |
||
| 292 | 296 | ); |
| 293 | 297 | |
| 294 | 298 | return true; |
| 295 | - } catch (PDOException $e) { |
|
| 299 | + } |
|
| 300 | + catch (PDOException $e) { |
|
| 296 | 301 | return false; |
| 297 | 302 | } |
| 298 | 303 | } |
@@ -314,7 +319,8 @@ discard block |
||
| 314 | 319 | ':exp' => time(), |
| 315 | 320 | ] |
| 316 | 321 | ); |
| 317 | - } catch (PDOException $e) { |
|
| 322 | + } |
|
| 323 | + catch (PDOException $e) { |
|
| 318 | 324 | return false; |
| 319 | 325 | } |
| 320 | 326 | } |
@@ -41,155 +41,155 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | class Driver implements AggregatablePoolInterface |
| 43 | 43 | { |
| 44 | - use TaggableCacheItemPoolTrait { |
|
| 45 | - __construct as protected __parentConstruct; |
|
| 46 | - } |
|
| 47 | - use MemcacheDriverCollisionDetectorTrait; |
|
| 48 | - |
|
| 49 | - protected int $memcacheFlags = 0; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Driver constructor. |
|
| 53 | - * @param ConfigurationOption $config |
|
| 54 | - * @param string $instanceId |
|
| 55 | - * @param EventManagerInterface $em |
|
| 56 | - * @throws PhpfastcacheDriverConnectException |
|
| 57 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 58 | - * @throws PhpfastcacheCoreException |
|
| 59 | - * @throws PhpfastcacheDriverCheckException |
|
| 60 | - * @throws PhpfastcacheIOException |
|
| 61 | - */ |
|
| 62 | - public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) |
|
| 63 | - { |
|
| 64 | - self::checkCollision('Memcache'); |
|
| 65 | - $this->__parentConstruct($config, $instanceId, $em); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return bool |
|
| 70 | - */ |
|
| 71 | - public function driverCheck(): bool |
|
| 72 | - { |
|
| 73 | - return class_exists('Memcache'); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @return DriverStatistic |
|
| 78 | - */ |
|
| 79 | - public function getStats(): DriverStatistic |
|
| 80 | - { |
|
| 81 | - $stats = (array)$this->instance->getstats(); |
|
| 82 | - $stats['uptime'] = (isset($stats['uptime']) ? $stats['uptime'] : 0); |
|
| 83 | - $stats['version'] = (isset($stats['version']) ? $stats['version'] : 'UnknownVersion'); |
|
| 84 | - $stats['bytes'] = (isset($stats['bytes']) ? $stats['version'] : 0); |
|
| 85 | - |
|
| 86 | - $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
| 87 | - |
|
| 88 | - return (new DriverStatistic()) |
|
| 89 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 90 | - ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
|
| 91 | - ->setRawData($stats) |
|
| 92 | - ->setSize((int)$stats['bytes']); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @return bool |
|
| 97 | - * @throws PhpfastcacheDriverException |
|
| 98 | - */ |
|
| 99 | - protected function driverConnect(): bool |
|
| 100 | - { |
|
| 101 | - $this->instance = new MemcacheSoftware(); |
|
| 102 | - |
|
| 103 | - foreach ($this->getConfig()->getServers() as $server) { |
|
| 104 | - try { |
|
| 105 | - /** |
|
| 106 | - * If path is provided we consider it as a UNIX Socket |
|
| 107 | - */ |
|
| 108 | - if (!empty($server['path'])) { |
|
| 109 | - $this->instance->addServer($server['path'], 0); |
|
| 110 | - } elseif (!empty($server['host'])) { |
|
| 111 | - $this->instance->addServer($server['host'], $server['port']); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
|
| 115 | - throw new PhpfastcacheDriverException('Unlike Memcached, Memcache does not support SASL authentication'); |
|
| 116 | - } |
|
| 117 | - } catch (Exception $e) { |
|
| 118 | - throw new PhpfastcacheDriverConnectException( |
|
| 119 | - sprintf( |
|
| 120 | - 'Failed to connect to memcache host/path "%s" with the following error: %s', |
|
| 121 | - $server['host'] ?: $server['path'], |
|
| 122 | - $e->getMessage() |
|
| 123 | - ) |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Since Memcached does not throw |
|
| 129 | - * any error if not connected ... |
|
| 130 | - */ |
|
| 131 | - if ( |
|
| 132 | - !$this->instance->getServerStatus( |
|
| 133 | - !empty($server['path']) ? $server['path'] : $server['host'], |
|
| 134 | - !empty($server['port']) ? $server['port'] : 0 |
|
| 135 | - ) |
|
| 136 | - ) { |
|
| 137 | - throw new PhpfastcacheDriverException('Memcache seems to not be connected'); |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param ExtendedCacheItemInterface $item |
|
| 146 | - * @return ?array<string, mixed> |
|
| 147 | - */ |
|
| 148 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 149 | - { |
|
| 150 | - $val = $this->instance->get($item->getKey()); |
|
| 151 | - |
|
| 152 | - if (empty($val) || !\is_array($val)) { |
|
| 153 | - return null; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return $val; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param ExtendedCacheItemInterface $item |
|
| 161 | - * @return mixed |
|
| 162 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 163 | - * @throws PhpfastcacheLogicException |
|
| 164 | - */ |
|
| 165 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 166 | - { |
|
| 167 | - |
|
| 168 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 169 | - |
|
| 170 | - // Memcache will only allow an expiration timer less than 2592000 seconds, |
|
| 171 | - // otherwise, it will assume you're giving it a UNIX timestamp. |
|
| 172 | - if ($ttl > 2592000) { |
|
| 173 | - $ttl = time() + $ttl; |
|
| 174 | - } |
|
| 175 | - return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $ttl); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param string $key |
|
| 180 | - * @param string $encodedKey |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
| 184 | - { |
|
| 185 | - return $this->instance->delete($key); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * @return bool |
|
| 190 | - */ |
|
| 191 | - protected function driverClear(): bool |
|
| 192 | - { |
|
| 193 | - return $this->instance->flush(); |
|
| 194 | - } |
|
| 44 | + use TaggableCacheItemPoolTrait { |
|
| 45 | + __construct as protected __parentConstruct; |
|
| 46 | + } |
|
| 47 | + use MemcacheDriverCollisionDetectorTrait; |
|
| 48 | + |
|
| 49 | + protected int $memcacheFlags = 0; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Driver constructor. |
|
| 53 | + * @param ConfigurationOption $config |
|
| 54 | + * @param string $instanceId |
|
| 55 | + * @param EventManagerInterface $em |
|
| 56 | + * @throws PhpfastcacheDriverConnectException |
|
| 57 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 58 | + * @throws PhpfastcacheCoreException |
|
| 59 | + * @throws PhpfastcacheDriverCheckException |
|
| 60 | + * @throws PhpfastcacheIOException |
|
| 61 | + */ |
|
| 62 | + public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) |
|
| 63 | + { |
|
| 64 | + self::checkCollision('Memcache'); |
|
| 65 | + $this->__parentConstruct($config, $instanceId, $em); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return bool |
|
| 70 | + */ |
|
| 71 | + public function driverCheck(): bool |
|
| 72 | + { |
|
| 73 | + return class_exists('Memcache'); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @return DriverStatistic |
|
| 78 | + */ |
|
| 79 | + public function getStats(): DriverStatistic |
|
| 80 | + { |
|
| 81 | + $stats = (array)$this->instance->getstats(); |
|
| 82 | + $stats['uptime'] = (isset($stats['uptime']) ? $stats['uptime'] : 0); |
|
| 83 | + $stats['version'] = (isset($stats['version']) ? $stats['version'] : 'UnknownVersion'); |
|
| 84 | + $stats['bytes'] = (isset($stats['bytes']) ? $stats['version'] : 0); |
|
| 85 | + |
|
| 86 | + $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
| 87 | + |
|
| 88 | + return (new DriverStatistic()) |
|
| 89 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 90 | + ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
|
| 91 | + ->setRawData($stats) |
|
| 92 | + ->setSize((int)$stats['bytes']); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @return bool |
|
| 97 | + * @throws PhpfastcacheDriverException |
|
| 98 | + */ |
|
| 99 | + protected function driverConnect(): bool |
|
| 100 | + { |
|
| 101 | + $this->instance = new MemcacheSoftware(); |
|
| 102 | + |
|
| 103 | + foreach ($this->getConfig()->getServers() as $server) { |
|
| 104 | + try { |
|
| 105 | + /** |
|
| 106 | + * If path is provided we consider it as a UNIX Socket |
|
| 107 | + */ |
|
| 108 | + if (!empty($server['path'])) { |
|
| 109 | + $this->instance->addServer($server['path'], 0); |
|
| 110 | + } elseif (!empty($server['host'])) { |
|
| 111 | + $this->instance->addServer($server['host'], $server['port']); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
|
| 115 | + throw new PhpfastcacheDriverException('Unlike Memcached, Memcache does not support SASL authentication'); |
|
| 116 | + } |
|
| 117 | + } catch (Exception $e) { |
|
| 118 | + throw new PhpfastcacheDriverConnectException( |
|
| 119 | + sprintf( |
|
| 120 | + 'Failed to connect to memcache host/path "%s" with the following error: %s', |
|
| 121 | + $server['host'] ?: $server['path'], |
|
| 122 | + $e->getMessage() |
|
| 123 | + ) |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Since Memcached does not throw |
|
| 129 | + * any error if not connected ... |
|
| 130 | + */ |
|
| 131 | + if ( |
|
| 132 | + !$this->instance->getServerStatus( |
|
| 133 | + !empty($server['path']) ? $server['path'] : $server['host'], |
|
| 134 | + !empty($server['port']) ? $server['port'] : 0 |
|
| 135 | + ) |
|
| 136 | + ) { |
|
| 137 | + throw new PhpfastcacheDriverException('Memcache seems to not be connected'); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param ExtendedCacheItemInterface $item |
|
| 146 | + * @return ?array<string, mixed> |
|
| 147 | + */ |
|
| 148 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 149 | + { |
|
| 150 | + $val = $this->instance->get($item->getKey()); |
|
| 151 | + |
|
| 152 | + if (empty($val) || !\is_array($val)) { |
|
| 153 | + return null; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return $val; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param ExtendedCacheItemInterface $item |
|
| 161 | + * @return mixed |
|
| 162 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 163 | + * @throws PhpfastcacheLogicException |
|
| 164 | + */ |
|
| 165 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 166 | + { |
|
| 167 | + |
|
| 168 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 169 | + |
|
| 170 | + // Memcache will only allow an expiration timer less than 2592000 seconds, |
|
| 171 | + // otherwise, it will assume you're giving it a UNIX timestamp. |
|
| 172 | + if ($ttl > 2592000) { |
|
| 173 | + $ttl = time() + $ttl; |
|
| 174 | + } |
|
| 175 | + return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $ttl); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param string $key |
|
| 180 | + * @param string $encodedKey |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
| 184 | + { |
|
| 185 | + return $this->instance->delete($key); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * @return bool |
|
| 190 | + */ |
|
| 191 | + protected function driverClear(): bool |
|
| 192 | + { |
|
| 193 | + return $this->instance->flush(); |
|
| 194 | + } |
|
| 195 | 195 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public function getStats(): DriverStatistic |
| 74 | 74 | { |
| 75 | - $stats = (array)$this->instance->getstats(); |
|
| 75 | + $stats = (array) $this->instance->getstats(); |
|
| 76 | 76 | $stats['uptime'] = (isset($stats['uptime']) ? $stats['uptime'] : 0); |
| 77 | 77 | $stats['version'] = (isset($stats['version']) ? $stats['version'] : 'UnknownVersion'); |
| 78 | 78 | $stats['bytes'] = (isset($stats['bytes']) ? $stats['version'] : 0); |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | ->setData(implode(', ', array_keys($this->itemInstances))) |
| 84 | 84 | ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
| 85 | 85 | ->setRawData($stats) |
| 86 | - ->setSize((int)$stats['bytes']); |
|
| 86 | + ->setSize((int) $stats['bytes']); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -59,8 +59,7 @@ discard block |
||
| 59 | 59 | * @throws PhpfastcacheDriverCheckException |
| 60 | 60 | * @throws PhpfastcacheIOException |
| 61 | 61 | */ |
| 62 | - public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) |
|
| 63 | - { |
|
| 62 | + public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) { |
|
| 64 | 63 | self::checkCollision('Memcache'); |
| 65 | 64 | $this->__parentConstruct($config, $instanceId, $em); |
| 66 | 65 | } |
@@ -107,14 +106,16 @@ discard block |
||
| 107 | 106 | */ |
| 108 | 107 | if (!empty($server['path'])) { |
| 109 | 108 | $this->instance->addServer($server['path'], 0); |
| 110 | - } elseif (!empty($server['host'])) { |
|
| 109 | + } |
|
| 110 | + elseif (!empty($server['host'])) { |
|
| 111 | 111 | $this->instance->addServer($server['host'], $server['port']); |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
| 115 | 115 | throw new PhpfastcacheDriverException('Unlike Memcached, Memcache does not support SASL authentication'); |
| 116 | 116 | } |
| 117 | - } catch (Exception $e) { |
|
| 117 | + } |
|
| 118 | + catch (Exception $e) { |
|
| 118 | 119 | throw new PhpfastcacheDriverConnectException( |
| 119 | 120 | sprintf( |
| 120 | 121 | 'Failed to connect to memcache host/path "%s" with the following error: %s', |
@@ -23,119 +23,119 @@ |
||
| 23 | 23 | |
| 24 | 24 | class Config extends ConfigurationOption |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - * |
|
| 29 | - * Multiple server can be added this way: |
|
| 30 | - * $cfg->setServers([ |
|
| 31 | - * [ |
|
| 32 | - * // If you use an UNIX socket set the host and port to null |
|
| 33 | - * 'host' => '127.0.0.1', |
|
| 34 | - * //'path' => 'path/to/unix/socket', |
|
| 35 | - * 'port' => 11211, |
|
| 36 | - * ] |
|
| 37 | - * ]); |
|
| 38 | - */ |
|
| 39 | - |
|
| 40 | - /** @var array<array<string, mixed>> */ |
|
| 41 | - protected array $servers = []; |
|
| 42 | - protected string $host = '127.0.0.1'; |
|
| 43 | - protected int $port = 11211; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return array<array<string, mixed>> |
|
| 47 | - */ |
|
| 48 | - public function getServers(): array |
|
| 49 | - { |
|
| 50 | - if (!count($this->servers)) { |
|
| 51 | - return [ |
|
| 52 | - [ |
|
| 53 | - 'host' => $this->getHost(), |
|
| 54 | - 'path' => $this->getPath(), |
|
| 55 | - 'port' => $this->getPort(), |
|
| 56 | - ], |
|
| 57 | - ]; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $this->servers; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param array<array<string, mixed>> $servers |
|
| 65 | - * @return self |
|
| 66 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 67 | - * @throws PhpfastcacheLogicException |
|
| 68 | - * |
|
| 69 | - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
| 70 | - * @SuppressWarnings(PHPMD.NPathComplexity) |
|
| 71 | - */ |
|
| 72 | - public function setServers(array $servers): static |
|
| 73 | - { |
|
| 74 | - foreach ($servers as $server) { |
|
| 75 | - if (\array_key_exists('saslUser', $server) || array_key_exists('saslPassword', $server)) { |
|
| 76 | - throw new PhpfastcacheInvalidConfigurationException('Unlike Memcached, Memcache does not support SASL authentication'); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - if ($diff = array_diff(array_keys($server), ['host', 'port', 'path'])) { |
|
| 80 | - throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - if (!empty($server['host']) && !empty($server['path'])) { |
|
| 84 | - throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.'); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) { |
|
| 88 | - throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) { |
|
| 92 | - throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined'); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port']) || $server['port'] < 1)) { |
|
| 96 | - throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if (!empty($server['port']) && !empty($server['path'])) { |
|
| 100 | - throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - return $this->setProperty('servers', $servers); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function getHost(): string |
|
| 110 | - { |
|
| 111 | - return $this->host; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param string $host |
|
| 116 | - * @return self |
|
| 117 | - * @throws PhpfastcacheLogicException |
|
| 118 | - */ |
|
| 119 | - public function setHost(string $host): static |
|
| 120 | - { |
|
| 121 | - return $this->setProperty('host', $host); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return int |
|
| 126 | - */ |
|
| 127 | - public function getPort(): int |
|
| 128 | - { |
|
| 129 | - return $this->port; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @param int $port |
|
| 134 | - * @return self |
|
| 135 | - * @throws PhpfastcacheLogicException |
|
| 136 | - */ |
|
| 137 | - public function setPort(int $port): static |
|
| 138 | - { |
|
| 139 | - return $this->setProperty('port', $port); |
|
| 140 | - } |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + * |
|
| 29 | + * Multiple server can be added this way: |
|
| 30 | + * $cfg->setServers([ |
|
| 31 | + * [ |
|
| 32 | + * // If you use an UNIX socket set the host and port to null |
|
| 33 | + * 'host' => '127.0.0.1', |
|
| 34 | + * //'path' => 'path/to/unix/socket', |
|
| 35 | + * 'port' => 11211, |
|
| 36 | + * ] |
|
| 37 | + * ]); |
|
| 38 | + */ |
|
| 39 | + |
|
| 40 | + /** @var array<array<string, mixed>> */ |
|
| 41 | + protected array $servers = []; |
|
| 42 | + protected string $host = '127.0.0.1'; |
|
| 43 | + protected int $port = 11211; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return array<array<string, mixed>> |
|
| 47 | + */ |
|
| 48 | + public function getServers(): array |
|
| 49 | + { |
|
| 50 | + if (!count($this->servers)) { |
|
| 51 | + return [ |
|
| 52 | + [ |
|
| 53 | + 'host' => $this->getHost(), |
|
| 54 | + 'path' => $this->getPath(), |
|
| 55 | + 'port' => $this->getPort(), |
|
| 56 | + ], |
|
| 57 | + ]; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $this->servers; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param array<array<string, mixed>> $servers |
|
| 65 | + * @return self |
|
| 66 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 67 | + * @throws PhpfastcacheLogicException |
|
| 68 | + * |
|
| 69 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
| 70 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
|
| 71 | + */ |
|
| 72 | + public function setServers(array $servers): static |
|
| 73 | + { |
|
| 74 | + foreach ($servers as $server) { |
|
| 75 | + if (\array_key_exists('saslUser', $server) || array_key_exists('saslPassword', $server)) { |
|
| 76 | + throw new PhpfastcacheInvalidConfigurationException('Unlike Memcached, Memcache does not support SASL authentication'); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + if ($diff = array_diff(array_keys($server), ['host', 'port', 'path'])) { |
|
| 80 | + throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + if (!empty($server['host']) && !empty($server['path'])) { |
|
| 84 | + throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.'); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) { |
|
| 88 | + throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) { |
|
| 92 | + throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined'); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port']) || $server['port'] < 1)) { |
|
| 96 | + throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if (!empty($server['port']) && !empty($server['path'])) { |
|
| 100 | + throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + return $this->setProperty('servers', $servers); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function getHost(): string |
|
| 110 | + { |
|
| 111 | + return $this->host; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param string $host |
|
| 116 | + * @return self |
|
| 117 | + * @throws PhpfastcacheLogicException |
|
| 118 | + */ |
|
| 119 | + public function setHost(string $host): static |
|
| 120 | + { |
|
| 121 | + return $this->setProperty('host', $host); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return int |
|
| 126 | + */ |
|
| 127 | + public function getPort(): int |
|
| 128 | + { |
|
| 129 | + return $this->port; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @param int $port |
|
| 134 | + * @return self |
|
| 135 | + * @throws PhpfastcacheLogicException |
|
| 136 | + */ |
|
| 137 | + public function setPort(int $port): static |
|
| 138 | + { |
|
| 139 | + return $this->setProperty('port', $port); |
|
| 140 | + } |
|
| 141 | 141 | } |
@@ -46,279 +46,279 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | class Driver implements AggregatablePoolInterface |
| 48 | 48 | { |
| 49 | - use TaggableCacheItemPoolTrait; |
|
| 50 | - |
|
| 51 | - protected Scope $scope; |
|
| 52 | - |
|
| 53 | - protected Collection $collection; |
|
| 54 | - |
|
| 55 | - protected CouchbaseBucket $bucketInstance; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @return bool |
|
| 59 | - */ |
|
| 60 | - public function driverCheck(): bool |
|
| 61 | - { |
|
| 62 | - return extension_loaded('couchbase'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return bool |
|
| 67 | - * @throws PhpfastcacheDriverCheckException |
|
| 68 | - */ |
|
| 69 | - protected function driverConnect(): bool |
|
| 70 | - { |
|
| 71 | - if (!\class_exists(ClusterOptions::class)) { |
|
| 72 | - throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 2.x which is no longer supported in Phpfastcache v9'); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $connectionString = "couchbase://{$this->getConfig()->getHost()}:{$this->getConfig()->getPort()}"; |
|
| 76 | - |
|
| 77 | - $options = new ClusterOptions(); |
|
| 78 | - $options->credentials($this->getConfig()->getUsername(), $this->getConfig()->getPassword()); |
|
| 79 | - $this->instance = new Cluster($connectionString, $options); |
|
| 80 | - |
|
| 81 | - $this->setBucket($this->instance->bucket($this->getConfig()->getBucketName())); |
|
| 82 | - $this->setScope($this->getBucket()->scope($this->getConfig()->getScopeName())); |
|
| 83 | - $this->setCollection($this->getScope()->collection($this->getConfig()->getCollectionName())); |
|
| 84 | - |
|
| 85 | - return true; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param ExtendedCacheItemInterface $item |
|
| 90 | - * @return ?array<string, mixed> |
|
| 91 | - */ |
|
| 92 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 93 | - { |
|
| 94 | - try { |
|
| 95 | - /** |
|
| 96 | - * CouchbaseBucket::get() returns a GetResult interface |
|
| 97 | - */ |
|
| 98 | - return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 99 | - } catch (DocumentNotFoundException) { |
|
| 100 | - return null; |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @param ExtendedCacheItemInterface $item |
|
| 106 | - * @return array<array<string, mixed>> |
|
| 107 | - */ |
|
| 108 | - protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
| 109 | - { |
|
| 110 | - try { |
|
| 111 | - $results = []; |
|
| 112 | - /** |
|
| 113 | - * CouchbaseBucket::get() returns a GetResult interface |
|
| 114 | - */ |
|
| 115 | - /** @var GetResult $document */ |
|
| 116 | - foreach ($this->getCollection()->getMulti($this->getKeys($items, true)) as $document) { |
|
| 117 | - $content = $document->content(); |
|
| 118 | - if ($content) { |
|
| 119 | - $decodedDocument = $this->decodeDocument($content); |
|
| 120 | - $results[$decodedDocument[ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX]] = $this->decodeDocument($content); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $results; |
|
| 125 | - } catch (DocumentNotFoundException) { |
|
| 126 | - return []; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @param ExtendedCacheItemInterface $item |
|
| 132 | - * @return bool |
|
| 133 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 134 | - * @throws PhpfastcacheLogicException |
|
| 135 | - */ |
|
| 136 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 137 | - { |
|
| 138 | - |
|
| 139 | - try { |
|
| 140 | - $this->getCollection()->upsert( |
|
| 141 | - $item->getEncodedKey(), |
|
| 142 | - $this->encodeDocument($this->driverPreWrap($item)), |
|
| 143 | - (new UpsertOptions())->expiry($item->getTtl()) |
|
| 144 | - ); |
|
| 145 | - return true; |
|
| 146 | - } catch (CouchbaseException) { |
|
| 147 | - return false; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $key |
|
| 153 | - * @param string $encodedKey |
|
| 154 | - * @return bool |
|
| 155 | - */ |
|
| 156 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
| 157 | - { |
|
| 158 | - |
|
| 159 | - try { |
|
| 160 | - return $this->getCollection()->remove($encodedKey)->mutationToken() !== null; |
|
| 161 | - } catch (DocumentNotFoundException) { |
|
| 162 | - return true; |
|
| 163 | - } catch (CouchbaseException) { |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @param string[] $keys |
|
| 171 | - * @return bool |
|
| 172 | - */ |
|
| 173 | - protected function driverDeleteMultiple(array $keys): bool |
|
| 174 | - { |
|
| 175 | - try { |
|
| 176 | - $this->getCollection()->removeMulti(array_map(fn(string $key) => $this->getEncodedKey($key), $keys)); |
|
| 177 | - return true; |
|
| 178 | - } catch (CouchbaseException) { |
|
| 179 | - return false; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @return bool |
|
| 186 | - * @throws PhpfastcacheUnsupportedMethodException |
|
| 187 | - */ |
|
| 188 | - protected function driverClear(): bool |
|
| 189 | - { |
|
| 190 | - if (!$this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->flushEnabled()) { |
|
| 191 | - $this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->enableFlush(true); |
|
| 192 | - /** @phpstan-ignore-next-line */ |
|
| 193 | - if (!$this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->flushEnabled()) { |
|
| 194 | - throw new PhpfastcacheUnsupportedMethodException( |
|
| 195 | - 'Flushing operation is not enabled on your Bucket. See https://docs.couchbase.com/server/current/manage/manage-buckets/flush-bucket.html' |
|
| 196 | - ); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $this->instance->buckets()->flush($this->getConfig()->getBucketName()); |
|
| 201 | - |
|
| 202 | - return true; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @return DriverStatistic |
|
| 207 | - * @throws \Exception |
|
| 208 | - */ |
|
| 209 | - public function getStats(): DriverStatistic |
|
| 210 | - { |
|
| 211 | - /** |
|
| 212 | - * Between SDK 2 and 3 we lost a lot of useful information :( |
|
| 213 | - * @see https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html#management-apis |
|
| 214 | - */ |
|
| 215 | - $info = $this->getBucket()->diagnostics(\bin2hex(\random_bytes(16))); |
|
| 216 | - |
|
| 217 | - return (new DriverStatistic()) |
|
| 218 | - ->setSize(0) |
|
| 219 | - ->setRawData($info) |
|
| 220 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 221 | - ->setInfo($info['sdk'] . "\n For more information see RawData."); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @return Collection |
|
| 226 | - */ |
|
| 227 | - public function getCollection(): Collection |
|
| 228 | - { |
|
| 229 | - return $this->collection; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @param Collection $collection |
|
| 234 | - * @return Driver |
|
| 235 | - */ |
|
| 236 | - public function setCollection(Collection $collection): Driver |
|
| 237 | - { |
|
| 238 | - $this->collection = $collection; |
|
| 239 | - return $this; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @return Scope |
|
| 244 | - */ |
|
| 245 | - public function getScope(): Scope |
|
| 246 | - { |
|
| 247 | - return $this->scope; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param Scope $scope |
|
| 252 | - * @return Driver |
|
| 253 | - */ |
|
| 254 | - public function setScope(Scope $scope): Driver |
|
| 255 | - { |
|
| 256 | - $this->scope = $scope; |
|
| 257 | - return $this; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @return CouchbaseBucket |
|
| 262 | - */ |
|
| 263 | - protected function getBucket(): CouchbaseBucket |
|
| 264 | - { |
|
| 265 | - return $this->bucketInstance; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @param CouchbaseBucket $couchbaseBucket |
|
| 270 | - */ |
|
| 271 | - protected function setBucket(CouchbaseBucket $couchbaseBucket): void |
|
| 272 | - { |
|
| 273 | - $this->bucketInstance = $couchbaseBucket; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @param array<string, mixed> $data |
|
| 279 | - * @return array<string, mixed> |
|
| 280 | - */ |
|
| 281 | - protected function encodeDocument(array $data): array |
|
| 282 | - { |
|
| 283 | - $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 284 | - $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
|
| 285 | - ->format(DateTimeInterface::ATOM); |
|
| 286 | - |
|
| 287 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 288 | - $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
|
| 289 | - ->format(\DateTimeInterface::ATOM); |
|
| 290 | - |
|
| 291 | - $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] |
|
| 292 | - ->format(\DateTimeInterface::ATOM); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - return $data; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * @param array<string, mixed> $data |
|
| 300 | - * @return array<string, mixed> |
|
| 301 | - */ |
|
| 302 | - protected function decodeDocument(array $data): array |
|
| 303 | - { |
|
| 304 | - $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->unserialize($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 305 | - $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 306 | - \DateTimeInterface::ATOM, |
|
| 307 | - $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
|
| 308 | - ); |
|
| 309 | - |
|
| 310 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 311 | - $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 312 | - \DateTimeInterface::ATOM, |
|
| 313 | - $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
|
| 314 | - ); |
|
| 315 | - |
|
| 316 | - $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 317 | - \DateTimeInterface::ATOM, |
|
| 318 | - $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] |
|
| 319 | - ); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - return $data; |
|
| 323 | - } |
|
| 49 | + use TaggableCacheItemPoolTrait; |
|
| 50 | + |
|
| 51 | + protected Scope $scope; |
|
| 52 | + |
|
| 53 | + protected Collection $collection; |
|
| 54 | + |
|
| 55 | + protected CouchbaseBucket $bucketInstance; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @return bool |
|
| 59 | + */ |
|
| 60 | + public function driverCheck(): bool |
|
| 61 | + { |
|
| 62 | + return extension_loaded('couchbase'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return bool |
|
| 67 | + * @throws PhpfastcacheDriverCheckException |
|
| 68 | + */ |
|
| 69 | + protected function driverConnect(): bool |
|
| 70 | + { |
|
| 71 | + if (!\class_exists(ClusterOptions::class)) { |
|
| 72 | + throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 2.x which is no longer supported in Phpfastcache v9'); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $connectionString = "couchbase://{$this->getConfig()->getHost()}:{$this->getConfig()->getPort()}"; |
|
| 76 | + |
|
| 77 | + $options = new ClusterOptions(); |
|
| 78 | + $options->credentials($this->getConfig()->getUsername(), $this->getConfig()->getPassword()); |
|
| 79 | + $this->instance = new Cluster($connectionString, $options); |
|
| 80 | + |
|
| 81 | + $this->setBucket($this->instance->bucket($this->getConfig()->getBucketName())); |
|
| 82 | + $this->setScope($this->getBucket()->scope($this->getConfig()->getScopeName())); |
|
| 83 | + $this->setCollection($this->getScope()->collection($this->getConfig()->getCollectionName())); |
|
| 84 | + |
|
| 85 | + return true; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param ExtendedCacheItemInterface $item |
|
| 90 | + * @return ?array<string, mixed> |
|
| 91 | + */ |
|
| 92 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 93 | + { |
|
| 94 | + try { |
|
| 95 | + /** |
|
| 96 | + * CouchbaseBucket::get() returns a GetResult interface |
|
| 97 | + */ |
|
| 98 | + return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 99 | + } catch (DocumentNotFoundException) { |
|
| 100 | + return null; |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @param ExtendedCacheItemInterface $item |
|
| 106 | + * @return array<array<string, mixed>> |
|
| 107 | + */ |
|
| 108 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
| 109 | + { |
|
| 110 | + try { |
|
| 111 | + $results = []; |
|
| 112 | + /** |
|
| 113 | + * CouchbaseBucket::get() returns a GetResult interface |
|
| 114 | + */ |
|
| 115 | + /** @var GetResult $document */ |
|
| 116 | + foreach ($this->getCollection()->getMulti($this->getKeys($items, true)) as $document) { |
|
| 117 | + $content = $document->content(); |
|
| 118 | + if ($content) { |
|
| 119 | + $decodedDocument = $this->decodeDocument($content); |
|
| 120 | + $results[$decodedDocument[ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX]] = $this->decodeDocument($content); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $results; |
|
| 125 | + } catch (DocumentNotFoundException) { |
|
| 126 | + return []; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @param ExtendedCacheItemInterface $item |
|
| 132 | + * @return bool |
|
| 133 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 134 | + * @throws PhpfastcacheLogicException |
|
| 135 | + */ |
|
| 136 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 137 | + { |
|
| 138 | + |
|
| 139 | + try { |
|
| 140 | + $this->getCollection()->upsert( |
|
| 141 | + $item->getEncodedKey(), |
|
| 142 | + $this->encodeDocument($this->driverPreWrap($item)), |
|
| 143 | + (new UpsertOptions())->expiry($item->getTtl()) |
|
| 144 | + ); |
|
| 145 | + return true; |
|
| 146 | + } catch (CouchbaseException) { |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $key |
|
| 153 | + * @param string $encodedKey |
|
| 154 | + * @return bool |
|
| 155 | + */ |
|
| 156 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
| 157 | + { |
|
| 158 | + |
|
| 159 | + try { |
|
| 160 | + return $this->getCollection()->remove($encodedKey)->mutationToken() !== null; |
|
| 161 | + } catch (DocumentNotFoundException) { |
|
| 162 | + return true; |
|
| 163 | + } catch (CouchbaseException) { |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @param string[] $keys |
|
| 171 | + * @return bool |
|
| 172 | + */ |
|
| 173 | + protected function driverDeleteMultiple(array $keys): bool |
|
| 174 | + { |
|
| 175 | + try { |
|
| 176 | + $this->getCollection()->removeMulti(array_map(fn(string $key) => $this->getEncodedKey($key), $keys)); |
|
| 177 | + return true; |
|
| 178 | + } catch (CouchbaseException) { |
|
| 179 | + return false; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @return bool |
|
| 186 | + * @throws PhpfastcacheUnsupportedMethodException |
|
| 187 | + */ |
|
| 188 | + protected function driverClear(): bool |
|
| 189 | + { |
|
| 190 | + if (!$this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->flushEnabled()) { |
|
| 191 | + $this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->enableFlush(true); |
|
| 192 | + /** @phpstan-ignore-next-line */ |
|
| 193 | + if (!$this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->flushEnabled()) { |
|
| 194 | + throw new PhpfastcacheUnsupportedMethodException( |
|
| 195 | + 'Flushing operation is not enabled on your Bucket. See https://docs.couchbase.com/server/current/manage/manage-buckets/flush-bucket.html' |
|
| 196 | + ); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $this->instance->buckets()->flush($this->getConfig()->getBucketName()); |
|
| 201 | + |
|
| 202 | + return true; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @return DriverStatistic |
|
| 207 | + * @throws \Exception |
|
| 208 | + */ |
|
| 209 | + public function getStats(): DriverStatistic |
|
| 210 | + { |
|
| 211 | + /** |
|
| 212 | + * Between SDK 2 and 3 we lost a lot of useful information :( |
|
| 213 | + * @see https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html#management-apis |
|
| 214 | + */ |
|
| 215 | + $info = $this->getBucket()->diagnostics(\bin2hex(\random_bytes(16))); |
|
| 216 | + |
|
| 217 | + return (new DriverStatistic()) |
|
| 218 | + ->setSize(0) |
|
| 219 | + ->setRawData($info) |
|
| 220 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 221 | + ->setInfo($info['sdk'] . "\n For more information see RawData."); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @return Collection |
|
| 226 | + */ |
|
| 227 | + public function getCollection(): Collection |
|
| 228 | + { |
|
| 229 | + return $this->collection; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @param Collection $collection |
|
| 234 | + * @return Driver |
|
| 235 | + */ |
|
| 236 | + public function setCollection(Collection $collection): Driver |
|
| 237 | + { |
|
| 238 | + $this->collection = $collection; |
|
| 239 | + return $this; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @return Scope |
|
| 244 | + */ |
|
| 245 | + public function getScope(): Scope |
|
| 246 | + { |
|
| 247 | + return $this->scope; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param Scope $scope |
|
| 252 | + * @return Driver |
|
| 253 | + */ |
|
| 254 | + public function setScope(Scope $scope): Driver |
|
| 255 | + { |
|
| 256 | + $this->scope = $scope; |
|
| 257 | + return $this; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @return CouchbaseBucket |
|
| 262 | + */ |
|
| 263 | + protected function getBucket(): CouchbaseBucket |
|
| 264 | + { |
|
| 265 | + return $this->bucketInstance; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @param CouchbaseBucket $couchbaseBucket |
|
| 270 | + */ |
|
| 271 | + protected function setBucket(CouchbaseBucket $couchbaseBucket): void |
|
| 272 | + { |
|
| 273 | + $this->bucketInstance = $couchbaseBucket; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @param array<string, mixed> $data |
|
| 279 | + * @return array<string, mixed> |
|
| 280 | + */ |
|
| 281 | + protected function encodeDocument(array $data): array |
|
| 282 | + { |
|
| 283 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 284 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
|
| 285 | + ->format(DateTimeInterface::ATOM); |
|
| 286 | + |
|
| 287 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 288 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
|
| 289 | + ->format(\DateTimeInterface::ATOM); |
|
| 290 | + |
|
| 291 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] |
|
| 292 | + ->format(\DateTimeInterface::ATOM); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + return $data; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * @param array<string, mixed> $data |
|
| 300 | + * @return array<string, mixed> |
|
| 301 | + */ |
|
| 302 | + protected function decodeDocument(array $data): array |
|
| 303 | + { |
|
| 304 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->unserialize($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 305 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 306 | + \DateTimeInterface::ATOM, |
|
| 307 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
|
| 308 | + ); |
|
| 309 | + |
|
| 310 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 311 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 312 | + \DateTimeInterface::ATOM, |
|
| 313 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
|
| 314 | + ); |
|
| 315 | + |
|
| 316 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 317 | + \DateTimeInterface::ATOM, |
|
| 318 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] |
|
| 319 | + ); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + return $data; |
|
| 323 | + } |
|
| 324 | 324 | } |
@@ -95,7 +95,7 @@ |
||
| 95 | 95 | /** |
| 96 | 96 | * CouchbaseBucket::get() returns a GetResult interface |
| 97 | 97 | */ |
| 98 | - return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 98 | + return $this->decodeDocument((array) $this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 99 | 99 | } catch (DocumentNotFoundException) { |
| 100 | 100 | return null; |
| 101 | 101 | } |
@@ -96,7 +96,8 @@ discard block |
||
| 96 | 96 | * CouchbaseBucket::get() returns a GetResult interface |
| 97 | 97 | */ |
| 98 | 98 | return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
| 99 | - } catch (DocumentNotFoundException) { |
|
| 99 | + } |
|
| 100 | + catch (DocumentNotFoundException) { |
|
| 100 | 101 | return null; |
| 101 | 102 | } |
| 102 | 103 | } |
@@ -122,7 +123,8 @@ discard block |
||
| 122 | 123 | } |
| 123 | 124 | |
| 124 | 125 | return $results; |
| 125 | - } catch (DocumentNotFoundException) { |
|
| 126 | + } |
|
| 127 | + catch (DocumentNotFoundException) { |
|
| 126 | 128 | return []; |
| 127 | 129 | } |
| 128 | 130 | } |
@@ -143,7 +145,8 @@ discard block |
||
| 143 | 145 | (new UpsertOptions())->expiry($item->getTtl()) |
| 144 | 146 | ); |
| 145 | 147 | return true; |
| 146 | - } catch (CouchbaseException) { |
|
| 148 | + } |
|
| 149 | + catch (CouchbaseException) { |
|
| 147 | 150 | return false; |
| 148 | 151 | } |
| 149 | 152 | } |
@@ -158,9 +161,11 @@ discard block |
||
| 158 | 161 | |
| 159 | 162 | try { |
| 160 | 163 | return $this->getCollection()->remove($encodedKey)->mutationToken() !== null; |
| 161 | - } catch (DocumentNotFoundException) { |
|
| 164 | + } |
|
| 165 | + catch (DocumentNotFoundException) { |
|
| 162 | 166 | return true; |
| 163 | - } catch (CouchbaseException) { |
|
| 167 | + } |
|
| 168 | + catch (CouchbaseException) { |
|
| 164 | 169 | return false; |
| 165 | 170 | } |
| 166 | 171 | } |
@@ -175,7 +180,8 @@ discard block |
||
| 175 | 180 | try { |
| 176 | 181 | $this->getCollection()->removeMulti(array_map(fn(string $key) => $this->getEncodedKey($key), $keys)); |
| 177 | 182 | return true; |
| 178 | - } catch (CouchbaseException) { |
|
| 183 | + } |
|
| 184 | + catch (CouchbaseException) { |
|
| 179 | 185 | return false; |
| 180 | 186 | } |
| 181 | 187 | } |
@@ -21,137 +21,137 @@ |
||
| 21 | 21 | |
| 22 | 22 | class Config extends ConfigurationOption |
| 23 | 23 | { |
| 24 | - protected const DEFAULT_VALUE = '_default'; |
|
| 25 | - protected string $host = '127.0.0.1'; |
|
| 26 | - protected int $port = 8091; |
|
| 24 | + protected const DEFAULT_VALUE = '_default'; |
|
| 25 | + protected string $host = '127.0.0.1'; |
|
| 26 | + protected int $port = 8091; |
|
| 27 | 27 | // SSL: 18091 |
| 28 | 28 | |
| 29 | - protected string $username = ''; |
|
| 30 | - protected string $password = ''; |
|
| 31 | - protected string $bucketName = self::DEFAULT_VALUE; |
|
| 32 | - protected string $scopeName = self::DEFAULT_VALUE; |
|
| 33 | - protected string $collectionName = self::DEFAULT_VALUE; |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - public function getHost(): string |
|
| 37 | - { |
|
| 38 | - return $this->host; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param string $host |
|
| 43 | - * @return Config |
|
| 44 | - * @throws PhpfastcacheLogicException |
|
| 45 | - */ |
|
| 46 | - public function setHost(string $host): Config |
|
| 47 | - { |
|
| 48 | - return $this->setProperty('host', $host); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @return int |
|
| 53 | - */ |
|
| 54 | - public function getPort() |
|
| 55 | - { |
|
| 56 | - return $this->port; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param int $port |
|
| 61 | - * @return Config |
|
| 62 | - * @throws PhpfastcacheLogicException |
|
| 63 | - */ |
|
| 64 | - public function setPort(int $port): Config |
|
| 65 | - { |
|
| 66 | - return $this->setProperty('port', $port); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public function getUsername(): string |
|
| 73 | - { |
|
| 74 | - return $this->username; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param string $username |
|
| 79 | - * @return Config |
|
| 80 | - * @throws PhpfastcacheLogicException |
|
| 81 | - */ |
|
| 82 | - public function setUsername(string $username): Config |
|
| 83 | - { |
|
| 84 | - return $this->setProperty('username', $username); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function getPassword(): string |
|
| 91 | - { |
|
| 92 | - return $this->password; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @param string $password |
|
| 97 | - * @return Config |
|
| 98 | - * @throws PhpfastcacheLogicException |
|
| 99 | - */ |
|
| 100 | - public function setPassword(string $password): Config |
|
| 101 | - { |
|
| 102 | - return $this->setProperty('password', $password); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function getBucketName(): string |
|
| 109 | - { |
|
| 110 | - return $this->bucketName; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param string $bucketName |
|
| 115 | - * @return Config |
|
| 116 | - * @throws PhpfastcacheLogicException |
|
| 117 | - */ |
|
| 118 | - public function setBucketName(string $bucketName): Config |
|
| 119 | - { |
|
| 120 | - return $this->setProperty('bucketName', $bucketName); |
|
| 121 | - } |
|
| 122 | - /** |
|
| 123 | - * @return string |
|
| 124 | - */ |
|
| 125 | - public function getScopeName(): string |
|
| 126 | - { |
|
| 127 | - return $this->scopeName; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @param string $scopeName |
|
| 132 | - * @return Config |
|
| 133 | - * @throws PhpfastcacheLogicException |
|
| 134 | - */ |
|
| 135 | - public function setScopeName(string $scopeName): Config |
|
| 136 | - { |
|
| 137 | - return $this->setProperty('scopeName', $scopeName); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return string |
|
| 142 | - */ |
|
| 143 | - public function getCollectionName(): string |
|
| 144 | - { |
|
| 145 | - return $this->collectionName; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @param string $collectionName |
|
| 150 | - * @return Config |
|
| 151 | - * @throws PhpfastcacheLogicException |
|
| 152 | - */ |
|
| 153 | - public function setCollectionName(string $collectionName): Config |
|
| 154 | - { |
|
| 155 | - return $this->setProperty('collectionName', $collectionName); |
|
| 156 | - } |
|
| 29 | + protected string $username = ''; |
|
| 30 | + protected string $password = ''; |
|
| 31 | + protected string $bucketName = self::DEFAULT_VALUE; |
|
| 32 | + protected string $scopeName = self::DEFAULT_VALUE; |
|
| 33 | + protected string $collectionName = self::DEFAULT_VALUE; |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + public function getHost(): string |
|
| 37 | + { |
|
| 38 | + return $this->host; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param string $host |
|
| 43 | + * @return Config |
|
| 44 | + * @throws PhpfastcacheLogicException |
|
| 45 | + */ |
|
| 46 | + public function setHost(string $host): Config |
|
| 47 | + { |
|
| 48 | + return $this->setProperty('host', $host); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @return int |
|
| 53 | + */ |
|
| 54 | + public function getPort() |
|
| 55 | + { |
|
| 56 | + return $this->port; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param int $port |
|
| 61 | + * @return Config |
|
| 62 | + * @throws PhpfastcacheLogicException |
|
| 63 | + */ |
|
| 64 | + public function setPort(int $port): Config |
|
| 65 | + { |
|
| 66 | + return $this->setProperty('port', $port); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public function getUsername(): string |
|
| 73 | + { |
|
| 74 | + return $this->username; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param string $username |
|
| 79 | + * @return Config |
|
| 80 | + * @throws PhpfastcacheLogicException |
|
| 81 | + */ |
|
| 82 | + public function setUsername(string $username): Config |
|
| 83 | + { |
|
| 84 | + return $this->setProperty('username', $username); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function getPassword(): string |
|
| 91 | + { |
|
| 92 | + return $this->password; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @param string $password |
|
| 97 | + * @return Config |
|
| 98 | + * @throws PhpfastcacheLogicException |
|
| 99 | + */ |
|
| 100 | + public function setPassword(string $password): Config |
|
| 101 | + { |
|
| 102 | + return $this->setProperty('password', $password); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function getBucketName(): string |
|
| 109 | + { |
|
| 110 | + return $this->bucketName; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param string $bucketName |
|
| 115 | + * @return Config |
|
| 116 | + * @throws PhpfastcacheLogicException |
|
| 117 | + */ |
|
| 118 | + public function setBucketName(string $bucketName): Config |
|
| 119 | + { |
|
| 120 | + return $this->setProperty('bucketName', $bucketName); |
|
| 121 | + } |
|
| 122 | + /** |
|
| 123 | + * @return string |
|
| 124 | + */ |
|
| 125 | + public function getScopeName(): string |
|
| 126 | + { |
|
| 127 | + return $this->scopeName; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @param string $scopeName |
|
| 132 | + * @return Config |
|
| 133 | + * @throws PhpfastcacheLogicException |
|
| 134 | + */ |
|
| 135 | + public function setScopeName(string $scopeName): Config |
|
| 136 | + { |
|
| 137 | + return $this->setProperty('scopeName', $scopeName); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return string |
|
| 142 | + */ |
|
| 143 | + public function getCollectionName(): string |
|
| 144 | + { |
|
| 145 | + return $this->collectionName; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @param string $collectionName |
|
| 150 | + * @return Config |
|
| 151 | + * @throws PhpfastcacheLogicException |
|
| 152 | + */ |
|
| 153 | + public function setCollectionName(string $collectionName): Config |
|
| 154 | + { |
|
| 155 | + return $this->setProperty('collectionName', $collectionName); |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -51,8 +51,7 @@ |
||
| 51 | 51 | /** |
| 52 | 52 | * @return int |
| 53 | 53 | */ |
| 54 | - public function getPort() |
|
| 55 | - { |
|
| 54 | + public function getPort() { |
|
| 56 | 55 | return $this->port; |
| 57 | 56 | } |
| 58 | 57 | |