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
|
|
|
|
15
|
|
|
namespace phpFastCache\Drivers\Couchbase; |
16
|
|
|
|
17
|
|
|
use CouchbaseCluster as CouchbaseClient; |
18
|
|
|
use phpFastCache\Core\Pool\DriverBaseTrait; |
19
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
20
|
|
|
use phpFastCache\Entities\driverStatistic; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
22
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
23
|
|
|
use Psr\Cache\CacheItemInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Driver |
27
|
|
|
* @package phpFastCache\Drivers |
28
|
|
|
*/ |
29
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
30
|
|
|
{ |
31
|
|
|
use DriverBaseTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CouchbaseClient |
35
|
|
|
*/ |
36
|
|
|
public $instance; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \CouchbaseBucket[] |
40
|
|
|
*/ |
41
|
|
|
protected $bucketInstances = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $bucketCurrent = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Driver constructor. |
50
|
|
|
* @param array $config |
51
|
|
|
* @throws phpFastCacheDriverException |
52
|
|
|
*/ |
53
|
|
|
public function __construct(array $config = []) |
54
|
|
|
{ |
55
|
|
|
$this->setup($config); |
56
|
|
|
|
57
|
|
View Code Duplication |
if (!$this->driverCheck()) { |
|
|
|
|
58
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
59
|
|
|
} else { |
60
|
|
|
$this->driverConnect(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function driverCheck() |
68
|
|
|
{ |
69
|
|
|
return extension_loaded('Couchbase'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
74
|
|
|
* @return mixed |
75
|
|
|
* @throws \InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
|
protected function driverWrite(CacheItemInterface $item) |
78
|
|
|
{ |
79
|
|
|
/** |
80
|
|
|
* Check for Cross-Driver type confusion |
81
|
|
|
*/ |
82
|
|
|
if ($item instanceof Item) { |
83
|
|
|
return $this->getBucket()->upsert($item->getKey(), $this->encode($this->driverPreWrap($item)), ['expiry' => $item->getTtl()]); |
84
|
|
|
} else { |
85
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
91
|
|
|
* @return mixed |
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->getKey())->value); |
100
|
|
|
} catch (\CouchbaseException $e) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
107
|
|
|
* @return bool |
108
|
|
|
* @throws \InvalidArgumentException |
109
|
|
|
*/ |
110
|
|
|
protected function driverDelete(CacheItemInterface $item) |
111
|
|
|
{ |
112
|
|
|
/** |
113
|
|
|
* Check for Cross-Driver type confusion |
114
|
|
|
*/ |
115
|
|
|
if ($item instanceof Item) { |
116
|
|
|
return $this->getBucket()->remove($item->getKey()); |
117
|
|
|
} else { |
118
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function driverClear() |
126
|
|
|
{ |
127
|
|
|
return $this->getBucket()->manager()->flush(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
protected function driverConnect() |
134
|
|
|
{ |
135
|
|
|
if ($this->instance instanceof CouchbaseClient) { |
136
|
|
|
throw new \LogicException('Already connected to Couchbase server'); |
137
|
|
|
} else { |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
$host = isset($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1'; |
141
|
|
|
$password = isset($this->config[ 'password' ]) ? $this->config[ 'password' ] : ''; |
142
|
|
|
$username = isset($this->config[ 'username' ]) ? $this->config[ 'username' ] : ''; |
143
|
|
|
$buckets = isset($this->config[ 'buckets' ]) ? $this->config[ 'buckets' ] : [ |
144
|
|
|
[ |
145
|
|
|
'bucket' => 'default', |
146
|
|
|
'password' => '', |
147
|
|
|
], |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
$this->instance = new CouchbaseClient("couchbase://{$host}", $username, $password); |
151
|
|
|
|
152
|
|
|
foreach ($buckets as $bucket) { |
153
|
|
|
$this->bucketCurrent = $this->bucketCurrent ?: $bucket[ 'bucket' ]; |
154
|
|
|
$this->setBucket($bucket[ 'bucket' ], $this->instance->openBucket($bucket[ 'bucket' ], $bucket[ 'password' ])); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return \CouchbaseBucket |
163
|
|
|
*/ |
164
|
|
|
protected function getBucket() |
165
|
|
|
{ |
166
|
|
|
return $this->bucketInstances[ $this->bucketCurrent ]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $bucketName |
171
|
|
|
* @param \CouchbaseBucket $CouchbaseBucket |
172
|
|
|
* @throws \LogicException |
173
|
|
|
*/ |
174
|
|
|
protected function setBucket($bucketName, \CouchbaseBucket $CouchbaseBucket) |
175
|
|
|
{ |
176
|
|
|
if (!array_key_exists($bucketName, $this->bucketInstances)) { |
177
|
|
|
$this->bucketInstances[ $bucketName ] = $CouchbaseBucket; |
178
|
|
|
} else { |
179
|
|
|
throw new \LogicException('A bucket instance with this name already exists.'); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/******************** |
184
|
|
|
* |
185
|
|
|
* PSR-6 Extended Methods |
186
|
|
|
* |
187
|
|
|
*******************/ |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return driverStatistic |
191
|
|
|
*/ |
192
|
|
|
public function getStats() |
193
|
|
|
{ |
194
|
|
|
$info = $this->getBucket()->manager()->info(); |
195
|
|
|
|
196
|
|
|
return (new driverStatistic()) |
197
|
|
|
->setSize($info[ 'basicStats' ][ 'diskUsed' ]) |
198
|
|
|
->setRawData($info) |
199
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
200
|
|
|
->setInfo('CouchBase version ' . $info[ 'nodes' ][ 0 ][ 'version' ] . ', Uptime (in days): ' . round($info[ 'nodes' ][ 0 ][ 'uptime' ] / 86400, 1) . "\n For more information see RawData."); |
201
|
|
|
} |
202
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.