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