@@ -20,119 +20,119 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Config extends ConfigurationOption |
| 22 | 22 | { |
| 23 | - protected const DEFAULT_VALUE = '_default'; |
|
| 24 | - /** |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $host = '127.0.0.1'; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var int |
|
| 31 | - */ |
|
| 32 | - protected $port = 8091;// SSL: 18091 |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $username = ''; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - protected $password = ''; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $bucketName = self::DEFAULT_VALUE; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function getHost(): string |
|
| 53 | - { |
|
| 54 | - return $this->host; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $host |
|
| 59 | - * @return Config |
|
| 60 | - */ |
|
| 61 | - public function setHost(string $host): Config |
|
| 62 | - { |
|
| 63 | - $this->host = $host; |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return int|null |
|
| 69 | - */ |
|
| 70 | - public function getPort() |
|
| 71 | - { |
|
| 72 | - return $this->port; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param int $port |
|
| 77 | - * @return Config |
|
| 78 | - */ |
|
| 79 | - public function setPort(int $port = null): Config |
|
| 80 | - { |
|
| 81 | - $this->port = $port; |
|
| 82 | - return $this; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function getUsername(): string |
|
| 89 | - { |
|
| 90 | - return $this->username; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param string $username |
|
| 95 | - * @return Config |
|
| 96 | - */ |
|
| 97 | - public function setUsername(string $username): Config |
|
| 98 | - { |
|
| 99 | - $this->username = $username; |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - public function getPassword(): string |
|
| 107 | - { |
|
| 108 | - return $this->password; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param string $password |
|
| 113 | - * @return Config |
|
| 114 | - */ |
|
| 115 | - public function setPassword(string $password): Config |
|
| 116 | - { |
|
| 117 | - $this->password = $password; |
|
| 118 | - return $this; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public function getBucketName(): string |
|
| 125 | - { |
|
| 126 | - return $this->bucketName; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param string $bucketName |
|
| 131 | - * @return Config |
|
| 132 | - */ |
|
| 133 | - public function setBucketName(string $bucketName): Config |
|
| 134 | - { |
|
| 135 | - $this->bucketName = $bucketName; |
|
| 136 | - return $this; |
|
| 137 | - } |
|
| 23 | + protected const DEFAULT_VALUE = '_default'; |
|
| 24 | + /** |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $host = '127.0.0.1'; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var int |
|
| 31 | + */ |
|
| 32 | + protected $port = 8091;// SSL: 18091 |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $username = ''; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + protected $password = ''; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $bucketName = self::DEFAULT_VALUE; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function getHost(): string |
|
| 53 | + { |
|
| 54 | + return $this->host; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $host |
|
| 59 | + * @return Config |
|
| 60 | + */ |
|
| 61 | + public function setHost(string $host): Config |
|
| 62 | + { |
|
| 63 | + $this->host = $host; |
|
| 64 | + return $this; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return int|null |
|
| 69 | + */ |
|
| 70 | + public function getPort() |
|
| 71 | + { |
|
| 72 | + return $this->port; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param int $port |
|
| 77 | + * @return Config |
|
| 78 | + */ |
|
| 79 | + public function setPort(int $port = null): Config |
|
| 80 | + { |
|
| 81 | + $this->port = $port; |
|
| 82 | + return $this; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function getUsername(): string |
|
| 89 | + { |
|
| 90 | + return $this->username; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param string $username |
|
| 95 | + * @return Config |
|
| 96 | + */ |
|
| 97 | + public function setUsername(string $username): Config |
|
| 98 | + { |
|
| 99 | + $this->username = $username; |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + public function getPassword(): string |
|
| 107 | + { |
|
| 108 | + return $this->password; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param string $password |
|
| 113 | + * @return Config |
|
| 114 | + */ |
|
| 115 | + public function setPassword(string $password): Config |
|
| 116 | + { |
|
| 117 | + $this->password = $password; |
|
| 118 | + return $this; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public function getBucketName(): string |
|
| 125 | + { |
|
| 126 | + return $this->bucketName; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param string $bucketName |
|
| 131 | + * @return Config |
|
| 132 | + */ |
|
| 133 | + public function setBucketName(string $bucketName): Config |
|
| 134 | + { |
|
| 135 | + $this->bucketName = $bucketName; |
|
| 136 | + return $this; |
|
| 137 | + } |
|
| 138 | 138 | } |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(CouchbaseDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof CouchbaseDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(CouchbaseDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof CouchbaseDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -38,221 +38,221 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 40 | 40 | { |
| 41 | - use DriverBaseTrait { |
|
| 42 | - __construct as __baseConstruct; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var CouchbaseBucket[] |
|
| 47 | - */ |
|
| 48 | - protected $bucketInstances = []; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var CouchbaseBucket |
|
| 52 | - */ |
|
| 53 | - protected $bucketInstance; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - protected $currentBucket = ''; |
|
| 59 | - |
|
| 60 | - public function __construct(ConfigurationOption $config, $instanceId) |
|
| 61 | - { |
|
| 62 | - // @todo Deprecation to enable in v8.1 |
|
| 63 | - // \trigger_error('Couchbase driver is now deprecated and will be removed in the V9, use Couchbasev3 instead which will support SDK 3.', \E_USER_DEPRECATED); |
|
| 64 | - $this->__baseConstruct($config, $instanceId); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return bool |
|
| 69 | - */ |
|
| 70 | - public function driverCheck(): bool |
|
| 71 | - { |
|
| 72 | - return extension_loaded('couchbase'); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @return DriverStatistic |
|
| 77 | - */ |
|
| 78 | - public function getStats(): DriverStatistic |
|
| 79 | - { |
|
| 80 | - $info = $this->getBucket()->manager()->info(); |
|
| 81 | - |
|
| 82 | - return (new DriverStatistic()) |
|
| 83 | - ->setSize($info['basicStats']['diskUsed']) |
|
| 84 | - ->setRawData($info) |
|
| 85 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 86 | - ->setInfo( |
|
| 87 | - 'CouchBase version ' . $info['nodes'][0]['version'] . ', Uptime (in days): ' . round( |
|
| 88 | - $info['nodes'][0]['uptime'] / 86400, |
|
| 89 | - 1 |
|
| 90 | - ) . "\n For more information see RawData." |
|
| 91 | - ); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @return bool |
|
| 96 | - * @throws PhpfastcacheLogicException |
|
| 97 | - */ |
|
| 98 | - protected function driverConnect(): bool |
|
| 99 | - { |
|
| 100 | - if (\class_exists(\Couchbase\ClusterOptions::class)) { |
|
| 101 | - throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 3.x so please use driver Couchbasev3'); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - if ($this->instance instanceof CouchbaseClient) { |
|
| 105 | - throw new PhpfastcacheLogicException('Already connected to Couchbase server'); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $clientConfig = $this->getConfig(); |
|
| 109 | - |
|
| 110 | - $authenticator = new PasswordAuthenticator(); |
|
| 111 | - $authenticator->username($clientConfig->getUsername())->password($clientConfig->getPassword()); |
|
| 112 | - |
|
| 113 | - $this->instance = new CouchbaseClient( |
|
| 114 | - 'couchbase://' . $clientConfig->getHost() . ($clientConfig->getPort() ? ":{$clientConfig->getPort()}" : '') |
|
| 115 | - ); |
|
| 116 | - |
|
| 117 | - $this->instance->authenticate($authenticator); |
|
| 118 | - $this->setBucket($this->instance->openBucket($clientConfig->getBucketName())); |
|
| 119 | - |
|
| 120 | - return true; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param CouchbaseBucket $CouchbaseBucket |
|
| 125 | - */ |
|
| 126 | - protected function setBucket(CouchbaseBucket $CouchbaseBucket) |
|
| 127 | - { |
|
| 128 | - $this->bucketInstance = $CouchbaseBucket; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param CacheItemInterface $item |
|
| 133 | - * @return null|array |
|
| 134 | - */ |
|
| 135 | - protected function driverRead(CacheItemInterface $item) |
|
| 136 | - { |
|
| 137 | - try { |
|
| 138 | - /** |
|
| 139 | - * CouchbaseBucket::get() returns a CouchbaseMetaDoc object |
|
| 140 | - */ |
|
| 141 | - return $this->decodeDocument((array) $this->getBucket()->get($item->getEncodedKey())->value); |
|
| 142 | - } catch (CouchbaseException $e) { |
|
| 143 | - return null; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @return CouchbaseBucket |
|
| 149 | - */ |
|
| 150 | - protected function getBucket(): CouchbaseBucket |
|
| 151 | - { |
|
| 152 | - return $this->bucketInstance; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @param CacheItemInterface $item |
|
| 157 | - * @return bool |
|
| 158 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 159 | - */ |
|
| 160 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 161 | - { |
|
| 162 | - /** |
|
| 163 | - * Check for Cross-Driver type confusion |
|
| 164 | - */ |
|
| 165 | - if ($item instanceof Item) { |
|
| 166 | - try { |
|
| 167 | - return (bool)$this->getBucket()->upsert( |
|
| 168 | - $item->getEncodedKey(), |
|
| 169 | - $this->encodeDocument($this->driverPreWrap($item)), |
|
| 170 | - ['expiry' => $item->getTtl()] |
|
| 171 | - ); |
|
| 172 | - } catch (CouchbaseException $e) { |
|
| 173 | - return false; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param CacheItemInterface $item |
|
| 182 | - * @return bool |
|
| 183 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 184 | - */ |
|
| 185 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 186 | - { |
|
| 187 | - /** |
|
| 188 | - * Check for Cross-Driver type confusion |
|
| 189 | - */ |
|
| 190 | - if ($item instanceof Item) { |
|
| 191 | - try { |
|
| 192 | - return (bool)$this->getBucket()->remove($item->getEncodedKey()); |
|
| 193 | - } catch (Exception $e) { |
|
| 194 | - return $e->getCode() === COUCHBASE_KEY_ENOENT; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @param array $data |
|
| 203 | - * @return array |
|
| 204 | - */ |
|
| 205 | - protected function encodeDocument(array $data): array |
|
| 206 | - { |
|
| 207 | - $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 208 | - $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
|
| 209 | - |
|
| 210 | - if($this->getConfig()->isItemDetailedDate()){ |
|
| 211 | - $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
|
| 212 | - $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return $data; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @param array $data |
|
| 220 | - * @return array |
|
| 221 | - */ |
|
| 222 | - protected function decodeDocument(array $data): array |
|
| 223 | - { |
|
| 224 | - $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->decode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 225 | - $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 226 | - \DateTime::ATOM, |
|
| 227 | - $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - if($this->getConfig()->isItemDetailedDate()){ |
|
| 231 | - $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 232 | - \DateTime::ATOM, |
|
| 233 | - $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
|
| 234 | - ); |
|
| 235 | - |
|
| 236 | - $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 237 | - \DateTime::ATOM, |
|
| 238 | - $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] |
|
| 239 | - ); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - return $data; |
|
| 243 | - } |
|
| 244 | - /******************** |
|
| 41 | + use DriverBaseTrait { |
|
| 42 | + __construct as __baseConstruct; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var CouchbaseBucket[] |
|
| 47 | + */ |
|
| 48 | + protected $bucketInstances = []; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var CouchbaseBucket |
|
| 52 | + */ |
|
| 53 | + protected $bucketInstance; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + protected $currentBucket = ''; |
|
| 59 | + |
|
| 60 | + public function __construct(ConfigurationOption $config, $instanceId) |
|
| 61 | + { |
|
| 62 | + // @todo Deprecation to enable in v8.1 |
|
| 63 | + // \trigger_error('Couchbase driver is now deprecated and will be removed in the V9, use Couchbasev3 instead which will support SDK 3.', \E_USER_DEPRECATED); |
|
| 64 | + $this->__baseConstruct($config, $instanceId); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return bool |
|
| 69 | + */ |
|
| 70 | + public function driverCheck(): bool |
|
| 71 | + { |
|
| 72 | + return extension_loaded('couchbase'); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @return DriverStatistic |
|
| 77 | + */ |
|
| 78 | + public function getStats(): DriverStatistic |
|
| 79 | + { |
|
| 80 | + $info = $this->getBucket()->manager()->info(); |
|
| 81 | + |
|
| 82 | + return (new DriverStatistic()) |
|
| 83 | + ->setSize($info['basicStats']['diskUsed']) |
|
| 84 | + ->setRawData($info) |
|
| 85 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 86 | + ->setInfo( |
|
| 87 | + 'CouchBase version ' . $info['nodes'][0]['version'] . ', Uptime (in days): ' . round( |
|
| 88 | + $info['nodes'][0]['uptime'] / 86400, |
|
| 89 | + 1 |
|
| 90 | + ) . "\n For more information see RawData." |
|
| 91 | + ); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @return bool |
|
| 96 | + * @throws PhpfastcacheLogicException |
|
| 97 | + */ |
|
| 98 | + protected function driverConnect(): bool |
|
| 99 | + { |
|
| 100 | + if (\class_exists(\Couchbase\ClusterOptions::class)) { |
|
| 101 | + throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 3.x so please use driver Couchbasev3'); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + if ($this->instance instanceof CouchbaseClient) { |
|
| 105 | + throw new PhpfastcacheLogicException('Already connected to Couchbase server'); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $clientConfig = $this->getConfig(); |
|
| 109 | + |
|
| 110 | + $authenticator = new PasswordAuthenticator(); |
|
| 111 | + $authenticator->username($clientConfig->getUsername())->password($clientConfig->getPassword()); |
|
| 112 | + |
|
| 113 | + $this->instance = new CouchbaseClient( |
|
| 114 | + 'couchbase://' . $clientConfig->getHost() . ($clientConfig->getPort() ? ":{$clientConfig->getPort()}" : '') |
|
| 115 | + ); |
|
| 116 | + |
|
| 117 | + $this->instance->authenticate($authenticator); |
|
| 118 | + $this->setBucket($this->instance->openBucket($clientConfig->getBucketName())); |
|
| 119 | + |
|
| 120 | + return true; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param CouchbaseBucket $CouchbaseBucket |
|
| 125 | + */ |
|
| 126 | + protected function setBucket(CouchbaseBucket $CouchbaseBucket) |
|
| 127 | + { |
|
| 128 | + $this->bucketInstance = $CouchbaseBucket; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param CacheItemInterface $item |
|
| 133 | + * @return null|array |
|
| 134 | + */ |
|
| 135 | + protected function driverRead(CacheItemInterface $item) |
|
| 136 | + { |
|
| 137 | + try { |
|
| 138 | + /** |
|
| 139 | + * CouchbaseBucket::get() returns a CouchbaseMetaDoc object |
|
| 140 | + */ |
|
| 141 | + return $this->decodeDocument((array) $this->getBucket()->get($item->getEncodedKey())->value); |
|
| 142 | + } catch (CouchbaseException $e) { |
|
| 143 | + return null; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @return CouchbaseBucket |
|
| 149 | + */ |
|
| 150 | + protected function getBucket(): CouchbaseBucket |
|
| 151 | + { |
|
| 152 | + return $this->bucketInstance; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @param CacheItemInterface $item |
|
| 157 | + * @return bool |
|
| 158 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 159 | + */ |
|
| 160 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 161 | + { |
|
| 162 | + /** |
|
| 163 | + * Check for Cross-Driver type confusion |
|
| 164 | + */ |
|
| 165 | + if ($item instanceof Item) { |
|
| 166 | + try { |
|
| 167 | + return (bool)$this->getBucket()->upsert( |
|
| 168 | + $item->getEncodedKey(), |
|
| 169 | + $this->encodeDocument($this->driverPreWrap($item)), |
|
| 170 | + ['expiry' => $item->getTtl()] |
|
| 171 | + ); |
|
| 172 | + } catch (CouchbaseException $e) { |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param CacheItemInterface $item |
|
| 182 | + * @return bool |
|
| 183 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 184 | + */ |
|
| 185 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 186 | + { |
|
| 187 | + /** |
|
| 188 | + * Check for Cross-Driver type confusion |
|
| 189 | + */ |
|
| 190 | + if ($item instanceof Item) { |
|
| 191 | + try { |
|
| 192 | + return (bool)$this->getBucket()->remove($item->getEncodedKey()); |
|
| 193 | + } catch (Exception $e) { |
|
| 194 | + return $e->getCode() === COUCHBASE_KEY_ENOENT; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @param array $data |
|
| 203 | + * @return array |
|
| 204 | + */ |
|
| 205 | + protected function encodeDocument(array $data): array |
|
| 206 | + { |
|
| 207 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 208 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
|
| 209 | + |
|
| 210 | + if($this->getConfig()->isItemDetailedDate()){ |
|
| 211 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
|
| 212 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return $data; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @param array $data |
|
| 220 | + * @return array |
|
| 221 | + */ |
|
| 222 | + protected function decodeDocument(array $data): array |
|
| 223 | + { |
|
| 224 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->decode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
| 225 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 226 | + \DateTime::ATOM, |
|
| 227 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + if($this->getConfig()->isItemDetailedDate()){ |
|
| 231 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 232 | + \DateTime::ATOM, |
|
| 233 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
|
| 234 | + ); |
|
| 235 | + |
|
| 236 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
|
| 237 | + \DateTime::ATOM, |
|
| 238 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] |
|
| 239 | + ); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + return $data; |
|
| 243 | + } |
|
| 244 | + /******************** |
|
| 245 | 245 | * |
| 246 | 246 | * PSR-6 Extended Methods |
| 247 | 247 | * |
| 248 | 248 | *******************/ |
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * @return bool |
|
| 252 | - */ |
|
| 253 | - protected function driverClear(): bool |
|
| 254 | - { |
|
| 255 | - $this->getBucket()->manager()->flush(); |
|
| 256 | - return true; |
|
| 257 | - } |
|
| 250 | + /** |
|
| 251 | + * @return bool |
|
| 252 | + */ |
|
| 253 | + protected function driverClear(): bool |
|
| 254 | + { |
|
| 255 | + $this->getBucket()->manager()->flush(); |
|
| 256 | + return true; |
|
| 257 | + } |
|
| 258 | 258 | } |
@@ -20,26 +20,26 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Config extends ConfigurationOption |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * @var bool |
|
| 25 | - */ |
|
| 26 | - protected $htaccess = true; |
|
| 23 | + /** |
|
| 24 | + * @var bool |
|
| 25 | + */ |
|
| 26 | + protected $htaccess = true; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @return bool |
|
| 30 | - */ |
|
| 31 | - public function getHtaccess(): bool |
|
| 32 | - { |
|
| 33 | - return $this->htaccess; |
|
| 34 | - } |
|
| 28 | + /** |
|
| 29 | + * @return bool |
|
| 30 | + */ |
|
| 31 | + public function getHtaccess(): bool |
|
| 32 | + { |
|
| 33 | + return $this->htaccess; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param bool $htaccess |
|
| 38 | - * @return self |
|
| 39 | - */ |
|
| 40 | - public function setHtaccess(bool $htaccess): self |
|
| 41 | - { |
|
| 42 | - $this->htaccess = $htaccess; |
|
| 43 | - return $this; |
|
| 44 | - } |
|
| 36 | + /** |
|
| 37 | + * @param bool $htaccess |
|
| 38 | + * @return self |
|
| 39 | + */ |
|
| 40 | + public function setHtaccess(bool $htaccess): self |
|
| 41 | + { |
|
| 42 | + $this->htaccess = $htaccess; |
|
| 43 | + return $this; |
|
| 44 | + } |
|
| 45 | 45 | } |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(LeveldbDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof LeveldbDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(LeveldbDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof LeveldbDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -32,114 +32,114 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 34 | 34 | { |
| 35 | - use DriverBaseTrait; |
|
| 36 | - use IOHelperTrait; |
|
| 37 | - |
|
| 38 | - protected const LEVELDB_FILENAME = '.database'; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - public function driverCheck(): bool |
|
| 44 | - { |
|
| 45 | - return extension_loaded('Leveldb'); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Close connection on destruct |
|
| 50 | - */ |
|
| 51 | - public function __destruct() |
|
| 52 | - { |
|
| 53 | - if ($this->instance instanceof LeveldbClient) { |
|
| 54 | - $this->instance->close(); |
|
| 55 | - $this->instance = null; |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param CacheItemInterface $item |
|
| 61 | - * @return null|array |
|
| 62 | - */ |
|
| 63 | - protected function driverRead(CacheItemInterface $item) |
|
| 64 | - { |
|
| 65 | - $val = $this->instance->get($item->getKey()); |
|
| 66 | - if (!$val) { |
|
| 67 | - return null; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return $this->decode($val); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param CacheItemInterface $item |
|
| 75 | - * @return bool |
|
| 76 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 77 | - */ |
|
| 78 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 79 | - { |
|
| 80 | - /** |
|
| 81 | - * Check for Cross-Driver type confusion |
|
| 82 | - */ |
|
| 83 | - if ($item instanceof Item) { |
|
| 84 | - return (bool)$this->instance->set($item->getKey(), $this->encode($this->driverPreWrap($item))); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param CacheItemInterface $item |
|
| 92 | - * @return bool |
|
| 93 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 94 | - */ |
|
| 95 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 96 | - { |
|
| 97 | - /** |
|
| 98 | - * Check for Cross-Driver type confusion |
|
| 99 | - */ |
|
| 100 | - if ($item instanceof Item) { |
|
| 101 | - return $this->instance->delete($item->getKey()); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @return bool |
|
| 109 | - */ |
|
| 110 | - protected function driverClear(): bool |
|
| 111 | - { |
|
| 112 | - if ($this->instance instanceof LeveldbClient) { |
|
| 113 | - $this->instance->close(); |
|
| 114 | - $this->instance = null; |
|
| 115 | - } |
|
| 116 | - $result = (bool)LeveldbClient::destroy($this->getLeveldbFile()); |
|
| 117 | - $this->driverConnect(); |
|
| 118 | - |
|
| 119 | - return $result; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @return string |
|
| 124 | - * @throws PhpfastcacheCoreException |
|
| 125 | - */ |
|
| 126 | - public function getLeveldbFile(): string |
|
| 127 | - { |
|
| 128 | - return $this->getPath() . '/' . self::LEVELDB_FILENAME; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @return bool |
|
| 133 | - * @throws PhpfastcacheLogicException |
|
| 134 | - */ |
|
| 135 | - protected function driverConnect(): bool |
|
| 136 | - { |
|
| 137 | - if ($this->instance instanceof LeveldbClient) { |
|
| 138 | - throw new PhpfastcacheLogicException('Already connected to Leveldb database'); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $this->instance = $this->instance ?: new LeveldbClient($this->getLeveldbFile()); |
|
| 142 | - |
|
| 143 | - return true; |
|
| 144 | - } |
|
| 35 | + use DriverBaseTrait; |
|
| 36 | + use IOHelperTrait; |
|
| 37 | + |
|
| 38 | + protected const LEVELDB_FILENAME = '.database'; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + public function driverCheck(): bool |
|
| 44 | + { |
|
| 45 | + return extension_loaded('Leveldb'); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Close connection on destruct |
|
| 50 | + */ |
|
| 51 | + public function __destruct() |
|
| 52 | + { |
|
| 53 | + if ($this->instance instanceof LeveldbClient) { |
|
| 54 | + $this->instance->close(); |
|
| 55 | + $this->instance = null; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param CacheItemInterface $item |
|
| 61 | + * @return null|array |
|
| 62 | + */ |
|
| 63 | + protected function driverRead(CacheItemInterface $item) |
|
| 64 | + { |
|
| 65 | + $val = $this->instance->get($item->getKey()); |
|
| 66 | + if (!$val) { |
|
| 67 | + return null; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return $this->decode($val); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param CacheItemInterface $item |
|
| 75 | + * @return bool |
|
| 76 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 77 | + */ |
|
| 78 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 79 | + { |
|
| 80 | + /** |
|
| 81 | + * Check for Cross-Driver type confusion |
|
| 82 | + */ |
|
| 83 | + if ($item instanceof Item) { |
|
| 84 | + return (bool)$this->instance->set($item->getKey(), $this->encode($this->driverPreWrap($item))); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param CacheItemInterface $item |
|
| 92 | + * @return bool |
|
| 93 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 94 | + */ |
|
| 95 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 96 | + { |
|
| 97 | + /** |
|
| 98 | + * Check for Cross-Driver type confusion |
|
| 99 | + */ |
|
| 100 | + if ($item instanceof Item) { |
|
| 101 | + return $this->instance->delete($item->getKey()); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @return bool |
|
| 109 | + */ |
|
| 110 | + protected function driverClear(): bool |
|
| 111 | + { |
|
| 112 | + if ($this->instance instanceof LeveldbClient) { |
|
| 113 | + $this->instance->close(); |
|
| 114 | + $this->instance = null; |
|
| 115 | + } |
|
| 116 | + $result = (bool)LeveldbClient::destroy($this->getLeveldbFile()); |
|
| 117 | + $this->driverConnect(); |
|
| 118 | + |
|
| 119 | + return $result; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @return string |
|
| 124 | + * @throws PhpfastcacheCoreException |
|
| 125 | + */ |
|
| 126 | + public function getLeveldbFile(): string |
|
| 127 | + { |
|
| 128 | + return $this->getPath() . '/' . self::LEVELDB_FILENAME; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @return bool |
|
| 133 | + * @throws PhpfastcacheLogicException |
|
| 134 | + */ |
|
| 135 | + protected function driverConnect(): bool |
|
| 136 | + { |
|
| 137 | + if ($this->instance instanceof LeveldbClient) { |
|
| 138 | + throw new PhpfastcacheLogicException('Already connected to Leveldb database'); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $this->instance = $this->instance ?: new LeveldbClient($this->getLeveldbFile()); |
|
| 142 | + |
|
| 143 | + return true; |
|
| 144 | + } |
|
| 145 | 145 | } |
@@ -22,185 +22,185 @@ |
||
| 22 | 22 | |
| 23 | 23 | class Config extends ConfigurationOption |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - * |
|
| 28 | - * Multiple server can be added this way: |
|
| 29 | - * $cfg->setServers([ |
|
| 30 | - * [ |
|
| 31 | - * // If you use an UNIX socket set the host and port to null |
|
| 32 | - * 'host' => '127.0.0.1', |
|
| 33 | - * //'path' => 'path/to/unix/socket', |
|
| 34 | - * 'port' => 11211, |
|
| 35 | - * 'saslUser' => null, |
|
| 36 | - * 'saslPassword' => null, |
|
| 37 | - * ] |
|
| 38 | - * ]); |
|
| 39 | - */ |
|
| 40 | - protected $servers = []; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - protected $host = '127.0.0.1'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var int |
|
| 49 | - */ |
|
| 50 | - protected $port = 11211; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - protected $saslUser = ''; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - protected $saslPassword = ''; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var string |
|
| 64 | - */ |
|
| 65 | - protected $optPrefix = ''; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return string |
|
| 69 | - */ |
|
| 70 | - public function getSaslUser(): string |
|
| 71 | - { |
|
| 72 | - return $this->saslUser; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $saslUser |
|
| 77 | - * @return self |
|
| 78 | - */ |
|
| 79 | - public function setSaslUser(string $saslUser): self |
|
| 80 | - { |
|
| 81 | - $this->saslUser = $saslUser; |
|
| 82 | - return $this; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function getSaslPassword(): string |
|
| 89 | - { |
|
| 90 | - return $this->saslPassword; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param string $saslPassword |
|
| 95 | - * @return self |
|
| 96 | - */ |
|
| 97 | - public function setSaslPassword(string $saslPassword): self |
|
| 98 | - { |
|
| 99 | - $this->saslPassword = $saslPassword; |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return array |
|
| 105 | - */ |
|
| 106 | - public function getServers(): array |
|
| 107 | - { |
|
| 108 | - return $this->servers; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param array $servers |
|
| 113 | - * @return self |
|
| 114 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 115 | - */ |
|
| 116 | - public function setServers(array $servers): self |
|
| 117 | - { |
|
| 118 | - foreach ($servers as $server) { |
|
| 119 | - if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword', 'path'])) { |
|
| 120 | - throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if (!empty($server['host']) && !empty($server['path'])) { |
|
| 124 | - throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.'); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) { |
|
| 128 | - throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined'); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) { |
|
| 132 | - throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined'); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) { |
|
| 136 | - throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if (!empty($server['port']) && !empty($server['path'])) { |
|
| 140 | - throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - if (!empty($server['saslUser']) && !empty($server['saslPassword']) && (!is_string($server['saslUser']) || !is_string($server['saslPassword']))) { |
|
| 144 | - throw new PhpfastcacheInvalidConfigurationException('If provided, saslUser and saslPassword must be a string'); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - $this->servers = $servers; |
|
| 148 | - return $this; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @return string |
|
| 153 | - */ |
|
| 154 | - public function getHost(): string |
|
| 155 | - { |
|
| 156 | - return $this->host; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param string $host |
|
| 161 | - * @return self |
|
| 162 | - */ |
|
| 163 | - public function setHost(string $host): self |
|
| 164 | - { |
|
| 165 | - $this->host = $host; |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return int |
|
| 171 | - */ |
|
| 172 | - public function getPort(): int |
|
| 173 | - { |
|
| 174 | - return $this->port; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @param int $port |
|
| 179 | - * @return Config |
|
| 180 | - */ |
|
| 181 | - public function setPort(int $port): self |
|
| 182 | - { |
|
| 183 | - $this->port = $port; |
|
| 184 | - return $this; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @return string |
|
| 189 | - * @since 8.0.2 |
|
| 190 | - */ |
|
| 191 | - public function getOptPrefix(): string |
|
| 192 | - { |
|
| 193 | - return $this->optPrefix; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @param string $optPrefix |
|
| 198 | - * @return Config |
|
| 199 | - * @since 8.0.2 |
|
| 200 | - */ |
|
| 201 | - public function setOptPrefix(string $optPrefix): Config |
|
| 202 | - { |
|
| 203 | - $this->optPrefix = trim($optPrefix); |
|
| 204 | - return $this; |
|
| 205 | - } |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + * |
|
| 28 | + * Multiple server can be added this way: |
|
| 29 | + * $cfg->setServers([ |
|
| 30 | + * [ |
|
| 31 | + * // If you use an UNIX socket set the host and port to null |
|
| 32 | + * 'host' => '127.0.0.1', |
|
| 33 | + * //'path' => 'path/to/unix/socket', |
|
| 34 | + * 'port' => 11211, |
|
| 35 | + * 'saslUser' => null, |
|
| 36 | + * 'saslPassword' => null, |
|
| 37 | + * ] |
|
| 38 | + * ]); |
|
| 39 | + */ |
|
| 40 | + protected $servers = []; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + protected $host = '127.0.0.1'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var int |
|
| 49 | + */ |
|
| 50 | + protected $port = 11211; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + protected $saslUser = ''; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + protected $saslPassword = ''; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var string |
|
| 64 | + */ |
|
| 65 | + protected $optPrefix = ''; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return string |
|
| 69 | + */ |
|
| 70 | + public function getSaslUser(): string |
|
| 71 | + { |
|
| 72 | + return $this->saslUser; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $saslUser |
|
| 77 | + * @return self |
|
| 78 | + */ |
|
| 79 | + public function setSaslUser(string $saslUser): self |
|
| 80 | + { |
|
| 81 | + $this->saslUser = $saslUser; |
|
| 82 | + return $this; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function getSaslPassword(): string |
|
| 89 | + { |
|
| 90 | + return $this->saslPassword; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param string $saslPassword |
|
| 95 | + * @return self |
|
| 96 | + */ |
|
| 97 | + public function setSaslPassword(string $saslPassword): self |
|
| 98 | + { |
|
| 99 | + $this->saslPassword = $saslPassword; |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return array |
|
| 105 | + */ |
|
| 106 | + public function getServers(): array |
|
| 107 | + { |
|
| 108 | + return $this->servers; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param array $servers |
|
| 113 | + * @return self |
|
| 114 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 115 | + */ |
|
| 116 | + public function setServers(array $servers): self |
|
| 117 | + { |
|
| 118 | + foreach ($servers as $server) { |
|
| 119 | + if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword', 'path'])) { |
|
| 120 | + throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if (!empty($server['host']) && !empty($server['path'])) { |
|
| 124 | + throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.'); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) { |
|
| 128 | + throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined'); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) { |
|
| 132 | + throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined'); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) { |
|
| 136 | + throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if (!empty($server['port']) && !empty($server['path'])) { |
|
| 140 | + throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + if (!empty($server['saslUser']) && !empty($server['saslPassword']) && (!is_string($server['saslUser']) || !is_string($server['saslPassword']))) { |
|
| 144 | + throw new PhpfastcacheInvalidConfigurationException('If provided, saslUser and saslPassword must be a string'); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + $this->servers = $servers; |
|
| 148 | + return $this; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @return string |
|
| 153 | + */ |
|
| 154 | + public function getHost(): string |
|
| 155 | + { |
|
| 156 | + return $this->host; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param string $host |
|
| 161 | + * @return self |
|
| 162 | + */ |
|
| 163 | + public function setHost(string $host): self |
|
| 164 | + { |
|
| 165 | + $this->host = $host; |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return int |
|
| 171 | + */ |
|
| 172 | + public function getPort(): int |
|
| 173 | + { |
|
| 174 | + return $this->port; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @param int $port |
|
| 179 | + * @return Config |
|
| 180 | + */ |
|
| 181 | + public function setPort(int $port): self |
|
| 182 | + { |
|
| 183 | + $this->port = $port; |
|
| 184 | + return $this; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @return string |
|
| 189 | + * @since 8.0.2 |
|
| 190 | + */ |
|
| 191 | + public function getOptPrefix(): string |
|
| 192 | + { |
|
| 193 | + return $this->optPrefix; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @param string $optPrefix |
|
| 198 | + * @return Config |
|
| 199 | + * @since 8.0.2 |
|
| 200 | + */ |
|
| 201 | + public function setOptPrefix(string $optPrefix): Config |
|
| 202 | + { |
|
| 203 | + $this->optPrefix = trim($optPrefix); |
|
| 204 | + return $this; |
|
| 205 | + } |
|
| 206 | 206 | } |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(MemcachedDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof MemcachedDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(MemcachedDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof MemcachedDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -37,179 +37,179 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 39 | 39 | { |
| 40 | - use DriverBaseTrait { |
|
| 41 | - __construct as protected __parentConstruct; |
|
| 42 | - } |
|
| 43 | - use MemcacheDriverCollisionDetectorTrait; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Driver constructor. |
|
| 47 | - * @param ConfigurationOption $config |
|
| 48 | - * @param string $instanceId |
|
| 49 | - * @throws PhpfastcacheDriverException |
|
| 50 | - */ |
|
| 51 | - public function __construct(ConfigurationOption $config, string $instanceId) |
|
| 52 | - { |
|
| 53 | - self::checkCollision('Memcached'); |
|
| 54 | - $this->__parentConstruct($config, $instanceId); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @return bool |
|
| 59 | - */ |
|
| 60 | - public function driverCheck(): bool |
|
| 61 | - { |
|
| 62 | - return class_exists('Memcached'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return DriverStatistic |
|
| 67 | - */ |
|
| 68 | - public function getStats(): DriverStatistic |
|
| 69 | - { |
|
| 70 | - $stats = current($this->instance->getStats()); |
|
| 71 | - $stats['uptime'] = $stats['uptime'] ?? 0; |
|
| 72 | - $stats['bytes'] = $stats['bytes'] ?? 0; |
|
| 73 | - $stats['version'] = $stats['version'] ?? $this->instance->getVersion(); |
|
| 74 | - |
|
| 75 | - $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
| 76 | - |
|
| 77 | - return (new DriverStatistic()) |
|
| 78 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 79 | - ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
|
| 80 | - ->setRawData($stats) |
|
| 81 | - ->setSize((int)$stats['bytes']); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @return bool |
|
| 86 | - */ |
|
| 87 | - protected function driverConnect(): bool |
|
| 88 | - { |
|
| 89 | - $this->instance = new MemcachedSoftware(); |
|
| 90 | - $optPrefix = $this->getConfig()->getOptPrefix(); |
|
| 91 | - $this->instance->setOption(MemcachedSoftware::OPT_BINARY_PROTOCOL, true); |
|
| 92 | - |
|
| 93 | - if ($optPrefix) { |
|
| 94 | - $this->instance->setOption(MemcachedSoftware::OPT_PREFIX_KEY, $optPrefix); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $servers = $this->getConfig()->getServers(); |
|
| 98 | - |
|
| 99 | - if (count($this->getConfig()->getServers()) < 1) { |
|
| 100 | - $this->getConfig()->setServers( |
|
| 101 | - [ |
|
| 102 | - [ |
|
| 103 | - 'host' => $this->getConfig()->getHost(), |
|
| 104 | - 'path' => $this->getConfig()->getPath(), |
|
| 105 | - 'port' => $this->getConfig()->getPort(), |
|
| 106 | - 'saslUser' => $this->getConfig()->getSaslUser() ?: null, |
|
| 107 | - 'saslPassword' => $this->getConfig()->getSaslPassword() ?: null, |
|
| 108 | - ], |
|
| 109 | - ] |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - foreach ($this->getConfig()->getServers() as $server) { |
|
| 114 | - try { |
|
| 115 | - /** |
|
| 116 | - * If path is provided we consider it as an UNIX Socket |
|
| 117 | - */ |
|
| 118 | - if (!empty($server['path']) && !$this->instance->addServer($server['path'], 0)) { |
|
| 119 | - $this->fallback = true; |
|
| 120 | - } else { |
|
| 121 | - if (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) { |
|
| 122 | - $this->fallback = true; |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
|
| 127 | - $this->instance->setSaslAuthData($server['saslUser'], $server['saslPassword']); |
|
| 128 | - } |
|
| 129 | - } catch (Exception $e) { |
|
| 130 | - $this->fallback = true; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Since Memcached does not throw |
|
| 136 | - * any error if not connected ... |
|
| 137 | - */ |
|
| 138 | - $version = $this->instance->getVersion(); |
|
| 139 | - if (!$version || $this->instance->getResultCode() !== MemcachedSoftware::RES_SUCCESS) { |
|
| 140 | - throw new PhpfastcacheDriverException('Memcached seems to not be connected'); |
|
| 141 | - } |
|
| 142 | - return true; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @param CacheItemInterface $item |
|
| 147 | - * @return null|array |
|
| 148 | - */ |
|
| 149 | - protected function driverRead(CacheItemInterface $item) |
|
| 150 | - { |
|
| 151 | - $val = $this->instance->get($item->getKey()); |
|
| 152 | - |
|
| 153 | - if ($val === false) { |
|
| 154 | - return null; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $val; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @param CacheItemInterface $item |
|
| 162 | - * @return bool |
|
| 163 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 164 | - */ |
|
| 165 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 166 | - { |
|
| 167 | - /** |
|
| 168 | - * Check for Cross-Driver type confusion |
|
| 169 | - */ |
|
| 170 | - if ($item instanceof Item) { |
|
| 171 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 172 | - |
|
| 173 | - // Memcache will only allow a expiration timer less than 2592000 seconds, |
|
| 174 | - // otherwise, it will assume you're giving it a UNIX timestamp. |
|
| 175 | - if ($ttl > 2592000) { |
|
| 176 | - $ttl = time() + $ttl; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $ttl); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @param CacheItemInterface $item |
|
| 187 | - * @return bool |
|
| 188 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 189 | - */ |
|
| 190 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 191 | - { |
|
| 192 | - /** |
|
| 193 | - * Check for Cross-Driver type confusion |
|
| 194 | - */ |
|
| 195 | - if ($item instanceof Item) { |
|
| 196 | - return $this->instance->delete($item->getKey()); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /******************** |
|
| 40 | + use DriverBaseTrait { |
|
| 41 | + __construct as protected __parentConstruct; |
|
| 42 | + } |
|
| 43 | + use MemcacheDriverCollisionDetectorTrait; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Driver constructor. |
|
| 47 | + * @param ConfigurationOption $config |
|
| 48 | + * @param string $instanceId |
|
| 49 | + * @throws PhpfastcacheDriverException |
|
| 50 | + */ |
|
| 51 | + public function __construct(ConfigurationOption $config, string $instanceId) |
|
| 52 | + { |
|
| 53 | + self::checkCollision('Memcached'); |
|
| 54 | + $this->__parentConstruct($config, $instanceId); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @return bool |
|
| 59 | + */ |
|
| 60 | + public function driverCheck(): bool |
|
| 61 | + { |
|
| 62 | + return class_exists('Memcached'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return DriverStatistic |
|
| 67 | + */ |
|
| 68 | + public function getStats(): DriverStatistic |
|
| 69 | + { |
|
| 70 | + $stats = current($this->instance->getStats()); |
|
| 71 | + $stats['uptime'] = $stats['uptime'] ?? 0; |
|
| 72 | + $stats['bytes'] = $stats['bytes'] ?? 0; |
|
| 73 | + $stats['version'] = $stats['version'] ?? $this->instance->getVersion(); |
|
| 74 | + |
|
| 75 | + $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
| 76 | + |
|
| 77 | + return (new DriverStatistic()) |
|
| 78 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 79 | + ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
|
| 80 | + ->setRawData($stats) |
|
| 81 | + ->setSize((int)$stats['bytes']); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @return bool |
|
| 86 | + */ |
|
| 87 | + protected function driverConnect(): bool |
|
| 88 | + { |
|
| 89 | + $this->instance = new MemcachedSoftware(); |
|
| 90 | + $optPrefix = $this->getConfig()->getOptPrefix(); |
|
| 91 | + $this->instance->setOption(MemcachedSoftware::OPT_BINARY_PROTOCOL, true); |
|
| 92 | + |
|
| 93 | + if ($optPrefix) { |
|
| 94 | + $this->instance->setOption(MemcachedSoftware::OPT_PREFIX_KEY, $optPrefix); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $servers = $this->getConfig()->getServers(); |
|
| 98 | + |
|
| 99 | + if (count($this->getConfig()->getServers()) < 1) { |
|
| 100 | + $this->getConfig()->setServers( |
|
| 101 | + [ |
|
| 102 | + [ |
|
| 103 | + 'host' => $this->getConfig()->getHost(), |
|
| 104 | + 'path' => $this->getConfig()->getPath(), |
|
| 105 | + 'port' => $this->getConfig()->getPort(), |
|
| 106 | + 'saslUser' => $this->getConfig()->getSaslUser() ?: null, |
|
| 107 | + 'saslPassword' => $this->getConfig()->getSaslPassword() ?: null, |
|
| 108 | + ], |
|
| 109 | + ] |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + foreach ($this->getConfig()->getServers() as $server) { |
|
| 114 | + try { |
|
| 115 | + /** |
|
| 116 | + * If path is provided we consider it as an UNIX Socket |
|
| 117 | + */ |
|
| 118 | + if (!empty($server['path']) && !$this->instance->addServer($server['path'], 0)) { |
|
| 119 | + $this->fallback = true; |
|
| 120 | + } else { |
|
| 121 | + if (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) { |
|
| 122 | + $this->fallback = true; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
|
| 127 | + $this->instance->setSaslAuthData($server['saslUser'], $server['saslPassword']); |
|
| 128 | + } |
|
| 129 | + } catch (Exception $e) { |
|
| 130 | + $this->fallback = true; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Since Memcached does not throw |
|
| 136 | + * any error if not connected ... |
|
| 137 | + */ |
|
| 138 | + $version = $this->instance->getVersion(); |
|
| 139 | + if (!$version || $this->instance->getResultCode() !== MemcachedSoftware::RES_SUCCESS) { |
|
| 140 | + throw new PhpfastcacheDriverException('Memcached seems to not be connected'); |
|
| 141 | + } |
|
| 142 | + return true; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @param CacheItemInterface $item |
|
| 147 | + * @return null|array |
|
| 148 | + */ |
|
| 149 | + protected function driverRead(CacheItemInterface $item) |
|
| 150 | + { |
|
| 151 | + $val = $this->instance->get($item->getKey()); |
|
| 152 | + |
|
| 153 | + if ($val === false) { |
|
| 154 | + return null; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $val; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @param CacheItemInterface $item |
|
| 162 | + * @return bool |
|
| 163 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 164 | + */ |
|
| 165 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 166 | + { |
|
| 167 | + /** |
|
| 168 | + * Check for Cross-Driver type confusion |
|
| 169 | + */ |
|
| 170 | + if ($item instanceof Item) { |
|
| 171 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 172 | + |
|
| 173 | + // Memcache will only allow a expiration timer less than 2592000 seconds, |
|
| 174 | + // otherwise, it will assume you're giving it a UNIX timestamp. |
|
| 175 | + if ($ttl > 2592000) { |
|
| 176 | + $ttl = time() + $ttl; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $ttl); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @param CacheItemInterface $item |
|
| 187 | + * @return bool |
|
| 188 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 189 | + */ |
|
| 190 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 191 | + { |
|
| 192 | + /** |
|
| 193 | + * Check for Cross-Driver type confusion |
|
| 194 | + */ |
|
| 195 | + if ($item instanceof Item) { |
|
| 196 | + return $this->instance->delete($item->getKey()); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /******************** |
|
| 203 | 203 | * |
| 204 | 204 | * PSR-6 Extended Methods |
| 205 | 205 | * |
| 206 | 206 | *******************/ |
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * @return bool |
|
| 210 | - */ |
|
| 211 | - protected function driverClear(): bool |
|
| 212 | - { |
|
| 213 | - return $this->instance->flush(); |
|
| 214 | - } |
|
| 208 | + /** |
|
| 209 | + * @return bool |
|
| 210 | + */ |
|
| 211 | + protected function driverClear(): bool |
|
| 212 | + { |
|
| 213 | + return $this->instance->flush(); |
|
| 214 | + } |
|
| 215 | 215 | } |