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]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Drivers\Couchbase; |
17
|
|
|
|
18
|
|
|
use CouchbaseCluster as CouchbaseClient; |
19
|
|
|
use Phpfastcache\Core\Pool\{ |
20
|
|
|
DriverBaseTrait, ExtendedCacheItemPoolInterface |
21
|
|
|
}; |
22
|
|
|
use Phpfastcache\Entities\DriverStatistic; |
23
|
|
|
use Phpfastcache\Exceptions\{ |
24
|
|
|
PhpfastcacheInvalidArgumentException, PhpfastcacheLogicException |
25
|
|
|
}; |
26
|
|
|
use Psr\Cache\CacheItemInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Driver |
30
|
|
|
* @package phpFastCache\Drivers |
31
|
|
|
* @property CouchbaseClient $instance Instance of driver service |
32
|
|
|
* @property Config $config Config object |
33
|
|
|
* @method Config getConfig() Return the config object |
34
|
|
|
*/ |
35
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
36
|
|
|
{ |
37
|
|
|
use DriverBaseTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \CouchbaseBucket[] |
41
|
|
|
*/ |
42
|
|
|
protected $bucketInstances = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \CouchbaseBucket |
46
|
|
|
*/ |
47
|
|
|
protected $bucketInstance; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $currentBucket = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function driverCheck(): bool |
58
|
|
|
{ |
59
|
|
|
return \extension_loaded('couchbase'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
* @throws PhpfastcacheLogicException |
65
|
|
|
*/ |
66
|
|
|
protected function driverConnect(): bool |
67
|
|
|
{ |
68
|
|
|
if ($this->instance instanceof CouchbaseClient) { |
|
|
|
|
69
|
|
|
throw new PhpfastcacheLogicException('Already connected to Couchbase server'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$clientConfig = $this->getConfig(); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$authenticator = new \Couchbase\PasswordAuthenticator(); |
76
|
|
|
$authenticator->username($clientConfig->getUsername())->password($clientConfig->getPassword()); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->instance = new CouchbaseClient( |
80
|
|
|
'couchbase://' . $clientConfig->getHost() . ($clientConfig->getPort() ? ":{$clientConfig->getPort()}" : '') |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->instance->authenticate($authenticator); |
84
|
|
|
$this->setBucket($this->instance->openBucket( $clientConfig->getBucketName())); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
91
|
|
|
* @return null|array |
92
|
|
|
*/ |
93
|
|
|
protected function driverRead(CacheItemInterface $item) |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
/** |
97
|
|
|
* CouchbaseBucket::get() returns a CouchbaseMetaDoc object |
98
|
|
|
*/ |
99
|
|
|
return $this->decode($this->getBucket()->get($item->getEncodedKey())->value); |
100
|
|
|
} catch (\CouchbaseException $e) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
107
|
|
|
* @return bool |
108
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
109
|
|
|
*/ |
110
|
|
|
protected function driverWrite(CacheItemInterface $item): bool |
111
|
|
|
{ |
112
|
|
|
/** |
113
|
|
|
* Check for Cross-Driver type confusion |
114
|
|
|
*/ |
115
|
|
|
if ($item instanceof Item) { |
116
|
|
|
try { |
117
|
|
|
return (bool)$this->getBucket()->upsert( |
118
|
|
|
$item->getEncodedKey(), |
119
|
|
|
$this->encode($this->driverPreWrap($item)), |
120
|
|
|
['expiry' => $item->getTtl()] |
121
|
|
|
); |
122
|
|
|
} catch (\CouchbaseException $e) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
132
|
|
|
* @return bool |
133
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
134
|
|
|
*/ |
135
|
|
|
protected function driverDelete(CacheItemInterface $item) |
136
|
|
|
{ |
137
|
|
|
/** |
138
|
|
|
* Check for Cross-Driver type confusion |
139
|
|
|
*/ |
140
|
|
|
if ($item instanceof Item) { |
141
|
|
|
try { |
142
|
|
|
return (bool)$this->getBucket()->remove($item->getEncodedKey()); |
143
|
|
|
} catch (\Couchbase\Exception $e) { |
144
|
|
|
return $e->getCode() === COUCHBASE_KEY_ENOENT; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
protected function driverClear(): bool |
155
|
|
|
{ |
156
|
|
|
$this->getBucket()->manager()->flush(); |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return \CouchbaseBucket |
162
|
|
|
*/ |
163
|
|
|
protected function getBucket(): \CouchbaseBucket |
164
|
|
|
{ |
165
|
|
|
return $this->bucketInstance; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param \CouchbaseBucket $CouchbaseBucket |
170
|
|
|
*/ |
171
|
|
|
protected function setBucket(\CouchbaseBucket $CouchbaseBucket) |
172
|
|
|
{ |
173
|
|
|
$this->bucketInstance = $CouchbaseBucket; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/******************** |
177
|
|
|
* |
178
|
|
|
* PSR-6 Extended Methods |
179
|
|
|
* |
180
|
|
|
*******************/ |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return DriverStatistic |
184
|
|
|
*/ |
185
|
|
|
public function getStats(): DriverStatistic |
186
|
|
|
{ |
187
|
|
|
$info = $this->getBucket()->manager()->info(); |
188
|
|
|
|
189
|
|
|
return (new DriverStatistic()) |
190
|
|
|
->setSize($info[ 'basicStats' ][ 'diskUsed' ]) |
191
|
|
|
->setRawData($info) |
192
|
|
|
->setData(\implode(', ', \array_keys($this->itemInstances))) |
193
|
|
|
->setInfo('CouchBase version ' . $info[ 'nodes' ][ 0 ][ 'version' ] . ', Uptime (in days): ' . round($info[ 'nodes' ][ 0 ][ 'uptime' ] / 86400, |
194
|
|
|
1) . "\n For more information see RawData."); |
195
|
|
|
} |
196
|
|
|
} |