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
|
|
|
|
13
|
|
|
namespace Ytake\LaravelCouchbase\Cache; |
14
|
|
|
|
15
|
|
|
use CouchbaseBucket; |
16
|
|
|
use CouchbaseCluster; |
17
|
|
|
use CouchbaseException; |
18
|
|
|
use Illuminate\Contracts\Cache\Store; |
19
|
|
|
use Ytake\LaravelCouchbase\Exceptions\FlushException; |
20
|
|
|
use Ytake\LaravelCouchbase\Cache\Legacy\CouchbaseTaggableStore; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class LegacyCouchbaseStore. |
24
|
|
|
* |
25
|
|
|
* @author Yuuki Takezawa<[email protected]> |
26
|
|
|
* @codeCoverageIgnore |
27
|
|
|
*/ |
28
|
|
|
class LegacyCouchbaseStore extends CouchbaseTaggableStore implements Store |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
/** @var string */ |
31
|
|
|
protected $prefix; |
32
|
|
|
|
33
|
|
|
/** @var CouchbaseBucket */ |
34
|
|
|
protected $bucket; |
35
|
|
|
|
36
|
|
|
/** @var CouchbaseCluster */ |
37
|
|
|
protected $cluster; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* LegacyCouchbaseStore constructor. |
41
|
|
|
* |
42
|
|
|
* @param CouchbaseCluster $cluster |
43
|
|
|
* @param $bucket |
44
|
|
|
* @param string $password |
45
|
|
|
* @param null $prefix |
46
|
|
|
* @param string $serialize |
47
|
|
|
*/ |
48
|
|
|
public function __construct(CouchbaseCluster $cluster, $bucket, $password = '', $prefix = null, $serialize = 'php') |
49
|
|
|
{ |
50
|
|
|
$this->cluster = $cluster; |
51
|
|
|
$this->setBucket($bucket, $password, $serialize); |
52
|
|
|
$this->setPrefix($prefix); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function get($key) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
$result = $this->bucket->get($this->resolveKey($key)); |
62
|
|
|
|
63
|
|
|
return $this->getMetaDoc($result); |
64
|
|
|
} catch (CouchbaseException $e) { |
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Store an item in the cache if the key doesn't exist. |
71
|
|
|
* |
72
|
|
|
* @param string|array $key |
73
|
|
|
* @param mixed $value |
74
|
|
|
* @param int $minutes |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function add($key, $value, $minutes = 0) |
79
|
|
|
{ |
80
|
|
|
$options = ($minutes === 0) ? [] : ['expiry' => ($minutes * 60)]; |
81
|
|
|
try { |
82
|
|
|
$this->bucket->insert($this->resolveKey($key), $value, $options); |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} catch (CouchbaseException $e) { |
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function put($key, $value, $minutes) |
94
|
|
|
{ |
95
|
|
|
$this->bucket->upsert($this->resolveKey($key), $value, ['expiry' => $minutes * 60]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function increment($key, $value = 1) |
102
|
|
|
{ |
103
|
|
|
return $this->bucket |
104
|
|
|
->counter($this->resolveKey($key), $value, ['initial' => abs($value)])->value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function decrement($key, $value = 1) |
111
|
|
|
{ |
112
|
|
|
return $this->bucket |
113
|
|
|
->counter($this->resolveKey($key), (0 - abs($value)), ['initial' => (0 - abs($value))])->value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function forever($key, $value) |
120
|
|
|
{ |
121
|
|
|
try { |
122
|
|
|
$this->bucket->insert($this->resolveKey($key), $value); |
123
|
|
|
} catch (CouchbaseException $e) { |
|
|
|
|
124
|
|
|
// bucket->insert when called from resetTag in TagSet can throw CAS exceptions, ignore. |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function forget($key) |
132
|
|
|
{ |
133
|
|
|
try { |
134
|
|
|
$this->bucket->remove($this->resolveKey($key)); |
135
|
|
|
} catch (\Exception $e) { |
136
|
|
|
// Ignore exceptions from remove |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* flush bucket. |
142
|
|
|
* |
143
|
|
|
* @throws FlushException |
144
|
|
|
* @codeCoverageIgnore |
145
|
|
|
*/ |
146
|
|
|
public function flush() |
147
|
|
|
{ |
148
|
|
|
$result = $this->bucket->manager()->flush(); |
149
|
|
|
if (isset($result['_'])) { |
150
|
|
|
throw new FlushException($result); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function getPrefix() |
158
|
|
|
{ |
159
|
|
|
return $this->prefix; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the cache key prefix. |
164
|
|
|
* |
165
|
|
|
* @param string $prefix |
166
|
|
|
*/ |
167
|
|
|
public function setPrefix($prefix) |
168
|
|
|
{ |
169
|
|
|
$this->prefix = !empty($prefix) ? $prefix.':' : ''; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $bucket |
174
|
|
|
* @param string $password |
175
|
|
|
* @param string $serialize |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setBucket($bucket, $password = '', $serialize = 'php') |
180
|
|
|
{ |
181
|
|
|
$this->bucket = $this->cluster->openBucket($bucket, $password); |
182
|
|
|
if ($serialize === 'php') { |
183
|
|
|
$this->bucket->setTranscoder('couchbase_php_serialize_encoder', 'couchbase_default_decoder'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param $keys |
191
|
|
|
* |
192
|
|
|
* @return array|string |
193
|
|
|
*/ |
194
|
|
|
private function resolveKey($keys) |
195
|
|
|
{ |
196
|
|
|
if (is_array($keys)) { |
197
|
|
|
$result = []; |
198
|
|
|
foreach ($keys as $key) { |
199
|
|
|
$result[] = $this->prefix.$key; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $result; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $this->prefix.$keys; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param $meta |
210
|
|
|
* |
211
|
|
|
* @return array|null |
212
|
|
|
*/ |
213
|
|
|
protected function getMetaDoc($meta) |
214
|
|
|
{ |
215
|
|
|
if ($meta instanceof \CouchbaseMetaDoc) { |
|
|
|
|
216
|
|
|
return $meta->value; |
217
|
|
|
} |
218
|
|
|
if (is_array($meta)) { |
219
|
|
|
$result = []; |
220
|
|
|
foreach ($meta as $row) { |
221
|
|
|
$result[] = $this->getMetaDoc($row); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $result; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|