Skip to content

Commit ce080dd

Browse files
committed
Added beta support for Couchbase SDK 3 as requested in #721
1 parent 3436704 commit ce080dd

File tree

10 files changed

+477
-34
lines changed

10 files changed

+477
-34
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This ensure you that the library is completely reliable when it come to manipula
2525
| Regular drivers | High performances drivers | Development drivers | Cluster-Aggregated drivers |
2626
|---------------------------------|------------------------------------|-------------------------------|-----------------------------------|
2727
| `Apcu` *(APC support removed)* | `Cassandra` | `Devnull` | `FullReplicationCluster` |
28-
| `Cookie` | `CouchBase` | `Devfalse` | `SemiReplicationCluster` |
28+
| `Cookie` | `CouchBase(v3)` | `Devfalse` | `SemiReplicationCluster` |
2929
| `Files` | `Couchdb` | `Devtrue` | `MasterSlaveReplicationCluster` |
3030
| `Leveldb` | `Mongodb` | `Memstatic` | `RandomReplicationCluster` |
3131
| `Memcache(d)` | `Predis` | | |

docs/DRIVERS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* A cookie driver to store non-sensitive scalar (only) data. Limited storage up to 4Ko.
1010
* Couchbase
1111
* A very high-performance NoSQL driver using a key-value pair system
12+
* Couchbasev3 **(Added in V8.0.8)**
13+
* Same as Couchbase but for Couchbase PHP-SDK 3.0 support
1214
* Couchdb
1315
* A very high-performance NoSQL driver using a key-value pair system
1416
* Devfalse

lib/Phpfastcache/Drivers/Couchbase/Config.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
class Config extends ConfigurationOption
2222
{
23+
protected const DEFAULT_VALUE = '_default';
2324
/**
2425
* @var string
2526
*/
@@ -43,7 +44,7 @@ class Config extends ConfigurationOption
4344
/**
4445
* @var string
4546
*/
46-
protected $bucketName = 'default';
47+
protected $bucketName = self::DEFAULT_VALUE;
4748

4849
/**
4950
* @return string
@@ -134,4 +135,4 @@ public function setBucketName(string $bucketName): Config
134135
$this->bucketName = $bucketName;
135136
return $this;
136137
}
137-
}
138+
}

lib/Phpfastcache/Drivers/Couchbase/Driver.php

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
namespace Phpfastcache\Drivers\Couchbase;
1818

19-
use Couchbase\Exception;
19+
use Couchbase\Exception as CouchbaseException;
2020
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;
2424
use Phpfastcache\Cluster\AggregatablePoolInterface;
2525
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface};
2626
use Phpfastcache\Entities\DriverStatistic;
27-
use Phpfastcache\Exceptions\{PhpfastcacheInvalidArgumentException, PhpfastcacheLogicException};
27+
use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException, PhpfastcacheInvalidArgumentException, PhpfastcacheLogicException};
2828
use Psr\Cache\CacheItemInterface;
2929

3030

@@ -87,13 +87,16 @@ public function getStats(): DriverStatistic
8787
*/
8888
protected function driverConnect(): bool
8989
{
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+
9094
if ($this->instance instanceof CouchbaseClient) {
9195
throw new PhpfastcacheLogicException('Already connected to Couchbase server');
9296
}
9397

9498
$clientConfig = $this->getConfig();
9599

96-
97100
$authenticator = new PasswordAuthenticator();
98101
$authenticator->username($clientConfig->getUsername())->password($clientConfig->getPassword());
99102

@@ -125,7 +128,7 @@ protected function driverRead(CacheItemInterface $item)
125128
/**
126129
* CouchbaseBucket::get() returns a CouchbaseMetaDoc object
127130
*/
128-
return $this->decode($this->getBucket()->get($item->getEncodedKey())->value);
131+
return $this->decodeDocument((array) $this->getBucket()->get($item->getEncodedKey())->value);
129132
} catch (CouchbaseException $e) {
130133
return null;
131134
}
@@ -153,7 +156,7 @@ protected function driverWrite(CacheItemInterface $item): bool
153156
try {
154157
return (bool)$this->getBucket()->upsert(
155158
$item->getEncodedKey(),
156-
$this->encode($this->driverPreWrap($item)),
159+
$this->encodeDocument($this->driverPreWrap($item)),
157160
['expiry' => $item->getTtl()]
158161
);
159162
} catch (CouchbaseException $e) {
@@ -185,6 +188,49 @@ protected function driverDelete(CacheItemInterface $item): bool
185188
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
186189
}
187190

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+
}
188234
/********************
189235
*
190236
* PSR-6 Extended Methods
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of phpFastCache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt file.
9+
*
10+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
*
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace Phpfastcache\Drivers\Couchbasev3;
18+
19+
use Phpfastcache\Drivers\Couchbase\Config as CoubaseV2Config;
20+
21+
class Config extends CoubaseV2Config
22+
{
23+
/**
24+
* @var string
25+
*/
26+
protected $bucketName = 'phpfastcache';
27+
28+
protected $scopeName = self::DEFAULT_VALUE;
29+
30+
protected $collectionName = self::DEFAULT_VALUE;
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getScopeName(): string
36+
{
37+
return $this->scopeName;
38+
}
39+
40+
/**
41+
* @param string $scopeName
42+
* @return Config
43+
*/
44+
public function setScopeName(string $scopeName): Config
45+
{
46+
$this->scopeName = $scopeName;
47+
return $this;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getCollectionName(): string
54+
{
55+
return $this->collectionName;
56+
}
57+
58+
/**
59+
* @param string $collectionName
60+
* @return Config
61+
*/
62+
public function setCollectionName(string $collectionName): Config
63+
{
64+
$this->collectionName = $collectionName;
65+
return $this;
66+
}
67+
}

0 commit comments

Comments
 (0)