1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
*/ |
12
|
|
|
namespace Ytake\LaravelCouchbase\Cache; |
13
|
|
|
|
14
|
|
|
use CouchbaseBucket; |
15
|
|
|
use CouchbaseCluster; |
16
|
|
|
use CouchbaseException; |
17
|
|
|
use Illuminate\Cache\TaggableStore; |
18
|
|
|
use Illuminate\Contracts\Cache\Store; |
19
|
|
|
use Illuminate\Cache\RetrievesMultipleKeys; |
20
|
|
|
use Ytake\LaravelCouchbase\Exceptions\FlushException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class CouchbaseStore. |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
class CouchbaseStore extends TaggableStore implements Store |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
use RetrievesMultipleKeys; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $prefix; |
31
|
|
|
|
32
|
|
|
/** @var CouchbaseBucket */ |
33
|
|
|
protected $bucket; |
34
|
|
|
|
35
|
|
|
/** @var CouchbaseCluster */ |
36
|
|
|
protected $cluster; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* CouchbaseStore constructor. |
40
|
|
|
* |
41
|
|
|
* @param CouchbaseCluster $cluster |
42
|
|
|
* @param $bucket |
43
|
|
|
* @param string $password |
44
|
|
|
* @param null $prefix |
45
|
|
|
* @param string $serialize |
46
|
|
|
*/ |
47
|
13 |
|
public function __construct(CouchbaseCluster $cluster, $bucket, $password = '', $prefix = null, $serialize = 'php') |
48
|
|
|
{ |
49
|
13 |
|
$this->cluster = $cluster; |
50
|
13 |
|
$this->setBucket($bucket, $password, $serialize); |
51
|
13 |
|
$this->setPrefix($prefix); |
52
|
13 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
6 |
|
public function get($key) |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
6 |
|
$result = $this->bucket->get($this->resolveKey($key)); |
61
|
|
|
|
62
|
5 |
|
return $this->getMetaDoc($result); |
63
|
1 |
|
} catch (CouchbaseException $e) { |
|
|
|
|
64
|
1 |
|
return; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Store an item in the cache if the key doesn't exist. |
70
|
|
|
* |
71
|
|
|
* @param string|array $key |
72
|
|
|
* @param mixed $value |
73
|
|
|
* @param int $minutes |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
5 |
|
public function add($key, $value, $minutes = 0) |
78
|
|
|
{ |
79
|
5 |
|
$options = ($minutes === 0) ? [] : ['expiry' => ($minutes * 60)]; |
80
|
|
|
try { |
81
|
5 |
|
$this->bucket->insert($this->resolveKey($key), $value, $options); |
82
|
|
|
|
83
|
5 |
|
return true; |
84
|
1 |
|
} catch (CouchbaseException $e) { |
|
|
|
|
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function put($key, $value, $minutes) |
93
|
|
|
{ |
94
|
1 |
|
$this->bucket->upsert($this->resolveKey($key), $value, ['expiry' => $minutes * 60]); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
1 |
|
public function increment($key, $value = 1) |
101
|
|
|
{ |
102
|
1 |
|
return $this->bucket |
103
|
1 |
|
->counter($this->resolveKey($key), $value, ['initial' => abs($value)])->value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function decrement($key, $value = 1) |
110
|
|
|
{ |
111
|
1 |
|
return $this->bucket |
112
|
1 |
|
->counter($this->resolveKey($key), (0 - abs($value)), ['initial' => (0 - abs($value))])->value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function forever($key, $value) |
119
|
|
|
{ |
120
|
|
|
$this->bucket->insert($this->resolveKey($key), $value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
8 |
|
public function forget($key) |
127
|
|
|
{ |
128
|
8 |
|
$this->resolveKey($key); |
129
|
8 |
|
$this->bucket->remove($this->resolveKey($key)); |
130
|
8 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* flush bucket. |
134
|
|
|
* |
135
|
|
|
* @throws FlushException |
136
|
|
|
* @codeCoverageIgnore |
137
|
|
|
*/ |
138
|
|
|
public function flush() |
139
|
|
|
{ |
140
|
|
|
$result = $this->bucket->manager()->flush(); |
141
|
|
|
if (isset($result['_'])) { |
142
|
|
|
throw new FlushException($result); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
1 |
|
public function getPrefix() |
150
|
|
|
{ |
151
|
1 |
|
return $this->prefix; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the cache key prefix. |
156
|
|
|
* |
157
|
|
|
* @param string $prefix |
158
|
|
|
*/ |
159
|
13 |
|
public function setPrefix($prefix) |
160
|
|
|
{ |
161
|
13 |
|
$this->prefix = !empty($prefix) ? $prefix . ':' : ''; |
162
|
13 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param $bucket |
166
|
|
|
* @param string $password |
167
|
|
|
* @param string $serialize |
168
|
|
|
* |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
13 |
|
public function setBucket($bucket, $password = '', $serialize = 'php') |
172
|
|
|
{ |
173
|
13 |
|
$this->bucket = $this->cluster->openBucket($bucket, $password); |
174
|
13 |
|
if ($serialize === 'php') { |
175
|
13 |
|
$this->bucket->setTranscoder('couchbase_php_serialize_encoder', 'couchbase_default_decoder'); |
176
|
13 |
|
} |
177
|
13 |
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param $keys |
183
|
|
|
* |
184
|
|
|
* @return array|string |
185
|
9 |
|
*/ |
186
|
|
|
private function resolveKey($keys) |
187
|
9 |
|
{ |
188
|
1 |
|
if (is_array($keys)) { |
189
|
1 |
|
$result = []; |
190
|
1 |
|
foreach ($keys as $key) { |
191
|
1 |
|
$result[] = $this->prefix . $key; |
192
|
|
|
} |
193
|
1 |
|
|
194
|
|
|
return $result; |
195
|
|
|
} |
196
|
8 |
|
|
197
|
|
|
return $this->prefix . $keys; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $meta |
202
|
|
|
* |
203
|
|
|
* @return array|null |
204
|
5 |
|
*/ |
205
|
|
|
protected function getMetaDoc($meta) |
206
|
5 |
|
{ |
207
|
5 |
|
if ($meta instanceof \CouchbaseMetaDoc) { |
|
|
|
|
208
|
|
|
return $meta->value; |
209
|
1 |
|
} |
210
|
1 |
|
if (is_array($meta)) { |
211
|
1 |
|
$result = []; |
212
|
1 |
|
foreach ($meta as $row) { |
213
|
1 |
|
$result[] = $this->getMetaDoc($row); |
214
|
|
|
} |
215
|
1 |
|
|
216
|
|
|
return $result; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
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.