@@ -18,12 +18,12 @@ |
||
| 18 | 18 | |
| 19 | 19 | class PhpfastcacheCorruptedDataException extends PhpfastcacheDriverException |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @param string $message |
|
| 23 | - * @param mixed|null $corruptedData |
|
| 24 | - */ |
|
| 25 | - public function __construct(protected $message = '', protected mixed $corruptedData = null) |
|
| 26 | - { |
|
| 27 | - parent::__construct($message); |
|
| 28 | - } |
|
| 21 | + /** |
|
| 22 | + * @param string $message |
|
| 23 | + * @param mixed|null $corruptedData |
|
| 24 | + */ |
|
| 25 | + public function __construct(protected $message = '', protected mixed $corruptedData = null) |
|
| 26 | + { |
|
| 27 | + parent::__construct($message); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -22,8 +22,7 @@ |
||
| 22 | 22 | * @param string $message |
| 23 | 23 | * @param mixed|null $corruptedData |
| 24 | 24 | */ |
| 25 | - public function __construct(protected $message = '', protected mixed $corruptedData = null) |
|
| 26 | - { |
|
| 25 | + public function __construct(protected $message = '', protected mixed $corruptedData = null) { |
|
| 27 | 26 | parent::__construct($message); |
| 28 | 27 | } |
| 29 | 28 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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', |
@@ -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 | } |