@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | */ |
| 165 | 165 | if ($item instanceof Item) { |
| 166 | 166 | try { |
| 167 | - return (bool)$this->getBucket()->upsert( |
|
| 167 | + return (bool) $this->getBucket()->upsert( |
|
| 168 | 168 | $item->getEncodedKey(), |
| 169 | 169 | $this->encodeDocument($this->driverPreWrap($item)), |
| 170 | 170 | ['expiry' => $item->getTtl()] |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | */ |
| 190 | 190 | if ($item instanceof Item) { |
| 191 | 191 | try { |
| 192 | - return (bool)$this->getBucket()->remove($item->getEncodedKey()); |
|
| 192 | + return (bool) $this->getBucket()->remove($item->getEncodedKey()); |
|
| 193 | 193 | } catch (Exception $e) { |
| 194 | 194 | return $e->getCode() === COUCHBASE_KEY_ENOENT; |
| 195 | 195 | } |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
| 208 | 208 | $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 209 | 209 | |
| 210 | - if($this->getConfig()->isItemDetailedDate()){ |
|
| 210 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 211 | 211 | $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 212 | 212 | $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 213 | 213 | } |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
| 228 | 228 | ); |
| 229 | 229 | |
| 230 | - if($this->getConfig()->isItemDetailedDate()){ |
|
| 230 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 231 | 231 | $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
| 232 | 232 | \DateTime::ATOM, |
| 233 | 233 | $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
@@ -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 | } |
@@ -57,8 +57,7 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | protected $currentBucket = ''; |
| 59 | 59 | |
| 60 | - public function __construct(ConfigurationOption $config, $instanceId) |
|
| 61 | - { |
|
| 60 | + public function __construct(ConfigurationOption $config, $instanceId) { |
|
| 62 | 61 | // @todo Deprecation to enable in v8.1 |
| 63 | 62 | // \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 | 63 | $this->__baseConstruct($config, $instanceId); |
@@ -123,8 +122,7 @@ discard block |
||
| 123 | 122 | /** |
| 124 | 123 | * @param CouchbaseBucket $CouchbaseBucket |
| 125 | 124 | */ |
| 126 | - protected function setBucket(CouchbaseBucket $CouchbaseBucket) |
|
| 127 | - { |
|
| 125 | + protected function setBucket(CouchbaseBucket $CouchbaseBucket) { |
|
| 128 | 126 | $this->bucketInstance = $CouchbaseBucket; |
| 129 | 127 | } |
| 130 | 128 | |
@@ -132,14 +130,14 @@ discard block |
||
| 132 | 130 | * @param CacheItemInterface $item |
| 133 | 131 | * @return null|array |
| 134 | 132 | */ |
| 135 | - protected function driverRead(CacheItemInterface $item) |
|
| 136 | - { |
|
| 133 | + protected function driverRead(CacheItemInterface $item) { |
|
| 137 | 134 | try { |
| 138 | 135 | /** |
| 139 | 136 | * CouchbaseBucket::get() returns a CouchbaseMetaDoc object |
| 140 | 137 | */ |
| 141 | 138 | return $this->decodeDocument((array) $this->getBucket()->get($item->getEncodedKey())->value); |
| 142 | - } catch (CouchbaseException $e) { |
|
| 139 | + } |
|
| 140 | + catch (CouchbaseException $e) { |
|
| 143 | 141 | return null; |
| 144 | 142 | } |
| 145 | 143 | } |
@@ -169,7 +167,8 @@ discard block |
||
| 169 | 167 | $this->encodeDocument($this->driverPreWrap($item)), |
| 170 | 168 | ['expiry' => $item->getTtl()] |
| 171 | 169 | ); |
| 172 | - } catch (CouchbaseException $e) { |
|
| 170 | + } |
|
| 171 | + catch (CouchbaseException $e) { |
|
| 173 | 172 | return false; |
| 174 | 173 | } |
| 175 | 174 | } |
@@ -190,7 +189,8 @@ discard block |
||
| 190 | 189 | if ($item instanceof Item) { |
| 191 | 190 | try { |
| 192 | 191 | return (bool)$this->getBucket()->remove($item->getEncodedKey()); |
| 193 | - } catch (Exception $e) { |
|
| 192 | + } |
|
| 193 | + catch (Exception $e) { |
|
| 194 | 194 | return $e->getCode() === COUCHBASE_KEY_ENOENT; |
| 195 | 195 | } |
| 196 | 196 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * Check for Cross-Driver type confusion |
| 82 | 82 | */ |
| 83 | 83 | if ($item instanceof Item) { |
| 84 | - return (bool)$this->instance->set($item->getKey(), $this->encode($this->driverPreWrap($item))); |
|
| 84 | + return (bool) $this->instance->set($item->getKey(), $this->encode($this->driverPreWrap($item))); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | $this->instance->close(); |
| 114 | 114 | $this->instance = null; |
| 115 | 115 | } |
| 116 | - $result = (bool)LeveldbClient::destroy($this->getLeveldbFile()); |
|
| 116 | + $result = (bool) LeveldbClient::destroy($this->getLeveldbFile()); |
|
| 117 | 117 | $this->driverConnect(); |
| 118 | 118 | |
| 119 | 119 | return $result; |
@@ -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 | } |
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | /** |
| 49 | 49 | * Close connection on destruct |
| 50 | 50 | */ |
| 51 | - public function __destruct() |
|
| 52 | - { |
|
| 51 | + public function __destruct() { |
|
| 53 | 52 | if ($this->instance instanceof LeveldbClient) { |
| 54 | 53 | $this->instance->close(); |
| 55 | 54 | $this->instance = null; |
@@ -60,8 +59,7 @@ discard block |
||
| 60 | 59 | * @param CacheItemInterface $item |
| 61 | 60 | * @return null|array |
| 62 | 61 | */ |
| 63 | - protected function driverRead(CacheItemInterface $item) |
|
| 64 | - { |
|
| 62 | + protected function driverRead(CacheItemInterface $item) { |
|
| 65 | 63 | $val = $this->instance->get($item->getKey()); |
| 66 | 64 | if (!$val) { |
| 67 | 65 | return null; |
@@ -132,7 +132,7 @@ |
||
| 132 | 132 | throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined'); |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) { |
|
| 135 | + if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port']) || $server['port'] < 1)) { |
|
| 136 | 136 | throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
| 137 | 137 | } |
| 138 | 138 | |
@@ -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 | } |
@@ -78,7 +78,7 @@ |
||
| 78 | 78 | ->setData(implode(', ', array_keys($this->itemInstances))) |
| 79 | 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 | 80 | ->setRawData($stats) |
| 81 | - ->setSize((int)$stats['bytes']); |
|
| 81 | + ->setSize((int) $stats['bytes']); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -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 | } |
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | * @param string $instanceId |
| 49 | 49 | * @throws PhpfastcacheDriverException |
| 50 | 50 | */ |
| 51 | - public function __construct(ConfigurationOption $config, string $instanceId) |
|
| 52 | - { |
|
| 51 | + public function __construct(ConfigurationOption $config, string $instanceId) { |
|
| 53 | 52 | self::checkCollision('Memcached'); |
| 54 | 53 | $this->__parentConstruct($config, $instanceId); |
| 55 | 54 | } |
@@ -117,7 +116,8 @@ discard block |
||
| 117 | 116 | */ |
| 118 | 117 | if (!empty($server['path']) && !$this->instance->addServer($server['path'], 0)) { |
| 119 | 118 | $this->fallback = true; |
| 120 | - } else { |
|
| 119 | + } |
|
| 120 | + else { |
|
| 121 | 121 | if (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) { |
| 122 | 122 | $this->fallback = true; |
| 123 | 123 | } |
@@ -126,7 +126,8 @@ discard block |
||
| 126 | 126 | if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
| 127 | 127 | $this->instance->setSaslAuthData($server['saslUser'], $server['saslPassword']); |
| 128 | 128 | } |
| 129 | - } catch (Exception $e) { |
|
| 129 | + } |
|
| 130 | + catch (Exception $e) { |
|
| 130 | 131 | $this->fallback = true; |
| 131 | 132 | } |
| 132 | 133 | } |
@@ -146,8 +147,7 @@ discard block |
||
| 146 | 147 | * @param CacheItemInterface $item |
| 147 | 148 | * @return null|array |
| 148 | 149 | */ |
| 149 | - protected function driverRead(CacheItemInterface $item) |
|
| 150 | - { |
|
| 150 | + protected function driverRead(CacheItemInterface $item) { |
|
| 151 | 151 | $val = $this->instance->get($item->getKey()); |
| 152 | 152 | |
| 153 | 153 | if ($val === false) { |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | return (new DriverStatistic()) |
| 56 | 56 | ->setData(implode(', ', array_keys($this->itemInstances))) |
| 57 | 57 | ->setRawData($info) |
| 58 | - ->setSize((int)$info['used_memory']) |
|
| 58 | + ->setSize((int) $info['used_memory']) |
|
| 59 | 59 | ->setInfo( |
| 60 | 60 | sprintf( |
| 61 | 61 | "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * Check for Cross-Driver type confusion |
| 170 | 170 | */ |
| 171 | 171 | if ($item instanceof Item) { |
| 172 | - return (bool)$this->instance->del($item->getKey()); |
|
| 172 | + return (bool) $this->instance->del($item->getKey()); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
@@ -33,159 +33,159 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 35 | 35 | { |
| 36 | - use DriverBaseTrait; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @return bool |
|
| 40 | - */ |
|
| 41 | - public function driverCheck(): bool |
|
| 42 | - { |
|
| 43 | - return extension_loaded('Redis'); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return DriverStatistic |
|
| 48 | - */ |
|
| 49 | - public function getStats(): DriverStatistic |
|
| 50 | - { |
|
| 51 | - // used_memory |
|
| 52 | - $info = $this->instance->info(); |
|
| 53 | - $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
| 54 | - |
|
| 55 | - return (new DriverStatistic()) |
|
| 56 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 57 | - ->setRawData($info) |
|
| 58 | - ->setSize((int)$info['used_memory']) |
|
| 59 | - ->setInfo( |
|
| 60 | - sprintf( |
|
| 61 | - "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 62 | - $info['redis_version'], |
|
| 63 | - $date->format(DATE_RFC2822) |
|
| 64 | - ) |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return bool |
|
| 70 | - * @throws PhpfastcacheLogicException |
|
| 71 | - */ |
|
| 72 | - protected function driverConnect(): bool |
|
| 73 | - { |
|
| 74 | - if ($this->instance instanceof RedisClient) { |
|
| 75 | - throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * In case of an user-provided |
|
| 80 | - * Redis client just return here |
|
| 81 | - */ |
|
| 82 | - if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
| 83 | - /** |
|
| 84 | - * Unlike Predis, we can't test if we're are connected |
|
| 85 | - * or not, so let's just assume that we are |
|
| 86 | - */ |
|
| 87 | - $this->instance = $this->getConfig()->getRedisClient(); |
|
| 88 | - return true; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $this->instance = $this->instance ?: new RedisClient(); |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * If path is provided we consider it as an UNIX Socket |
|
| 95 | - */ |
|
| 96 | - if ($this->getConfig()->getPath()) { |
|
| 97 | - $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
| 98 | - } else { |
|
| 99 | - $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if (!$isConnected && $this->getConfig()->getPath()) { |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if ($this->getConfig()->getOptPrefix()) { |
|
| 107 | - $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if ($this->getConfig()->getDatabase() !== null) { |
|
| 115 | - $this->instance->select($this->getConfig()->getDatabase()); |
|
| 116 | - } |
|
| 117 | - return true; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param CacheItemInterface $item |
|
| 122 | - * @return null|array |
|
| 123 | - */ |
|
| 124 | - protected function driverRead(CacheItemInterface $item) |
|
| 125 | - { |
|
| 126 | - $val = $this->instance->get($item->getKey()); |
|
| 127 | - if ($val == false) { |
|
| 128 | - return null; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return $this->decode($val); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param CacheItemInterface $item |
|
| 136 | - * @return mixed |
|
| 137 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 138 | - */ |
|
| 139 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 140 | - { |
|
| 141 | - /** |
|
| 142 | - * Check for Cross-Driver type confusion |
|
| 143 | - */ |
|
| 144 | - if ($item instanceof Item) { |
|
| 145 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @see https://redis.io/commands/setex |
|
| 149 | - * @see https://redis.io/commands/expire |
|
| 150 | - */ |
|
| 151 | - if ($ttl <= 0) { |
|
| 152 | - return $this->instance->expire($item->getKey(), 0); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param CacheItemInterface $item |
|
| 163 | - * @return bool |
|
| 164 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 165 | - */ |
|
| 166 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 167 | - { |
|
| 168 | - /** |
|
| 169 | - * Check for Cross-Driver type confusion |
|
| 170 | - */ |
|
| 171 | - if ($item instanceof Item) { |
|
| 172 | - return (bool)$this->instance->del($item->getKey()); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /******************** |
|
| 36 | + use DriverBaseTrait; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @return bool |
|
| 40 | + */ |
|
| 41 | + public function driverCheck(): bool |
|
| 42 | + { |
|
| 43 | + return extension_loaded('Redis'); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return DriverStatistic |
|
| 48 | + */ |
|
| 49 | + public function getStats(): DriverStatistic |
|
| 50 | + { |
|
| 51 | + // used_memory |
|
| 52 | + $info = $this->instance->info(); |
|
| 53 | + $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
| 54 | + |
|
| 55 | + return (new DriverStatistic()) |
|
| 56 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 57 | + ->setRawData($info) |
|
| 58 | + ->setSize((int)$info['used_memory']) |
|
| 59 | + ->setInfo( |
|
| 60 | + sprintf( |
|
| 61 | + "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 62 | + $info['redis_version'], |
|
| 63 | + $date->format(DATE_RFC2822) |
|
| 64 | + ) |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return bool |
|
| 70 | + * @throws PhpfastcacheLogicException |
|
| 71 | + */ |
|
| 72 | + protected function driverConnect(): bool |
|
| 73 | + { |
|
| 74 | + if ($this->instance instanceof RedisClient) { |
|
| 75 | + throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * In case of an user-provided |
|
| 80 | + * Redis client just return here |
|
| 81 | + */ |
|
| 82 | + if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
| 83 | + /** |
|
| 84 | + * Unlike Predis, we can't test if we're are connected |
|
| 85 | + * or not, so let's just assume that we are |
|
| 86 | + */ |
|
| 87 | + $this->instance = $this->getConfig()->getRedisClient(); |
|
| 88 | + return true; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $this->instance = $this->instance ?: new RedisClient(); |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * If path is provided we consider it as an UNIX Socket |
|
| 95 | + */ |
|
| 96 | + if ($this->getConfig()->getPath()) { |
|
| 97 | + $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
| 98 | + } else { |
|
| 99 | + $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if (!$isConnected && $this->getConfig()->getPath()) { |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if ($this->getConfig()->getOptPrefix()) { |
|
| 107 | + $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if ($this->getConfig()->getDatabase() !== null) { |
|
| 115 | + $this->instance->select($this->getConfig()->getDatabase()); |
|
| 116 | + } |
|
| 117 | + return true; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param CacheItemInterface $item |
|
| 122 | + * @return null|array |
|
| 123 | + */ |
|
| 124 | + protected function driverRead(CacheItemInterface $item) |
|
| 125 | + { |
|
| 126 | + $val = $this->instance->get($item->getKey()); |
|
| 127 | + if ($val == false) { |
|
| 128 | + return null; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return $this->decode($val); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param CacheItemInterface $item |
|
| 136 | + * @return mixed |
|
| 137 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 138 | + */ |
|
| 139 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 140 | + { |
|
| 141 | + /** |
|
| 142 | + * Check for Cross-Driver type confusion |
|
| 143 | + */ |
|
| 144 | + if ($item instanceof Item) { |
|
| 145 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @see https://redis.io/commands/setex |
|
| 149 | + * @see https://redis.io/commands/expire |
|
| 150 | + */ |
|
| 151 | + if ($ttl <= 0) { |
|
| 152 | + return $this->instance->expire($item->getKey(), 0); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param CacheItemInterface $item |
|
| 163 | + * @return bool |
|
| 164 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 165 | + */ |
|
| 166 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 167 | + { |
|
| 168 | + /** |
|
| 169 | + * Check for Cross-Driver type confusion |
|
| 170 | + */ |
|
| 171 | + if ($item instanceof Item) { |
|
| 172 | + return (bool)$this->instance->del($item->getKey()); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /******************** |
|
| 179 | 179 | * |
| 180 | 180 | * PSR-6 Extended Methods |
| 181 | 181 | * |
| 182 | 182 | *******************/ |
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * @return bool |
|
| 186 | - */ |
|
| 187 | - protected function driverClear(): bool |
|
| 188 | - { |
|
| 189 | - return $this->instance->flushDB(); |
|
| 190 | - } |
|
| 184 | + /** |
|
| 185 | + * @return bool |
|
| 186 | + */ |
|
| 187 | + protected function driverClear(): bool |
|
| 188 | + { |
|
| 189 | + return $this->instance->flushDB(); |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -95,7 +95,8 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | if ($this->getConfig()->getPath()) { |
| 97 | 97 | $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
| 98 | - } else { |
|
| 98 | + } |
|
| 99 | + else { |
|
| 99 | 100 | $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
| 100 | 101 | } |
| 101 | 102 | |
@@ -121,8 +122,7 @@ discard block |
||
| 121 | 122 | * @param CacheItemInterface $item |
| 122 | 123 | * @return null|array |
| 123 | 124 | */ |
| 124 | - protected function driverRead(CacheItemInterface $item) |
|
| 125 | - { |
|
| 125 | + protected function driverRead(CacheItemInterface $item) { |
|
| 126 | 126 | $val = $this->instance->get($item->getKey()); |
| 127 | 127 | if ($val == false) { |
| 128 | 128 | return null; |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | return (new DriverStatistic()) |
| 76 | 76 | ->setData(implode(', ', array_keys($this->itemInstances))) |
| 77 | 77 | ->setRawData($info) |
| 78 | - ->setSize((int)$size) |
|
| 78 | + ->setSize((int) $size) |
|
| 79 | 79 | ->setInfo( |
| 80 | 80 | sprintf( |
| 81 | 81 | "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @see https://redis.io/commands/expire |
| 173 | 173 | */ |
| 174 | 174 | if ($ttl <= 0) { |
| 175 | - return (bool)$this->instance->expire($item->getKey(), 0); |
|
| 175 | + return (bool) $this->instance->expire($item->getKey(), 0); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | * Check for Cross-Driver type confusion |
| 199 | 199 | */ |
| 200 | 200 | if ($item instanceof Item) { |
| 201 | - return (bool)$this->instance->del([$item->getKey()]); |
|
| 201 | + return (bool) $this->instance->del([$item->getKey()]); |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
@@ -35,180 +35,180 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 37 | 37 | { |
| 38 | - use DriverBaseTrait; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - public function driverCheck(): bool |
|
| 44 | - { |
|
| 45 | - if (extension_loaded('Redis')) { |
|
| 46 | - trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - return class_exists('Predis\Client'); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return string |
|
| 54 | - */ |
|
| 55 | - public function getHelp(): string |
|
| 56 | - { |
|
| 57 | - return <<<HELP |
|
| 38 | + use DriverBaseTrait; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + public function driverCheck(): bool |
|
| 44 | + { |
|
| 45 | + if (extension_loaded('Redis')) { |
|
| 46 | + trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + return class_exists('Predis\Client'); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return string |
|
| 54 | + */ |
|
| 55 | + public function getHelp(): string |
|
| 56 | + { |
|
| 57 | + return <<<HELP |
|
| 58 | 58 | <p> |
| 59 | 59 | To install the Predis library via Composer: |
| 60 | 60 | <code>composer require "predis/predis" "~1.1.0"</code> |
| 61 | 61 | </p> |
| 62 | 62 | HELP; |
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return DriverStatistic |
|
| 67 | - */ |
|
| 68 | - public function getStats(): DriverStatistic |
|
| 69 | - { |
|
| 70 | - $info = $this->instance->info(); |
|
| 71 | - $size = (isset($info['Memory']['used_memory']) ? $info['Memory']['used_memory'] : 0); |
|
| 72 | - $version = (isset($info['Server']['redis_version']) ? $info['Server']['redis_version'] : 0); |
|
| 73 | - $date = (isset($info['Server']['uptime_in_seconds']) ? (new DateTime())->setTimestamp(time() - $info['Server']['uptime_in_seconds']) : 'unknown date'); |
|
| 74 | - |
|
| 75 | - return (new DriverStatistic()) |
|
| 76 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 77 | - ->setRawData($info) |
|
| 78 | - ->setSize((int)$size) |
|
| 79 | - ->setInfo( |
|
| 80 | - sprintf( |
|
| 81 | - "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 82 | - $version, |
|
| 83 | - $date->format(DATE_RFC2822) |
|
| 84 | - ) |
|
| 85 | - ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return bool |
|
| 90 | - * @throws PhpfastcacheDriverException |
|
| 91 | - * @throws PhpfastcacheLogicException |
|
| 92 | - */ |
|
| 93 | - protected function driverConnect(): bool |
|
| 94 | - { |
|
| 95 | - if ($this->instance instanceof PredisClient) { |
|
| 96 | - throw new PhpfastcacheLogicException('Already connected to Predis server'); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * In case of an user-provided |
|
| 101 | - * Predis client just return here |
|
| 102 | - */ |
|
| 103 | - if ($this->getConfig()->getPredisClient() instanceof PredisClient) { |
|
| 104 | - $this->instance = $this->getConfig()->getPredisClient(); |
|
| 105 | - if (!$this->instance->isConnected()) { |
|
| 106 | - $this->instance->connect(); |
|
| 107 | - } |
|
| 108 | - return true; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $options = []; |
|
| 112 | - |
|
| 113 | - if ($this->getConfig()->getOptPrefix()) { |
|
| 114 | - $options['prefix'] = $this->getConfig()->getOptPrefix(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if (!empty($this->getConfig()->getPath())) { |
|
| 118 | - $this->instance = new PredisClient( |
|
| 119 | - [ |
|
| 120 | - 'scheme' => $this->getConfig()->getScheme(), |
|
| 121 | - 'persistent' => $this->getConfig()->isPersistent(), |
|
| 122 | - 'timeout' => $this->getConfig()->getTimeout(), |
|
| 123 | - 'path' => $this->getConfig()->getPath(), |
|
| 124 | - ], $options |
|
| 125 | - ); |
|
| 126 | - } else { |
|
| 127 | - $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - try { |
|
| 131 | - $this->instance->connect(); |
|
| 132 | - } catch (PredisConnectionException $e) { |
|
| 133 | - throw new PhpfastcacheDriverException( |
|
| 134 | - 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
|
| 135 | - 0, |
|
| 136 | - $e |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return true; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param CacheItemInterface $item |
|
| 145 | - * @return null|array |
|
| 146 | - */ |
|
| 147 | - protected function driverRead(CacheItemInterface $item) |
|
| 148 | - { |
|
| 149 | - $val = $this->instance->get($item->getKey()); |
|
| 150 | - if ($val == false) { |
|
| 151 | - return null; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - return $this->decode($val); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @param CacheItemInterface $item |
|
| 159 | - * @return mixed |
|
| 160 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 161 | - */ |
|
| 162 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 163 | - { |
|
| 164 | - /** |
|
| 165 | - * Check for Cross-Driver type confusion |
|
| 166 | - */ |
|
| 167 | - if ($item instanceof Item) { |
|
| 168 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @see https://redis.io/commands/setex |
|
| 172 | - * @see https://redis.io/commands/expire |
|
| 173 | - */ |
|
| 174 | - if ($ttl <= 0) { |
|
| 175 | - return (bool)$this->instance->expire($item->getKey(), 0); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /******************** |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return DriverStatistic |
|
| 67 | + */ |
|
| 68 | + public function getStats(): DriverStatistic |
|
| 69 | + { |
|
| 70 | + $info = $this->instance->info(); |
|
| 71 | + $size = (isset($info['Memory']['used_memory']) ? $info['Memory']['used_memory'] : 0); |
|
| 72 | + $version = (isset($info['Server']['redis_version']) ? $info['Server']['redis_version'] : 0); |
|
| 73 | + $date = (isset($info['Server']['uptime_in_seconds']) ? (new DateTime())->setTimestamp(time() - $info['Server']['uptime_in_seconds']) : 'unknown date'); |
|
| 74 | + |
|
| 75 | + return (new DriverStatistic()) |
|
| 76 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 77 | + ->setRawData($info) |
|
| 78 | + ->setSize((int)$size) |
|
| 79 | + ->setInfo( |
|
| 80 | + sprintf( |
|
| 81 | + "The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
| 82 | + $version, |
|
| 83 | + $date->format(DATE_RFC2822) |
|
| 84 | + ) |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return bool |
|
| 90 | + * @throws PhpfastcacheDriverException |
|
| 91 | + * @throws PhpfastcacheLogicException |
|
| 92 | + */ |
|
| 93 | + protected function driverConnect(): bool |
|
| 94 | + { |
|
| 95 | + if ($this->instance instanceof PredisClient) { |
|
| 96 | + throw new PhpfastcacheLogicException('Already connected to Predis server'); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * In case of an user-provided |
|
| 101 | + * Predis client just return here |
|
| 102 | + */ |
|
| 103 | + if ($this->getConfig()->getPredisClient() instanceof PredisClient) { |
|
| 104 | + $this->instance = $this->getConfig()->getPredisClient(); |
|
| 105 | + if (!$this->instance->isConnected()) { |
|
| 106 | + $this->instance->connect(); |
|
| 107 | + } |
|
| 108 | + return true; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $options = []; |
|
| 112 | + |
|
| 113 | + if ($this->getConfig()->getOptPrefix()) { |
|
| 114 | + $options['prefix'] = $this->getConfig()->getOptPrefix(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if (!empty($this->getConfig()->getPath())) { |
|
| 118 | + $this->instance = new PredisClient( |
|
| 119 | + [ |
|
| 120 | + 'scheme' => $this->getConfig()->getScheme(), |
|
| 121 | + 'persistent' => $this->getConfig()->isPersistent(), |
|
| 122 | + 'timeout' => $this->getConfig()->getTimeout(), |
|
| 123 | + 'path' => $this->getConfig()->getPath(), |
|
| 124 | + ], $options |
|
| 125 | + ); |
|
| 126 | + } else { |
|
| 127 | + $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + try { |
|
| 131 | + $this->instance->connect(); |
|
| 132 | + } catch (PredisConnectionException $e) { |
|
| 133 | + throw new PhpfastcacheDriverException( |
|
| 134 | + 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
|
| 135 | + 0, |
|
| 136 | + $e |
|
| 137 | + ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return true; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param CacheItemInterface $item |
|
| 145 | + * @return null|array |
|
| 146 | + */ |
|
| 147 | + protected function driverRead(CacheItemInterface $item) |
|
| 148 | + { |
|
| 149 | + $val = $this->instance->get($item->getKey()); |
|
| 150 | + if ($val == false) { |
|
| 151 | + return null; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + return $this->decode($val); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @param CacheItemInterface $item |
|
| 159 | + * @return mixed |
|
| 160 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 161 | + */ |
|
| 162 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 163 | + { |
|
| 164 | + /** |
|
| 165 | + * Check for Cross-Driver type confusion |
|
| 166 | + */ |
|
| 167 | + if ($item instanceof Item) { |
|
| 168 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @see https://redis.io/commands/setex |
|
| 172 | + * @see https://redis.io/commands/expire |
|
| 173 | + */ |
|
| 174 | + if ($ttl <= 0) { |
|
| 175 | + return (bool)$this->instance->expire($item->getKey(), 0); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /******************** |
|
| 185 | 185 | * |
| 186 | 186 | * PSR-6 Extended Methods |
| 187 | 187 | * |
| 188 | 188 | *******************/ |
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * @param CacheItemInterface $item |
|
| 192 | - * @return bool |
|
| 193 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 194 | - */ |
|
| 195 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 196 | - { |
|
| 197 | - /** |
|
| 198 | - * Check for Cross-Driver type confusion |
|
| 199 | - */ |
|
| 200 | - if ($item instanceof Item) { |
|
| 201 | - return (bool)$this->instance->del([$item->getKey()]); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @return bool |
|
| 209 | - */ |
|
| 210 | - protected function driverClear(): bool |
|
| 211 | - { |
|
| 212 | - return $this->instance->flushdb()->getPayload() === 'OK'; |
|
| 213 | - } |
|
| 190 | + /** |
|
| 191 | + * @param CacheItemInterface $item |
|
| 192 | + * @return bool |
|
| 193 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 194 | + */ |
|
| 195 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 196 | + { |
|
| 197 | + /** |
|
| 198 | + * Check for Cross-Driver type confusion |
|
| 199 | + */ |
|
| 200 | + if ($item instanceof Item) { |
|
| 201 | + return (bool)$this->instance->del([$item->getKey()]); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @return bool |
|
| 209 | + */ |
|
| 210 | + protected function driverClear(): bool |
|
| 211 | + { |
|
| 212 | + return $this->instance->flushdb()->getPayload() === 'OK'; |
|
| 213 | + } |
|
| 214 | 214 | } |
@@ -123,13 +123,15 @@ discard block |
||
| 123 | 123 | 'path' => $this->getConfig()->getPath(), |
| 124 | 124 | ], $options |
| 125 | 125 | ); |
| 126 | - } else { |
|
| 126 | + } |
|
| 127 | + else { |
|
| 127 | 128 | $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
| 128 | 129 | } |
| 129 | 130 | |
| 130 | 131 | try { |
| 131 | 132 | $this->instance->connect(); |
| 132 | - } catch (PredisConnectionException $e) { |
|
| 133 | + } |
|
| 134 | + catch (PredisConnectionException $e) { |
|
| 133 | 135 | throw new PhpfastcacheDriverException( |
| 134 | 136 | 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
| 135 | 137 | 0, |
@@ -144,8 +146,7 @@ discard block |
||
| 144 | 146 | * @param CacheItemInterface $item |
| 145 | 147 | * @return null|array |
| 146 | 148 | */ |
| 147 | - protected function driverRead(CacheItemInterface $item) |
|
| 148 | - { |
|
| 149 | + protected function driverRead(CacheItemInterface $item) { |
|
| 149 | 150 | $val = $this->instance->get($item->getKey()); |
| 150 | 151 | if ($val == false) { |
| 151 | 152 | return null; |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | ) |
| 96 | 96 | )->toArray()[0]; |
| 97 | 97 | |
| 98 | - $array_filter_recursive = static function ($array, callable $callback = null) use (&$array_filter_recursive) { |
|
| 98 | + $array_filter_recursive = static function($array, callable $callback = null) use (&$array_filter_recursive) { |
|
| 99 | 99 | $array = $callback($array); |
| 100 | 100 | |
| 101 | 101 | if (\is_object($array) || \is_array($array)) { |
@@ -107,12 +107,12 @@ discard block |
||
| 107 | 107 | return $array; |
| 108 | 108 | }; |
| 109 | 109 | |
| 110 | - $callback = static function ($item) { |
|
| 110 | + $callback = static function($item) { |
|
| 111 | 111 | /** |
| 112 | 112 | * Remove unserializable properties |
| 113 | 113 | */ |
| 114 | 114 | if ($item instanceof UTCDateTime) { |
| 115 | - return (string)$item; |
|
| 115 | + return (string) $item; |
|
| 116 | 116 | } |
| 117 | 117 | return $item; |
| 118 | 118 | }; |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | self::DRIVER_CDATE_WRAPPER_INDEX => new UTCDateTime($item->getCreationDate()), |
| 202 | 202 | ]; |
| 203 | 203 | } |
| 204 | - $result = (array)$this->getCollection()->updateOne( |
|
| 204 | + $result = (array) $this->getCollection()->updateOne( |
|
| 205 | 205 | ['_id' => $this->getMongoDbItemKey($item)], |
| 206 | 206 | [ |
| 207 | 207 | '$set' => $set, |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | $this->database->selectCollection($collectionName) |
| 282 | 282 | ->createIndex( |
| 283 | 283 | [self::DRIVER_EDATE_WRAPPER_INDEX => 1], |
| 284 | - ['expireAfterSeconds' => 0, 'name' => 'auto_expire_index'] |
|
| 284 | + ['expireAfterSeconds' => 0, 'name' => 'auto_expire_index'] |
|
| 285 | 285 | ); |
| 286 | 286 | } |
| 287 | 287 | |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | if (count($servers) > 0) { |
| 312 | 312 | $host = array_reduce( |
| 313 | 313 | $servers, |
| 314 | - static function ($carry, $data) { |
|
| 314 | + static function($carry, $data) { |
|
| 315 | 315 | $carry .= ($carry === '' ? '' : ',') . $data['host'] . ':' . $data['port']; |
| 316 | 316 | return $carry; |
| 317 | 317 | }, |
@@ -36,331 +36,331 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 38 | 38 | { |
| 39 | - public const MONGODB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
| 40 | - |
|
| 41 | - use DriverBaseTrait; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var Collection |
|
| 45 | - */ |
|
| 46 | - public $collection; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var Database |
|
| 50 | - */ |
|
| 51 | - public $database; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @return bool |
|
| 55 | - */ |
|
| 56 | - public function driverCheck(): bool |
|
| 57 | - { |
|
| 58 | - $mongoExtensionExists = class_exists(Manager::class); |
|
| 59 | - |
|
| 60 | - if (!$mongoExtensionExists && class_exists(MongoClient::class)) { |
|
| 61 | - trigger_error( |
|
| 62 | - 'This driver is used to support the pecl MongoDb extension with mongo-php-library. |
|
| 39 | + public const MONGODB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
| 40 | + |
|
| 41 | + use DriverBaseTrait; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var Collection |
|
| 45 | + */ |
|
| 46 | + public $collection; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var Database |
|
| 50 | + */ |
|
| 51 | + public $database; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @return bool |
|
| 55 | + */ |
|
| 56 | + public function driverCheck(): bool |
|
| 57 | + { |
|
| 58 | + $mongoExtensionExists = class_exists(Manager::class); |
|
| 59 | + |
|
| 60 | + if (!$mongoExtensionExists && class_exists(MongoClient::class)) { |
|
| 61 | + trigger_error( |
|
| 62 | + 'This driver is used to support the pecl MongoDb extension with mongo-php-library. |
|
| 63 | 63 | For MongoDb with Mongo PECL support use Mongo Driver.', |
| 64 | - E_USER_ERROR |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return $mongoExtensionExists && class_exists(Collection::class); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return DriverStatistic |
|
| 73 | - */ |
|
| 74 | - public function getStats(): DriverStatistic |
|
| 75 | - { |
|
| 76 | - $serverStats = $this->instance->getManager()->executeCommand( |
|
| 77 | - $this->getConfig()->getDatabaseName(), |
|
| 78 | - new Command( |
|
| 79 | - [ |
|
| 80 | - 'serverStatus' => 1, |
|
| 81 | - 'recordStats' => 0, |
|
| 82 | - 'repl' => 0, |
|
| 83 | - 'metrics' => 0, |
|
| 84 | - ] |
|
| 85 | - ) |
|
| 86 | - )->toArray()[0]; |
|
| 87 | - |
|
| 88 | - $collectionStats = $this->instance->getManager()->executeCommand( |
|
| 89 | - $this->getConfig()->getDatabaseName(), |
|
| 90 | - new Command( |
|
| 91 | - [ |
|
| 92 | - 'collStats' => $this->getConfig()->getCollectionName(), |
|
| 93 | - 'verbose' => true, |
|
| 94 | - ] |
|
| 95 | - ) |
|
| 96 | - )->toArray()[0]; |
|
| 97 | - |
|
| 98 | - $array_filter_recursive = static function ($array, callable $callback = null) use (&$array_filter_recursive) { |
|
| 99 | - $array = $callback($array); |
|
| 100 | - |
|
| 101 | - if (\is_object($array) || \is_array($array)) { |
|
| 102 | - foreach ($array as &$value) { |
|
| 103 | - $value = $array_filter_recursive($value, $callback); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return $array; |
|
| 108 | - }; |
|
| 109 | - |
|
| 110 | - $callback = static function ($item) { |
|
| 111 | - /** |
|
| 112 | - * Remove unserializable properties |
|
| 113 | - */ |
|
| 114 | - if ($item instanceof UTCDateTime) { |
|
| 115 | - return (string)$item; |
|
| 116 | - } |
|
| 117 | - return $item; |
|
| 118 | - }; |
|
| 119 | - |
|
| 120 | - $serverStats = $array_filter_recursive($serverStats, $callback); |
|
| 121 | - $collectionStats = $array_filter_recursive($collectionStats, $callback); |
|
| 122 | - |
|
| 123 | - $stats = (new DriverStatistic()) |
|
| 124 | - ->setInfo( |
|
| 125 | - 'MongoDB version ' . $serverStats->version . ', Uptime (in days): ' . round( |
|
| 126 | - $serverStats->uptime / 86400, |
|
| 127 | - 1 |
|
| 128 | - ) . "\n For more information see RawData." |
|
| 129 | - ) |
|
| 130 | - ->setSize($collectionStats->size) |
|
| 131 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 132 | - ->setRawData( |
|
| 133 | - [ |
|
| 134 | - 'serverStatus' => $serverStats, |
|
| 135 | - 'collStats' => $collectionStats, |
|
| 136 | - ] |
|
| 137 | - ); |
|
| 138 | - |
|
| 139 | - return $stats; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param CacheItemInterface $item |
|
| 144 | - * @return null|array |
|
| 145 | - */ |
|
| 146 | - protected function driverRead(CacheItemInterface $item) |
|
| 147 | - { |
|
| 148 | - $document = $this->getCollection()->findOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 149 | - |
|
| 150 | - if ($document) { |
|
| 151 | - $return = [ |
|
| 152 | - self::DRIVER_DATA_WRAPPER_INDEX => $this->decode($document[self::DRIVER_DATA_WRAPPER_INDEX]->getData()), |
|
| 153 | - self::DRIVER_TAGS_WRAPPER_INDEX => $document[self::DRIVER_TAGS_WRAPPER_INDEX]->jsonSerialize(), |
|
| 154 | - self::DRIVER_EDATE_WRAPPER_INDEX => $document[self::DRIVER_EDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 155 | - ]; |
|
| 156 | - |
|
| 157 | - if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 158 | - $return += [ |
|
| 159 | - self::DRIVER_MDATE_WRAPPER_INDEX => $document[self::DRIVER_MDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 160 | - self::DRIVER_CDATE_WRAPPER_INDEX => $document[self::DRIVER_CDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 161 | - ]; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return $return; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return null; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return Collection |
|
| 172 | - */ |
|
| 173 | - protected function getCollection(): Collection |
|
| 174 | - { |
|
| 175 | - return $this->collection; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param CacheItemInterface $item |
|
| 180 | - * @return mixed |
|
| 181 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 182 | - * @throws PhpfastcacheDriverException |
|
| 183 | - */ |
|
| 184 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 185 | - { |
|
| 186 | - /** |
|
| 187 | - * Check for Cross-Driver type confusion |
|
| 188 | - */ |
|
| 189 | - if ($item instanceof Item) { |
|
| 190 | - try { |
|
| 191 | - $set = [ |
|
| 192 | - self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), |
|
| 193 | - self::DRIVER_DATA_WRAPPER_INDEX => new Binary($this->encode($item->get()), Binary::TYPE_GENERIC), |
|
| 194 | - self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 195 | - self::DRIVER_EDATE_WRAPPER_INDEX => new UTCDateTime($item->getExpirationDate()), |
|
| 196 | - ]; |
|
| 197 | - |
|
| 198 | - if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 199 | - $set += [ |
|
| 200 | - self::DRIVER_MDATE_WRAPPER_INDEX => new UTCDateTime($item->getModificationDate()), |
|
| 201 | - self::DRIVER_CDATE_WRAPPER_INDEX => new UTCDateTime($item->getCreationDate()), |
|
| 202 | - ]; |
|
| 203 | - } |
|
| 204 | - $result = (array)$this->getCollection()->updateOne( |
|
| 205 | - ['_id' => $this->getMongoDbItemKey($item)], |
|
| 206 | - [ |
|
| 207 | - '$set' => $set, |
|
| 208 | - ], |
|
| 209 | - ['upsert' => true, 'multiple' => false] |
|
| 210 | - ); |
|
| 211 | - } catch (MongoDBException $e) { |
|
| 212 | - throw new PhpfastcacheDriverException('Got an exception while trying to write data to MongoDB server: ' . $e->getMessage(), 0, $e); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return isset($result['ok']) ? $result['ok'] == 1 : true; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param CacheItemInterface $item |
|
| 223 | - * @return bool |
|
| 224 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 225 | - */ |
|
| 226 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 227 | - { |
|
| 228 | - /** |
|
| 229 | - * Check for Cross-Driver type confusion |
|
| 230 | - */ |
|
| 231 | - if ($item instanceof Item) { |
|
| 232 | - /** |
|
| 233 | - * @var DeleteResult $deletionResult |
|
| 234 | - */ |
|
| 235 | - $deletionResult = $this->getCollection()->deleteOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 236 | - |
|
| 237 | - return $deletionResult->isAcknowledged(); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @return bool |
|
| 245 | - */ |
|
| 246 | - protected function driverClear(): bool |
|
| 247 | - { |
|
| 248 | - try { |
|
| 249 | - return $this->collection->deleteMany([])->isAcknowledged(); |
|
| 250 | - } catch (MongoDBException $e) { |
|
| 251 | - throw new PhpfastcacheDriverException('Got error while trying to empty the collection: ' . $e->getMessage(), 0, $e); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @return bool |
|
| 257 | - * @throws MongodbException |
|
| 258 | - * @throws LogicException |
|
| 259 | - */ |
|
| 260 | - protected function driverConnect(): bool |
|
| 261 | - { |
|
| 262 | - if ($this->instance instanceof Client) { |
|
| 263 | - throw new LogicException('Already connected to Mongodb server'); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $timeout = $this->getConfig()->getTimeout() * 1000; |
|
| 267 | - $collectionName = $this->getConfig()->getCollectionName(); |
|
| 268 | - $databaseName = $this->getConfig()->getDatabaseName(); |
|
| 269 | - $driverOptions = $this->getConfig()->getDriverOptions(); |
|
| 270 | - |
|
| 271 | - $this->instance = $this->instance ?: new Client($this->buildConnectionURI($databaseName), ['connectTimeoutMS' => $timeout], $driverOptions); |
|
| 272 | - $this->database = $this->database ?: $this->instance->selectDatabase($databaseName); |
|
| 273 | - |
|
| 274 | - if (!$this->collectionExists($collectionName)) { |
|
| 275 | - $this->database->createCollection($collectionName); |
|
| 276 | - $this->database->selectCollection($collectionName) |
|
| 277 | - ->createIndex( |
|
| 278 | - [self::DRIVER_KEY_WRAPPER_INDEX => 1], |
|
| 279 | - ['unique' => true, 'name' => 'unique_key_index'] |
|
| 280 | - ); |
|
| 281 | - $this->database->selectCollection($collectionName) |
|
| 282 | - ->createIndex( |
|
| 283 | - [self::DRIVER_EDATE_WRAPPER_INDEX => 1], |
|
| 284 | - ['expireAfterSeconds' => 0, 'name' => 'auto_expire_index'] |
|
| 285 | - ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $this->collection = $this->database->selectCollection($collectionName); |
|
| 289 | - |
|
| 290 | - return true; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Builds the connection URI from the given parameters. |
|
| 295 | - * |
|
| 296 | - * @param string $databaseName |
|
| 297 | - * @return string The connection URI. |
|
| 298 | - */ |
|
| 299 | - protected function buildConnectionURI(string $databaseName): string |
|
| 300 | - { |
|
| 301 | - $databaseName = \urlencode($databaseName); |
|
| 302 | - $servers = $this->getConfig()->getServers(); |
|
| 303 | - $options = $this->getConfig()->getOptions(); |
|
| 304 | - |
|
| 305 | - $protocol = $this->getConfig()->getProtocol(); |
|
| 306 | - $host = $this->getConfig()->getHost(); |
|
| 307 | - $port = $this->getConfig()->getPort(); |
|
| 308 | - $username = $this->getConfig()->getUsername(); |
|
| 309 | - $password = $this->getConfig()->getPassword(); |
|
| 310 | - |
|
| 311 | - if (count($servers) > 0) { |
|
| 312 | - $host = array_reduce( |
|
| 313 | - $servers, |
|
| 314 | - static function ($carry, $data) { |
|
| 315 | - $carry .= ($carry === '' ? '' : ',') . $data['host'] . ':' . $data['port']; |
|
| 316 | - return $carry; |
|
| 317 | - }, |
|
| 318 | - '' |
|
| 319 | - ); |
|
| 320 | - $port = false; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - return implode( |
|
| 324 | - '', |
|
| 325 | - [ |
|
| 326 | - "{$protocol}://", |
|
| 327 | - $username ?: '', |
|
| 328 | - $password ? ":{$password}" : '', |
|
| 329 | - $username ? '@' : '', |
|
| 330 | - $host, |
|
| 331 | - $port !== 27017 && $port !== false ? ":{$port}" : '', |
|
| 332 | - $databaseName ? "/{$databaseName}" : '', |
|
| 333 | - count($options) > 0 ? '?' . http_build_query($options) : '', |
|
| 334 | - ] |
|
| 335 | - ); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - protected function getMongoDbItemKey(CacheItemInterface $item) |
|
| 339 | - { |
|
| 340 | - return 'pfc_' . $item->getEncodedKey(); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /******************** |
|
| 64 | + E_USER_ERROR |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return $mongoExtensionExists && class_exists(Collection::class); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return DriverStatistic |
|
| 73 | + */ |
|
| 74 | + public function getStats(): DriverStatistic |
|
| 75 | + { |
|
| 76 | + $serverStats = $this->instance->getManager()->executeCommand( |
|
| 77 | + $this->getConfig()->getDatabaseName(), |
|
| 78 | + new Command( |
|
| 79 | + [ |
|
| 80 | + 'serverStatus' => 1, |
|
| 81 | + 'recordStats' => 0, |
|
| 82 | + 'repl' => 0, |
|
| 83 | + 'metrics' => 0, |
|
| 84 | + ] |
|
| 85 | + ) |
|
| 86 | + )->toArray()[0]; |
|
| 87 | + |
|
| 88 | + $collectionStats = $this->instance->getManager()->executeCommand( |
|
| 89 | + $this->getConfig()->getDatabaseName(), |
|
| 90 | + new Command( |
|
| 91 | + [ |
|
| 92 | + 'collStats' => $this->getConfig()->getCollectionName(), |
|
| 93 | + 'verbose' => true, |
|
| 94 | + ] |
|
| 95 | + ) |
|
| 96 | + )->toArray()[0]; |
|
| 97 | + |
|
| 98 | + $array_filter_recursive = static function ($array, callable $callback = null) use (&$array_filter_recursive) { |
|
| 99 | + $array = $callback($array); |
|
| 100 | + |
|
| 101 | + if (\is_object($array) || \is_array($array)) { |
|
| 102 | + foreach ($array as &$value) { |
|
| 103 | + $value = $array_filter_recursive($value, $callback); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return $array; |
|
| 108 | + }; |
|
| 109 | + |
|
| 110 | + $callback = static function ($item) { |
|
| 111 | + /** |
|
| 112 | + * Remove unserializable properties |
|
| 113 | + */ |
|
| 114 | + if ($item instanceof UTCDateTime) { |
|
| 115 | + return (string)$item; |
|
| 116 | + } |
|
| 117 | + return $item; |
|
| 118 | + }; |
|
| 119 | + |
|
| 120 | + $serverStats = $array_filter_recursive($serverStats, $callback); |
|
| 121 | + $collectionStats = $array_filter_recursive($collectionStats, $callback); |
|
| 122 | + |
|
| 123 | + $stats = (new DriverStatistic()) |
|
| 124 | + ->setInfo( |
|
| 125 | + 'MongoDB version ' . $serverStats->version . ', Uptime (in days): ' . round( |
|
| 126 | + $serverStats->uptime / 86400, |
|
| 127 | + 1 |
|
| 128 | + ) . "\n For more information see RawData." |
|
| 129 | + ) |
|
| 130 | + ->setSize($collectionStats->size) |
|
| 131 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 132 | + ->setRawData( |
|
| 133 | + [ |
|
| 134 | + 'serverStatus' => $serverStats, |
|
| 135 | + 'collStats' => $collectionStats, |
|
| 136 | + ] |
|
| 137 | + ); |
|
| 138 | + |
|
| 139 | + return $stats; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param CacheItemInterface $item |
|
| 144 | + * @return null|array |
|
| 145 | + */ |
|
| 146 | + protected function driverRead(CacheItemInterface $item) |
|
| 147 | + { |
|
| 148 | + $document = $this->getCollection()->findOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 149 | + |
|
| 150 | + if ($document) { |
|
| 151 | + $return = [ |
|
| 152 | + self::DRIVER_DATA_WRAPPER_INDEX => $this->decode($document[self::DRIVER_DATA_WRAPPER_INDEX]->getData()), |
|
| 153 | + self::DRIVER_TAGS_WRAPPER_INDEX => $document[self::DRIVER_TAGS_WRAPPER_INDEX]->jsonSerialize(), |
|
| 154 | + self::DRIVER_EDATE_WRAPPER_INDEX => $document[self::DRIVER_EDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 155 | + ]; |
|
| 156 | + |
|
| 157 | + if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 158 | + $return += [ |
|
| 159 | + self::DRIVER_MDATE_WRAPPER_INDEX => $document[self::DRIVER_MDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 160 | + self::DRIVER_CDATE_WRAPPER_INDEX => $document[self::DRIVER_CDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 161 | + ]; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return $return; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return null; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return Collection |
|
| 172 | + */ |
|
| 173 | + protected function getCollection(): Collection |
|
| 174 | + { |
|
| 175 | + return $this->collection; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param CacheItemInterface $item |
|
| 180 | + * @return mixed |
|
| 181 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 182 | + * @throws PhpfastcacheDriverException |
|
| 183 | + */ |
|
| 184 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 185 | + { |
|
| 186 | + /** |
|
| 187 | + * Check for Cross-Driver type confusion |
|
| 188 | + */ |
|
| 189 | + if ($item instanceof Item) { |
|
| 190 | + try { |
|
| 191 | + $set = [ |
|
| 192 | + self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), |
|
| 193 | + self::DRIVER_DATA_WRAPPER_INDEX => new Binary($this->encode($item->get()), Binary::TYPE_GENERIC), |
|
| 194 | + self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 195 | + self::DRIVER_EDATE_WRAPPER_INDEX => new UTCDateTime($item->getExpirationDate()), |
|
| 196 | + ]; |
|
| 197 | + |
|
| 198 | + if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 199 | + $set += [ |
|
| 200 | + self::DRIVER_MDATE_WRAPPER_INDEX => new UTCDateTime($item->getModificationDate()), |
|
| 201 | + self::DRIVER_CDATE_WRAPPER_INDEX => new UTCDateTime($item->getCreationDate()), |
|
| 202 | + ]; |
|
| 203 | + } |
|
| 204 | + $result = (array)$this->getCollection()->updateOne( |
|
| 205 | + ['_id' => $this->getMongoDbItemKey($item)], |
|
| 206 | + [ |
|
| 207 | + '$set' => $set, |
|
| 208 | + ], |
|
| 209 | + ['upsert' => true, 'multiple' => false] |
|
| 210 | + ); |
|
| 211 | + } catch (MongoDBException $e) { |
|
| 212 | + throw new PhpfastcacheDriverException('Got an exception while trying to write data to MongoDB server: ' . $e->getMessage(), 0, $e); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return isset($result['ok']) ? $result['ok'] == 1 : true; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param CacheItemInterface $item |
|
| 223 | + * @return bool |
|
| 224 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 225 | + */ |
|
| 226 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 227 | + { |
|
| 228 | + /** |
|
| 229 | + * Check for Cross-Driver type confusion |
|
| 230 | + */ |
|
| 231 | + if ($item instanceof Item) { |
|
| 232 | + /** |
|
| 233 | + * @var DeleteResult $deletionResult |
|
| 234 | + */ |
|
| 235 | + $deletionResult = $this->getCollection()->deleteOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 236 | + |
|
| 237 | + return $deletionResult->isAcknowledged(); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @return bool |
|
| 245 | + */ |
|
| 246 | + protected function driverClear(): bool |
|
| 247 | + { |
|
| 248 | + try { |
|
| 249 | + return $this->collection->deleteMany([])->isAcknowledged(); |
|
| 250 | + } catch (MongoDBException $e) { |
|
| 251 | + throw new PhpfastcacheDriverException('Got error while trying to empty the collection: ' . $e->getMessage(), 0, $e); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @return bool |
|
| 257 | + * @throws MongodbException |
|
| 258 | + * @throws LogicException |
|
| 259 | + */ |
|
| 260 | + protected function driverConnect(): bool |
|
| 261 | + { |
|
| 262 | + if ($this->instance instanceof Client) { |
|
| 263 | + throw new LogicException('Already connected to Mongodb server'); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $timeout = $this->getConfig()->getTimeout() * 1000; |
|
| 267 | + $collectionName = $this->getConfig()->getCollectionName(); |
|
| 268 | + $databaseName = $this->getConfig()->getDatabaseName(); |
|
| 269 | + $driverOptions = $this->getConfig()->getDriverOptions(); |
|
| 270 | + |
|
| 271 | + $this->instance = $this->instance ?: new Client($this->buildConnectionURI($databaseName), ['connectTimeoutMS' => $timeout], $driverOptions); |
|
| 272 | + $this->database = $this->database ?: $this->instance->selectDatabase($databaseName); |
|
| 273 | + |
|
| 274 | + if (!$this->collectionExists($collectionName)) { |
|
| 275 | + $this->database->createCollection($collectionName); |
|
| 276 | + $this->database->selectCollection($collectionName) |
|
| 277 | + ->createIndex( |
|
| 278 | + [self::DRIVER_KEY_WRAPPER_INDEX => 1], |
|
| 279 | + ['unique' => true, 'name' => 'unique_key_index'] |
|
| 280 | + ); |
|
| 281 | + $this->database->selectCollection($collectionName) |
|
| 282 | + ->createIndex( |
|
| 283 | + [self::DRIVER_EDATE_WRAPPER_INDEX => 1], |
|
| 284 | + ['expireAfterSeconds' => 0, 'name' => 'auto_expire_index'] |
|
| 285 | + ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $this->collection = $this->database->selectCollection($collectionName); |
|
| 289 | + |
|
| 290 | + return true; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Builds the connection URI from the given parameters. |
|
| 295 | + * |
|
| 296 | + * @param string $databaseName |
|
| 297 | + * @return string The connection URI. |
|
| 298 | + */ |
|
| 299 | + protected function buildConnectionURI(string $databaseName): string |
|
| 300 | + { |
|
| 301 | + $databaseName = \urlencode($databaseName); |
|
| 302 | + $servers = $this->getConfig()->getServers(); |
|
| 303 | + $options = $this->getConfig()->getOptions(); |
|
| 304 | + |
|
| 305 | + $protocol = $this->getConfig()->getProtocol(); |
|
| 306 | + $host = $this->getConfig()->getHost(); |
|
| 307 | + $port = $this->getConfig()->getPort(); |
|
| 308 | + $username = $this->getConfig()->getUsername(); |
|
| 309 | + $password = $this->getConfig()->getPassword(); |
|
| 310 | + |
|
| 311 | + if (count($servers) > 0) { |
|
| 312 | + $host = array_reduce( |
|
| 313 | + $servers, |
|
| 314 | + static function ($carry, $data) { |
|
| 315 | + $carry .= ($carry === '' ? '' : ',') . $data['host'] . ':' . $data['port']; |
|
| 316 | + return $carry; |
|
| 317 | + }, |
|
| 318 | + '' |
|
| 319 | + ); |
|
| 320 | + $port = false; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + return implode( |
|
| 324 | + '', |
|
| 325 | + [ |
|
| 326 | + "{$protocol}://", |
|
| 327 | + $username ?: '', |
|
| 328 | + $password ? ":{$password}" : '', |
|
| 329 | + $username ? '@' : '', |
|
| 330 | + $host, |
|
| 331 | + $port !== 27017 && $port !== false ? ":{$port}" : '', |
|
| 332 | + $databaseName ? "/{$databaseName}" : '', |
|
| 333 | + count($options) > 0 ? '?' . http_build_query($options) : '', |
|
| 334 | + ] |
|
| 335 | + ); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + protected function getMongoDbItemKey(CacheItemInterface $item) |
|
| 339 | + { |
|
| 340 | + return 'pfc_' . $item->getEncodedKey(); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /******************** |
|
| 344 | 344 | * |
| 345 | 345 | * PSR-6 Extended Methods |
| 346 | 346 | * |
| 347 | 347 | *******************/ |
| 348 | 348 | |
| 349 | - /** |
|
| 350 | - * Checks if a collection name exists on the Mongo database. |
|
| 351 | - * |
|
| 352 | - * @param string $collectionName The collection name to check. |
|
| 353 | - * |
|
| 354 | - * @return bool True if the collection exists, false if not. |
|
| 355 | - */ |
|
| 356 | - protected function collectionExists($collectionName): bool |
|
| 357 | - { |
|
| 358 | - foreach ($this->database->listCollections() as $collection) { |
|
| 359 | - if ($collection->getName() === $collectionName) { |
|
| 360 | - return true; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return false; |
|
| 365 | - } |
|
| 349 | + /** |
|
| 350 | + * Checks if a collection name exists on the Mongo database. |
|
| 351 | + * |
|
| 352 | + * @param string $collectionName The collection name to check. |
|
| 353 | + * |
|
| 354 | + * @return bool True if the collection exists, false if not. |
|
| 355 | + */ |
|
| 356 | + protected function collectionExists($collectionName): bool |
|
| 357 | + { |
|
| 358 | + foreach ($this->database->listCollections() as $collection) { |
|
| 359 | + if ($collection->getName() === $collectionName) { |
|
| 360 | + return true; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return false; |
|
| 365 | + } |
|
| 366 | 366 | } |
@@ -143,8 +143,7 @@ discard block |
||
| 143 | 143 | * @param CacheItemInterface $item |
| 144 | 144 | * @return null|array |
| 145 | 145 | */ |
| 146 | - protected function driverRead(CacheItemInterface $item) |
|
| 147 | - { |
|
| 146 | + protected function driverRead(CacheItemInterface $item) { |
|
| 148 | 147 | $document = $this->getCollection()->findOne(['_id' => $this->getMongoDbItemKey($item)]); |
| 149 | 148 | |
| 150 | 149 | if ($document) { |
@@ -208,7 +207,8 @@ discard block |
||
| 208 | 207 | ], |
| 209 | 208 | ['upsert' => true, 'multiple' => false] |
| 210 | 209 | ); |
| 211 | - } catch (MongoDBException $e) { |
|
| 210 | + } |
|
| 211 | + catch (MongoDBException $e) { |
|
| 212 | 212 | throw new PhpfastcacheDriverException('Got an exception while trying to write data to MongoDB server: ' . $e->getMessage(), 0, $e); |
| 213 | 213 | } |
| 214 | 214 | |
@@ -247,7 +247,8 @@ discard block |
||
| 247 | 247 | { |
| 248 | 248 | try { |
| 249 | 249 | return $this->collection->deleteMany([])->isAcknowledged(); |
| 250 | - } catch (MongoDBException $e) { |
|
| 250 | + } |
|
| 251 | + catch (MongoDBException $e) { |
|
| 251 | 252 | throw new PhpfastcacheDriverException('Got error while trying to empty the collection: ' . $e->getMessage(), 0, $e); |
| 252 | 253 | } |
| 253 | 254 | } |
@@ -335,8 +336,7 @@ discard block |
||
| 335 | 336 | ); |
| 336 | 337 | } |
| 337 | 338 | |
| 338 | - protected function getMongoDbItemKey(CacheItemInterface $item) |
|
| 339 | - { |
|
| 339 | + protected function getMongoDbItemKey(CacheItemInterface $item) { |
|
| 340 | 340 | return 'pfc_' . $item->getEncodedKey(); |
| 341 | 341 | } |
| 342 | 342 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | /** |
| 82 | 82 | * CouchbaseBucket::get() returns a GetResult interface |
| 83 | 83 | */ |
| 84 | - return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 84 | + return $this->decodeDocument((array) $this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 85 | 85 | } catch (DocumentNotFoundException $e) { |
| 86 | 86 | return null; |
| 87 | 87 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | ->setSize(0) |
| 162 | 162 | ->setRawData($info) |
| 163 | 163 | ->setData(implode(', ', array_keys($this->itemInstances))) |
| 164 | - ->setInfo( $info['sdk'] . "\n For more information see RawData."); |
|
| 164 | + ->setInfo($info['sdk'] . "\n For more information see RawData."); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | /** |
@@ -33,170 +33,170 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Driver extends CoubaseV2Driver |
| 35 | 35 | { |
| 36 | - /** |
|
| 37 | - * @var Scope |
|
| 38 | - */ |
|
| 39 | - protected $scope; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Collection |
|
| 43 | - */ |
|
| 44 | - protected $collection; |
|
| 45 | - |
|
| 46 | - public function __construct(ConfigurationOption $config, $instanceId) |
|
| 47 | - { |
|
| 48 | - $this->__baseConstruct($config, $instanceId); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @return bool |
|
| 53 | - * @throws PhpfastcacheLogicException |
|
| 54 | - */ |
|
| 55 | - protected function driverConnect(): bool |
|
| 56 | - { |
|
| 57 | - if (!\class_exists(ClusterOptions::class)) { |
|
| 58 | - throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 2.x so please use driver Couchbasev3'); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $connectionString = "couchbase://{$this->getConfig()->getHost()}:{$this->getConfig()->getPort()}"; |
|
| 62 | - |
|
| 63 | - $options = new ClusterOptions(); |
|
| 64 | - $options->credentials($this->getConfig()->getUsername(), $this->getConfig()->getPassword()); |
|
| 65 | - $this->instance = new Cluster($connectionString, $options); |
|
| 66 | - |
|
| 67 | - $this->setBucket($this->instance->bucket($this->getConfig()->getBucketName())); |
|
| 68 | - $this->setScope($this->getBucket()->scope($this->getConfig()->getScopeName())); |
|
| 69 | - $this->setCollection($this->getScope()->collection($this->getConfig()->getCollectionName())); |
|
| 70 | - |
|
| 71 | - return true; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param CacheItemInterface $item |
|
| 76 | - * @return null|array |
|
| 77 | - */ |
|
| 78 | - protected function driverRead(CacheItemInterface $item) |
|
| 79 | - { |
|
| 80 | - try { |
|
| 81 | - /** |
|
| 82 | - * CouchbaseBucket::get() returns a GetResult interface |
|
| 83 | - */ |
|
| 84 | - return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 85 | - } catch (DocumentNotFoundException $e) { |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param CacheItemInterface $item |
|
| 92 | - * @return bool |
|
| 93 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 94 | - */ |
|
| 95 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 96 | - { |
|
| 97 | - /** |
|
| 98 | - * Check for Cross-Driver type confusion |
|
| 99 | - */ |
|
| 100 | - if ($item instanceof Item) { |
|
| 101 | - try { |
|
| 102 | - $this->getCollection()->upsert( |
|
| 103 | - $item->getEncodedKey(), |
|
| 104 | - $this->encodeDocument($this->driverPreWrap($item)), |
|
| 105 | - (new UpsertOptions())->expiry($item->getTtl()) |
|
| 106 | - ); |
|
| 107 | - return true; |
|
| 108 | - } catch (CouchbaseException $e) { |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param CacheItemInterface $item |
|
| 118 | - * @return bool |
|
| 119 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 120 | - */ |
|
| 121 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 122 | - { |
|
| 123 | - /** |
|
| 124 | - * Check for Cross-Driver type confusion |
|
| 125 | - */ |
|
| 126 | - if ($item instanceof Item) { |
|
| 127 | - try { |
|
| 128 | - $this->getCollection()->remove($item->getEncodedKey()); |
|
| 129 | - return true; |
|
| 130 | - } catch (DocumentNotFoundException $e) { |
|
| 131 | - return true; |
|
| 132 | - } catch (CouchbaseException $e) { |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return bool |
|
| 142 | - */ |
|
| 143 | - protected function driverClear(): bool |
|
| 144 | - { |
|
| 145 | - $this->instance->buckets()->flush($this->getConfig()->getBucketName()); |
|
| 146 | - return true; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @return DriverStatistic |
|
| 151 | - */ |
|
| 152 | - public function getStats(): DriverStatistic |
|
| 153 | - { |
|
| 154 | - /** |
|
| 155 | - * Between SDK 2 and 3 we lost a lot of useful information :( |
|
| 156 | - * @see https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html#management-apis |
|
| 157 | - */ |
|
| 158 | - $info = $this->getBucket()->diagnostics(\bin2hex(\random_bytes(16))); |
|
| 159 | - |
|
| 160 | - return (new DriverStatistic()) |
|
| 161 | - ->setSize(0) |
|
| 162 | - ->setRawData($info) |
|
| 163 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 164 | - ->setInfo( $info['sdk'] . "\n For more information see RawData."); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @return Collection |
|
| 169 | - */ |
|
| 170 | - public function getCollection(): Collection |
|
| 171 | - { |
|
| 172 | - return $this->collection; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param Collection $collection |
|
| 177 | - * @return Driver |
|
| 178 | - */ |
|
| 179 | - public function setCollection(Collection $collection): Driver |
|
| 180 | - { |
|
| 181 | - $this->collection = $collection; |
|
| 182 | - return $this; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return Scope |
|
| 187 | - */ |
|
| 188 | - public function getScope(): Scope |
|
| 189 | - { |
|
| 190 | - return $this->scope; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param Scope $scope |
|
| 195 | - * @return Driver |
|
| 196 | - */ |
|
| 197 | - public function setScope(Scope $scope): Driver |
|
| 198 | - { |
|
| 199 | - $this->scope = $scope; |
|
| 200 | - return $this; |
|
| 201 | - } |
|
| 36 | + /** |
|
| 37 | + * @var Scope |
|
| 38 | + */ |
|
| 39 | + protected $scope; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Collection |
|
| 43 | + */ |
|
| 44 | + protected $collection; |
|
| 45 | + |
|
| 46 | + public function __construct(ConfigurationOption $config, $instanceId) |
|
| 47 | + { |
|
| 48 | + $this->__baseConstruct($config, $instanceId); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @return bool |
|
| 53 | + * @throws PhpfastcacheLogicException |
|
| 54 | + */ |
|
| 55 | + protected function driverConnect(): bool |
|
| 56 | + { |
|
| 57 | + if (!\class_exists(ClusterOptions::class)) { |
|
| 58 | + throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 2.x so please use driver Couchbasev3'); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $connectionString = "couchbase://{$this->getConfig()->getHost()}:{$this->getConfig()->getPort()}"; |
|
| 62 | + |
|
| 63 | + $options = new ClusterOptions(); |
|
| 64 | + $options->credentials($this->getConfig()->getUsername(), $this->getConfig()->getPassword()); |
|
| 65 | + $this->instance = new Cluster($connectionString, $options); |
|
| 66 | + |
|
| 67 | + $this->setBucket($this->instance->bucket($this->getConfig()->getBucketName())); |
|
| 68 | + $this->setScope($this->getBucket()->scope($this->getConfig()->getScopeName())); |
|
| 69 | + $this->setCollection($this->getScope()->collection($this->getConfig()->getCollectionName())); |
|
| 70 | + |
|
| 71 | + return true; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param CacheItemInterface $item |
|
| 76 | + * @return null|array |
|
| 77 | + */ |
|
| 78 | + protected function driverRead(CacheItemInterface $item) |
|
| 79 | + { |
|
| 80 | + try { |
|
| 81 | + /** |
|
| 82 | + * CouchbaseBucket::get() returns a GetResult interface |
|
| 83 | + */ |
|
| 84 | + return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 85 | + } catch (DocumentNotFoundException $e) { |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param CacheItemInterface $item |
|
| 92 | + * @return bool |
|
| 93 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 94 | + */ |
|
| 95 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 96 | + { |
|
| 97 | + /** |
|
| 98 | + * Check for Cross-Driver type confusion |
|
| 99 | + */ |
|
| 100 | + if ($item instanceof Item) { |
|
| 101 | + try { |
|
| 102 | + $this->getCollection()->upsert( |
|
| 103 | + $item->getEncodedKey(), |
|
| 104 | + $this->encodeDocument($this->driverPreWrap($item)), |
|
| 105 | + (new UpsertOptions())->expiry($item->getTtl()) |
|
| 106 | + ); |
|
| 107 | + return true; |
|
| 108 | + } catch (CouchbaseException $e) { |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param CacheItemInterface $item |
|
| 118 | + * @return bool |
|
| 119 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 120 | + */ |
|
| 121 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 122 | + { |
|
| 123 | + /** |
|
| 124 | + * Check for Cross-Driver type confusion |
|
| 125 | + */ |
|
| 126 | + if ($item instanceof Item) { |
|
| 127 | + try { |
|
| 128 | + $this->getCollection()->remove($item->getEncodedKey()); |
|
| 129 | + return true; |
|
| 130 | + } catch (DocumentNotFoundException $e) { |
|
| 131 | + return true; |
|
| 132 | + } catch (CouchbaseException $e) { |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return bool |
|
| 142 | + */ |
|
| 143 | + protected function driverClear(): bool |
|
| 144 | + { |
|
| 145 | + $this->instance->buckets()->flush($this->getConfig()->getBucketName()); |
|
| 146 | + return true; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @return DriverStatistic |
|
| 151 | + */ |
|
| 152 | + public function getStats(): DriverStatistic |
|
| 153 | + { |
|
| 154 | + /** |
|
| 155 | + * Between SDK 2 and 3 we lost a lot of useful information :( |
|
| 156 | + * @see https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html#management-apis |
|
| 157 | + */ |
|
| 158 | + $info = $this->getBucket()->diagnostics(\bin2hex(\random_bytes(16))); |
|
| 159 | + |
|
| 160 | + return (new DriverStatistic()) |
|
| 161 | + ->setSize(0) |
|
| 162 | + ->setRawData($info) |
|
| 163 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 164 | + ->setInfo( $info['sdk'] . "\n For more information see RawData."); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @return Collection |
|
| 169 | + */ |
|
| 170 | + public function getCollection(): Collection |
|
| 171 | + { |
|
| 172 | + return $this->collection; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param Collection $collection |
|
| 177 | + * @return Driver |
|
| 178 | + */ |
|
| 179 | + public function setCollection(Collection $collection): Driver |
|
| 180 | + { |
|
| 181 | + $this->collection = $collection; |
|
| 182 | + return $this; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return Scope |
|
| 187 | + */ |
|
| 188 | + public function getScope(): Scope |
|
| 189 | + { |
|
| 190 | + return $this->scope; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param Scope $scope |
|
| 195 | + * @return Driver |
|
| 196 | + */ |
|
| 197 | + public function setScope(Scope $scope): Driver |
|
| 198 | + { |
|
| 199 | + $this->scope = $scope; |
|
| 200 | + return $this; |
|
| 201 | + } |
|
| 202 | 202 | } |
@@ -43,8 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | protected $collection; |
| 45 | 45 | |
| 46 | - public function __construct(ConfigurationOption $config, $instanceId) |
|
| 47 | - { |
|
| 46 | + public function __construct(ConfigurationOption $config, $instanceId) { |
|
| 48 | 47 | $this->__baseConstruct($config, $instanceId); |
| 49 | 48 | } |
| 50 | 49 | |
@@ -75,14 +74,14 @@ discard block |
||
| 75 | 74 | * @param CacheItemInterface $item |
| 76 | 75 | * @return null|array |
| 77 | 76 | */ |
| 78 | - protected function driverRead(CacheItemInterface $item) |
|
| 79 | - { |
|
| 77 | + protected function driverRead(CacheItemInterface $item) { |
|
| 80 | 78 | try { |
| 81 | 79 | /** |
| 82 | 80 | * CouchbaseBucket::get() returns a GetResult interface |
| 83 | 81 | */ |
| 84 | 82 | return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
| 85 | - } catch (DocumentNotFoundException $e) { |
|
| 83 | + } |
|
| 84 | + catch (DocumentNotFoundException $e) { |
|
| 86 | 85 | return null; |
| 87 | 86 | } |
| 88 | 87 | } |
@@ -105,7 +104,8 @@ discard block |
||
| 105 | 104 | (new UpsertOptions())->expiry($item->getTtl()) |
| 106 | 105 | ); |
| 107 | 106 | return true; |
| 108 | - } catch (CouchbaseException $e) { |
|
| 107 | + } |
|
| 108 | + catch (CouchbaseException $e) { |
|
| 109 | 109 | return false; |
| 110 | 110 | } |
| 111 | 111 | } |
@@ -127,9 +127,11 @@ discard block |
||
| 127 | 127 | try { |
| 128 | 128 | $this->getCollection()->remove($item->getEncodedKey()); |
| 129 | 129 | return true; |
| 130 | - } catch (DocumentNotFoundException $e) { |
|
| 130 | + } |
|
| 131 | + catch (DocumentNotFoundException $e) { |
|
| 131 | 132 | return true; |
| 132 | - } catch (CouchbaseException $e) { |
|
| 133 | + } |
|
| 134 | + catch (CouchbaseException $e) { |
|
| 133 | 135 | return false; |
| 134 | 136 | } |
| 135 | 137 | } |
@@ -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 | /** |
@@ -37,170 +37,170 @@ |
||
| 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 | - * @var int |
|
| 47 | - */ |
|
| 48 | - protected $memcacheFlags = 0; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Driver constructor. |
|
| 52 | - * @param ConfigurationOption $config |
|
| 53 | - * @param string $instanceId |
|
| 54 | - * @throws PhpfastcacheDriverException |
|
| 55 | - */ |
|
| 56 | - public function __construct(ConfigurationOption $config, string $instanceId) |
|
| 57 | - { |
|
| 58 | - self::checkCollision('Memcache'); |
|
| 59 | - $this->__parentConstruct($config, $instanceId); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @return bool |
|
| 64 | - */ |
|
| 65 | - public function driverCheck(): bool |
|
| 66 | - { |
|
| 67 | - return class_exists('Memcache'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @return DriverStatistic |
|
| 72 | - */ |
|
| 73 | - public function getStats(): DriverStatistic |
|
| 74 | - { |
|
| 75 | - $stats = (array)$this->instance->getstats(); |
|
| 76 | - $stats['uptime'] = (isset($stats['uptime']) ? $stats['uptime'] : 0); |
|
| 77 | - $stats['version'] = (isset($stats['version']) ? $stats['version'] : 'UnknownVersion'); |
|
| 78 | - $stats['bytes'] = (isset($stats['bytes']) ? $stats['version'] : 0); |
|
| 79 | - |
|
| 80 | - $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
| 81 | - |
|
| 82 | - return (new DriverStatistic()) |
|
| 83 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 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 | - ->setRawData($stats) |
|
| 86 | - ->setSize((int)$stats['bytes']); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - protected function driverConnect(): bool |
|
| 93 | - { |
|
| 94 | - $this->instance = new MemcacheSoftware(); |
|
| 95 | - |
|
| 96 | - if (count($this->getConfig()->getServers()) < 1) { |
|
| 97 | - $this->getConfig()->setServers( |
|
| 98 | - [ |
|
| 99 | - [ |
|
| 100 | - 'host' => $this->getConfig()->getHost(), |
|
| 101 | - 'path' => $this->getConfig()->getPath(), |
|
| 102 | - 'port' => $this->getConfig()->getPort(), |
|
| 103 | - ] |
|
| 104 | - ] |
|
| 105 | - ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - foreach ($this->getConfig()->getServers() as $server) { |
|
| 109 | - try { |
|
| 110 | - /** |
|
| 111 | - * If path is provided we consider it as an UNIX Socket |
|
| 112 | - */ |
|
| 113 | - if (!empty($server['path']) && !$this->instance->addServer($server['path'], 0)) { |
|
| 114 | - $this->fallback = true; |
|
| 115 | - } elseif (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) { |
|
| 116 | - $this->fallback = true; |
|
| 117 | - } |
|
| 118 | - } catch (Exception $e) { |
|
| 119 | - $this->fallback = true; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Since Memcached does not throw |
|
| 124 | - * any error if not connected ... |
|
| 125 | - */ |
|
| 126 | - if (!$this->instance->getServerStatus( |
|
| 127 | - !empty($server['path']) ? $server['path'] : $server['host'], |
|
| 128 | - !empty($server['port']) ? $server['port'] : 0 |
|
| 129 | - )) { |
|
| 130 | - throw new PhpfastcacheDriverException('Memcache seems to not be connected'); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return true; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param CacheItemInterface $item |
|
| 139 | - * @return null|array |
|
| 140 | - */ |
|
| 141 | - protected function driverRead(CacheItemInterface $item) |
|
| 142 | - { |
|
| 143 | - $val = $this->instance->get($item->getKey()); |
|
| 144 | - |
|
| 145 | - if ($val === false) { |
|
| 146 | - return null; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return $val; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param CacheItemInterface $item |
|
| 154 | - * @return mixed |
|
| 155 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 156 | - */ |
|
| 157 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 158 | - { |
|
| 159 | - /** |
|
| 160 | - * Check for Cross-Driver type confusion |
|
| 161 | - */ |
|
| 162 | - if ($item instanceof Item) { |
|
| 163 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 164 | - |
|
| 165 | - // Memcache will only allow a expiration timer less than 2592000 seconds, |
|
| 166 | - // otherwise, it will assume you're giving it a UNIX timestamp. |
|
| 167 | - if ($ttl > 2592000) { |
|
| 168 | - $ttl = time() + $ttl; |
|
| 169 | - } |
|
| 170 | - return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $ttl); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @param CacheItemInterface $item |
|
| 178 | - * @return bool |
|
| 179 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 180 | - */ |
|
| 181 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 182 | - { |
|
| 183 | - /** |
|
| 184 | - * Check for Cross-Driver type confusion |
|
| 185 | - */ |
|
| 186 | - if ($item instanceof Item) { |
|
| 187 | - return $this->instance->delete($item->getKey()); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /******************** |
|
| 40 | + use DriverBaseTrait { |
|
| 41 | + __construct as protected __parentConstruct; |
|
| 42 | + } |
|
| 43 | + use MemcacheDriverCollisionDetectorTrait; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var int |
|
| 47 | + */ |
|
| 48 | + protected $memcacheFlags = 0; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Driver constructor. |
|
| 52 | + * @param ConfigurationOption $config |
|
| 53 | + * @param string $instanceId |
|
| 54 | + * @throws PhpfastcacheDriverException |
|
| 55 | + */ |
|
| 56 | + public function __construct(ConfigurationOption $config, string $instanceId) |
|
| 57 | + { |
|
| 58 | + self::checkCollision('Memcache'); |
|
| 59 | + $this->__parentConstruct($config, $instanceId); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @return bool |
|
| 64 | + */ |
|
| 65 | + public function driverCheck(): bool |
|
| 66 | + { |
|
| 67 | + return class_exists('Memcache'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @return DriverStatistic |
|
| 72 | + */ |
|
| 73 | + public function getStats(): DriverStatistic |
|
| 74 | + { |
|
| 75 | + $stats = (array)$this->instance->getstats(); |
|
| 76 | + $stats['uptime'] = (isset($stats['uptime']) ? $stats['uptime'] : 0); |
|
| 77 | + $stats['version'] = (isset($stats['version']) ? $stats['version'] : 'UnknownVersion'); |
|
| 78 | + $stats['bytes'] = (isset($stats['bytes']) ? $stats['version'] : 0); |
|
| 79 | + |
|
| 80 | + $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
| 81 | + |
|
| 82 | + return (new DriverStatistic()) |
|
| 83 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 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 | + ->setRawData($stats) |
|
| 86 | + ->setSize((int)$stats['bytes']); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + protected function driverConnect(): bool |
|
| 93 | + { |
|
| 94 | + $this->instance = new MemcacheSoftware(); |
|
| 95 | + |
|
| 96 | + if (count($this->getConfig()->getServers()) < 1) { |
|
| 97 | + $this->getConfig()->setServers( |
|
| 98 | + [ |
|
| 99 | + [ |
|
| 100 | + 'host' => $this->getConfig()->getHost(), |
|
| 101 | + 'path' => $this->getConfig()->getPath(), |
|
| 102 | + 'port' => $this->getConfig()->getPort(), |
|
| 103 | + ] |
|
| 104 | + ] |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + foreach ($this->getConfig()->getServers() as $server) { |
|
| 109 | + try { |
|
| 110 | + /** |
|
| 111 | + * If path is provided we consider it as an UNIX Socket |
|
| 112 | + */ |
|
| 113 | + if (!empty($server['path']) && !$this->instance->addServer($server['path'], 0)) { |
|
| 114 | + $this->fallback = true; |
|
| 115 | + } elseif (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) { |
|
| 116 | + $this->fallback = true; |
|
| 117 | + } |
|
| 118 | + } catch (Exception $e) { |
|
| 119 | + $this->fallback = true; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Since Memcached does not throw |
|
| 124 | + * any error if not connected ... |
|
| 125 | + */ |
|
| 126 | + if (!$this->instance->getServerStatus( |
|
| 127 | + !empty($server['path']) ? $server['path'] : $server['host'], |
|
| 128 | + !empty($server['port']) ? $server['port'] : 0 |
|
| 129 | + )) { |
|
| 130 | + throw new PhpfastcacheDriverException('Memcache seems to not be connected'); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return true; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param CacheItemInterface $item |
|
| 139 | + * @return null|array |
|
| 140 | + */ |
|
| 141 | + protected function driverRead(CacheItemInterface $item) |
|
| 142 | + { |
|
| 143 | + $val = $this->instance->get($item->getKey()); |
|
| 144 | + |
|
| 145 | + if ($val === false) { |
|
| 146 | + return null; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return $val; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @param CacheItemInterface $item |
|
| 154 | + * @return mixed |
|
| 155 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 156 | + */ |
|
| 157 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 158 | + { |
|
| 159 | + /** |
|
| 160 | + * Check for Cross-Driver type confusion |
|
| 161 | + */ |
|
| 162 | + if ($item instanceof Item) { |
|
| 163 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 164 | + |
|
| 165 | + // Memcache will only allow a expiration timer less than 2592000 seconds, |
|
| 166 | + // otherwise, it will assume you're giving it a UNIX timestamp. |
|
| 167 | + if ($ttl > 2592000) { |
|
| 168 | + $ttl = time() + $ttl; |
|
| 169 | + } |
|
| 170 | + return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $ttl); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @param CacheItemInterface $item |
|
| 178 | + * @return bool |
|
| 179 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 180 | + */ |
|
| 181 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 182 | + { |
|
| 183 | + /** |
|
| 184 | + * Check for Cross-Driver type confusion |
|
| 185 | + */ |
|
| 186 | + if ($item instanceof Item) { |
|
| 187 | + return $this->instance->delete($item->getKey()); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /******************** |
|
| 194 | 194 | * |
| 195 | 195 | * PSR-6 Extended Methods |
| 196 | 196 | * |
| 197 | 197 | *******************/ |
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * @return bool |
|
| 201 | - */ |
|
| 202 | - protected function driverClear(): bool |
|
| 203 | - { |
|
| 204 | - return $this->instance->flush(); |
|
| 205 | - } |
|
| 199 | + /** |
|
| 200 | + * @return bool |
|
| 201 | + */ |
|
| 202 | + protected function driverClear(): bool |
|
| 203 | + { |
|
| 204 | + return $this->instance->flush(); |
|
| 205 | + } |
|
| 206 | 206 | } |
@@ -53,8 +53,7 @@ discard block |
||
| 53 | 53 | * @param string $instanceId |
| 54 | 54 | * @throws PhpfastcacheDriverException |
| 55 | 55 | */ |
| 56 | - public function __construct(ConfigurationOption $config, string $instanceId) |
|
| 57 | - { |
|
| 56 | + public function __construct(ConfigurationOption $config, string $instanceId) { |
|
| 58 | 57 | self::checkCollision('Memcache'); |
| 59 | 58 | $this->__parentConstruct($config, $instanceId); |
| 60 | 59 | } |
@@ -112,10 +111,12 @@ discard block |
||
| 112 | 111 | */ |
| 113 | 112 | if (!empty($server['path']) && !$this->instance->addServer($server['path'], 0)) { |
| 114 | 113 | $this->fallback = true; |
| 115 | - } elseif (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) { |
|
| 114 | + } |
|
| 115 | + elseif (!empty($server['host']) && !$this->instance->addServer($server['host'], $server['port'])) { |
|
| 116 | 116 | $this->fallback = true; |
| 117 | 117 | } |
| 118 | - } catch (Exception $e) { |
|
| 118 | + } |
|
| 119 | + catch (Exception $e) { |
|
| 119 | 120 | $this->fallback = true; |
| 120 | 121 | } |
| 121 | 122 | |
@@ -138,8 +139,7 @@ discard block |
||
| 138 | 139 | * @param CacheItemInterface $item |
| 139 | 140 | * @return null|array |
| 140 | 141 | */ |
| 141 | - protected function driverRead(CacheItemInterface $item) |
|
| 142 | - { |
|
| 142 | + protected function driverRead(CacheItemInterface $item) { |
|
| 143 | 143 | $val = $this->instance->get($item->getKey()); |
| 144 | 144 | |
| 145 | 145 | if ($val === false) { |