|
16 | 16 |
|
17 | 17 | namespace Phpfastcache\Drivers\Couchbase; |
18 | 18 |
|
19 | | -use Couchbase\Exception; |
| 19 | +use Couchbase\Exception as CouchbaseException; |
20 | 20 | use Couchbase\PasswordAuthenticator; |
21 | | -use CouchbaseBucket; |
22 | | -use CouchbaseCluster as CouchbaseClient; |
23 | | -use CouchbaseException; |
| 21 | +use Couchbase\Bucket as CouchbaseBucket; |
| 22 | +use Couchbase\Cluster as CouchbaseClient; |
| 23 | +use DateTime; |
24 | 24 | use Phpfastcache\Cluster\AggregatablePoolInterface; |
25 | 25 | use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface}; |
26 | 26 | use Phpfastcache\Entities\DriverStatistic; |
27 | | -use Phpfastcache\Exceptions\{PhpfastcacheInvalidArgumentException, PhpfastcacheLogicException}; |
| 27 | +use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException, PhpfastcacheInvalidArgumentException, PhpfastcacheLogicException}; |
28 | 28 | use Psr\Cache\CacheItemInterface; |
29 | 29 |
|
30 | 30 |
|
@@ -87,13 +87,16 @@ public function getStats(): DriverStatistic |
87 | 87 | */ |
88 | 88 | protected function driverConnect(): bool |
89 | 89 | { |
| 90 | + if (\class_exists(\Couchbase\ClusterOptions::class)) { |
| 91 | + throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 3.x so please use driver Couchbasev3'); |
| 92 | + } |
| 93 | + |
90 | 94 | if ($this->instance instanceof CouchbaseClient) { |
91 | 95 | throw new PhpfastcacheLogicException('Already connected to Couchbase server'); |
92 | 96 | } |
93 | 97 |
|
94 | 98 | $clientConfig = $this->getConfig(); |
95 | 99 |
|
96 | | - |
97 | 100 | $authenticator = new PasswordAuthenticator(); |
98 | 101 | $authenticator->username($clientConfig->getUsername())->password($clientConfig->getPassword()); |
99 | 102 |
|
@@ -125,7 +128,7 @@ protected function driverRead(CacheItemInterface $item) |
125 | 128 | /** |
126 | 129 | * CouchbaseBucket::get() returns a CouchbaseMetaDoc object |
127 | 130 | */ |
128 | | - return $this->decode($this->getBucket()->get($item->getEncodedKey())->value); |
| 131 | + return $this->decodeDocument((array) $this->getBucket()->get($item->getEncodedKey())->value); |
129 | 132 | } catch (CouchbaseException $e) { |
130 | 133 | return null; |
131 | 134 | } |
@@ -153,7 +156,7 @@ protected function driverWrite(CacheItemInterface $item): bool |
153 | 156 | try { |
154 | 157 | return (bool)$this->getBucket()->upsert( |
155 | 158 | $item->getEncodedKey(), |
156 | | - $this->encode($this->driverPreWrap($item)), |
| 159 | + $this->encodeDocument($this->driverPreWrap($item)), |
157 | 160 | ['expiry' => $item->getTtl()] |
158 | 161 | ); |
159 | 162 | } catch (CouchbaseException $e) { |
@@ -185,6 +188,49 @@ protected function driverDelete(CacheItemInterface $item): bool |
185 | 188 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
186 | 189 | } |
187 | 190 |
|
| 191 | + /** |
| 192 | + * @param array $data |
| 193 | + * @return array |
| 194 | + */ |
| 195 | + protected function encodeDocument(array $data): array |
| 196 | + { |
| 197 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
| 198 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 199 | + |
| 200 | + if($this->getConfig()->isItemDetailedDate()){ |
| 201 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 202 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 203 | + } |
| 204 | + |
| 205 | + return $data; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * @param array $data |
| 210 | + * @return array |
| 211 | + */ |
| 212 | + protected function decodeDocument(array $data): array |
| 213 | + { |
| 214 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->decode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
| 215 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
| 216 | + \DateTime::ATOM, |
| 217 | + $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] |
| 218 | + ); |
| 219 | + |
| 220 | + if($this->getConfig()->isItemDetailedDate()){ |
| 221 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
| 222 | + \DateTime::ATOM, |
| 223 | + $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] |
| 224 | + ); |
| 225 | + |
| 226 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = \DateTime::createFromFormat( |
| 227 | + \DateTime::ATOM, |
| 228 | + $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] |
| 229 | + ); |
| 230 | + } |
| 231 | + |
| 232 | + return $data; |
| 233 | + } |
188 | 234 | /******************** |
189 | 235 | * |
190 | 236 | * PSR-6 Extended Methods |
|
0 commit comments