-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriver.php
More file actions
296 lines (254 loc) · 9.27 KB
/
Copy pathDriver.php
File metadata and controls
296 lines (254 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
declare(strict_types=1);
namespace Phpfastcache\Drivers\Couchbasev4;
use Couchbase\BaseException as CouchbaseException;
use Couchbase\Bucket as CouchbaseBucket;
use Couchbase\Cluster;
use Couchbase\ClusterOptions;
use Couchbase\Collection;
use Couchbase\Exception\DocumentNotFoundException;
use Couchbase\Scope;
use Couchbase\UpsertOptions;
use DateTimeInterface;
use Phpfastcache\Cluster\AggregatablePoolInterface;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
use Phpfastcache\Entities\DriverStatistic;
use Phpfastcache\Event\EventManagerInterface;
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
use ReflectionExtension;
/**
* @property Cluster $instance Instance of driver service
* @method Config getConfig()
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Driver implements AggregatablePoolInterface
{
use TaggableCacheItemPoolTrait {
__construct as __baseConstruct;
}
protected Scope $scope;
protected Collection $collection;
protected CouchbaseBucket $bucketInstance;
protected int $currentParentPID = 0;
public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em)
{
$this->__baseConstruct($config, $instanceId, $em);
}
/**
* @return bool
*/
public function driverCheck(): bool
{
return extension_loaded('couchbase');
}
/**
* @return bool
* @throws PhpfastcacheDriverCheckException
*/
protected function driverConnect(): bool
{
if (!\class_exists(ClusterOptions::class)) {
throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 2.x which is no longer supported in Phpfastcache v9');
}
$extVersion = (new ReflectionExtension('couchbase'))->getVersion();
if (version_compare($extVersion, '4.0.0', '<') || version_compare($extVersion, '5.0.0', '>=')) {
throw new PhpfastcacheDriverCheckException("You are using Couchbase extension $extVersion, You need to use a Couchbase V4 extension");
}
$this->currentParentPID = posix_getppid();
$schema = $this->getConfig()->getSecure() ? 'couchbases' : 'couchbase';
$servers = $this->getConfig()->getServers();
$connectionString = $schema . '://' . implode(',', $servers) . '?ppid=' . $this->currentParentPID;
$options = $this->getConfig()->getClusterOptions();
$options->credentials($this->getConfig()->getUsername(), $this->getConfig()->getPassword());
$this->instance = new Cluster($connectionString, $options);
$this->setBucket($this->instance->bucket($this->getConfig()->getBucketName()));
$this->setScope($this->getBucket()->scope($this->getConfig()->getScopeName()));
$this->setCollection($this->getScope()->collection($this->getConfig()->getCollectionName()));
return true;
}
/**
* Work around for couchbase V4 not being fork safe
* https://issues.couchbase.com/projects/PCBC/issues/PCBC-886
* @return void
* @throws PhpfastcacheDriverCheckException
*/
protected function checkCurrentParentPID(): void
{
if ($this->currentParentPID !== posix_getppid()) {
$this->driverConnect();
}
}
/**
* @param ExtendedCacheItemInterface $item
* @return ?array<string, mixed>
*/
protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
try {
$this->checkCurrentParentPID();
/**
* CouchbaseBucket::get() returns a GetResult interface
*/
return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content());
} catch (DocumentNotFoundException) {
return null;
}
}
/**
* @param ExtendedCacheItemInterface $item
* @return bool
* @throws PhpfastcacheInvalidArgumentException
* @throws PhpfastcacheLogicException
*/
protected function driverWrite(ExtendedCacheItemInterface $item): bool
{
$this->assertCacheItemType($item, Item::class);
try {
$this->checkCurrentParentPID();
$this->getCollection()->upsert(
$item->getEncodedKey(),
$this->encodeDocument($this->driverPreWrap($item)),
(new UpsertOptions())->expiry($item->getTtl())
);
return true;
} catch (CouchbaseException) {
return false;
}
}
/**
* @param ExtendedCacheItemInterface $item
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
{
$this->assertCacheItemType($item, Item::class);
try {
$this->checkCurrentParentPID();
$this->getCollection()->remove($item->getEncodedKey());
return true;
} catch (DocumentNotFoundException) {
return true;
} catch (CouchbaseException) {
return false;
}
}
/**
* @return bool
*/
protected function driverClear(): bool
{
$this->checkCurrentParentPID();
$this->instance->buckets()->flush($this->getConfig()->getBucketName());
return true;
}
/**
* @return DriverStatistic
* @throws \Exception
*/
public function getStats(): DriverStatistic
{
$this->checkCurrentParentPID();
/**
* Between SDK 2 and 3 we lost a lot of useful information :(
* @see https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html#management-apis
*/
$info = $this->instance->diagnostics(\bin2hex(\random_bytes(16)));
return (new DriverStatistic())
->setSize(0)
->setRawData($info)
->setData(implode(', ', array_keys($this->itemInstances)))
->setInfo($info['sdk'] . "\n For more information see RawData.");
}
/**
* @return Collection
*/
public function getCollection(): Collection
{
return $this->collection;
}
/**
* @param Collection $collection
* @return Driver
*/
public function setCollection(Collection $collection): Driver
{
$this->collection = $collection;
return $this;
}
/**
* @return Scope
*/
public function getScope(): Scope
{
return $this->scope;
}
/**
* @param Scope $scope
* @return Driver
*/
public function setScope(Scope $scope): Driver
{
$this->scope = $scope;
return $this;
}
/**
* @return CouchbaseBucket
*/
protected function getBucket(): CouchbaseBucket
{
return $this->bucketInstance;
}
/**
* @param CouchbaseBucket $couchbaseBucket
*/
protected function setBucket(CouchbaseBucket $couchbaseBucket): void
{
$this->bucketInstance = $couchbaseBucket;
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function encodeDocument(array $data): array
{
$data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]);
$data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]
->format(DateTimeInterface::ATOM);
if ($this->getConfig()->isItemDetailedDate()) {
$data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]
->format(\DateTimeInterface::ATOM);
$data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]
->format(\DateTimeInterface::ATOM);
}
return $data;
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function decodeDocument(array $data): array
{
$data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->decode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]);
$data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = \DateTime::createFromFormat(
\DateTimeInterface::ATOM,
$data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]
);
if ($this->getConfig()->isItemDetailedDate()) {
$data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat(
\DateTimeInterface::ATOM,
$data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]
);
$data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = \DateTime::createFromFormat(
\DateTimeInterface::ATOM,
$data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]
);
}
return $data;
}
}