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