forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAerospike.php
382 lines (323 loc) · 9.88 KB
/
Aerospike.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Serghei Iakovlev <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Cache\Backend;
use Phalcon\Cache\FrontendInterface;
use Phalcon\Cache\Exception;
use Phalcon\Cache\Backend;
use Phalcon\Cache\BackendInterface;
/**
* Phalcon\Cache\Backend\Aerospike
*
* Allows to cache output fragments, PHP data or raw data to a Aerospike backend.
*
* <code>
* use Phalcon\Cache\Frontend\Data;
* use Phalcon\Cache\Backend\Aerospike as CacheBackend;
*
* // Cache data for 2 days
* $frontCache = new Data(['lifetime' => 172800]);
*
* // Create the Cache setting redis connection options
* $cache = new CacheBackend($frontCache, [
* 'hosts' => [
* ['addr' => '127.0.0.1', 'port' => 3000]
* ],
* 'persistent' => true,
* 'namespace' => 'test',
* 'prefix' => 'cache_',
* 'options' => [
* \Aerospike::OPT_CONNECT_TIMEOUT => 1250,
* \Aerospike::OPT_WRITE_TIMEOUT => 1500
* ]
* ]);
*
* // Cache arbitrary data
* $cache->save('my-data', [1, 2, 3, 4, 5]);
*
* // Get data
* $data = $cache->get('my-data');
* </code>
*
* @package Phalcon\Cache\Backend
* @property \Phalcon\Cache\FrontendInterface _frontend
*/
class Aerospike extends Backend implements BackendInterface
{
use Prefixable;
/**
* The Aerospike DB
* @var \Aerospike
*/
protected $db;
/**
* Default Aerospike namespace
* @var string
*/
protected $namespace = 'test';
/**
* The Aerospike Set for store cache
* @var string
*/
protected $set = 'cache';
/**
* Phalcon\Cache\Backend\Aerospike constructor
*
* @param FrontendInterface $frontend Frontend Interface
* @param array $options Constructor options
* @throws Exception
*/
public function __construct(FrontendInterface $frontend, array $options)
{
if (!isset($options['hosts']) || !is_array($options['hosts'])) {
throw new Exception('No hosts given in options');
}
if (isset($options['namespace'])) {
$this->namespace = $options['namespace'];
unset($options['namespace']);
}
if (isset($options['prefix'])) {
$this->_prefix = $options['prefix'];
}
if (isset($options['set']) && !empty($options['set'])) {
$this->set = $options['set'];
unset($options['set']);
}
$persistent = false;
if (isset($options['persistent'])) {
$persistent = (bool) $options['persistent'];
}
$opts = [];
if (isset($options['options']) && is_array($options['options'])) {
$opts = $options['options'];
}
$this->db = new \Aerospike(['hosts' => $options['hosts']], $persistent, $opts);
if (!$this->db->isConnected()) {
throw new Exception(
sprintf('Aerospike failed to connect [%s]: %s', $this->db->errorno(), $this->db->error())
);
}
parent::__construct($frontend, $options);
}
/**
* Gets the Aerospike instance.
*
* @return \Aerospike
*/
public function getDb()
{
return $this->db;
}
/**
* {@inheritdoc}
*
* @param int|string $keyName
* @param string $content
* @param int $lifetime
* @param bool $stopBuffer
* @return bool
*
* @throws Exception
*/
public function save($keyName = null, $content = null, $lifetime = null, $stopBuffer = true)
{
if ($keyName === null) {
$prefixedKey = $this->_lastKey;
} else {
$prefixedKey = $this->getPrefixedIdentifier($keyName);
}
if (!$prefixedKey) {
throw new Exception('The cache must be started first');
}
if (null === $content) {
$cachedContent = $this->_frontend->getContent();
} else {
$cachedContent = $content;
}
if (null === $lifetime) {
$lifetime = $this->_lastLifetime;
if (null === $lifetime) {
$lifetime = $this->_frontend->getLifetime();
}
}
$aKey = $this->buildKey($prefixedKey);
if (is_numeric($cachedContent)) {
$bins = ['value' => $cachedContent];
} else {
$bins = ['value' => $this->_frontend->beforeStore($cachedContent)];
}
$status = $this->db->put(
$aKey,
$bins,
$lifetime,
[\Aerospike::OPT_POLICY_KEY => \Aerospike::POLICY_KEY_SEND]
);
if (\Aerospike::OK != $status) {
throw new Exception(
sprintf('Failed storing data in Aerospike: %s', $this->db->error()),
$this->db->errorno()
);
}
if (true === $stopBuffer) {
$this->_frontend->stop();
}
if (true === $this->_frontend->isBuffering()) {
echo $cachedContent;
}
$this->_started = false;
return \Aerospike::OK == $status;
}
/**
* {@inheritdoc}
*
* @param string $prefix
* @return array
*/
public function queryKeys($prefix = null)
{
if (!$prefix) {
$prefix = $this->_prefix;
} else {
$prefix = $this->getPrefixedIdentifier($prefix);
}
$keys = [];
$globalPrefix = $this->_prefix;
$this->db->scan($this->namespace, $this->set, function ($record) use (&$keys, $prefix, $globalPrefix) {
$key = $record['key']['key'];
if (empty($prefix) || 0 === strpos($key, $prefix)) {
$keys[] = preg_replace(sprintf('#^%s(.+)#u', preg_quote($globalPrefix)), '$1', $key);
}
});
return $keys;
}
/**
* {@inheritdoc}
*
* @param int|string $keyName
* @param int $lifetime
* @return mixed
*/
public function get($keyName, $lifetime = null)
{
$prefixedKey = $this->getPrefixedIdentifier($keyName);
$aKey = $this->buildKey($prefixedKey);
$this->_lastKey = $prefixedKey;
$status = $this->db->get($aKey, $cache);
if ($status != \Aerospike::OK) {
return null;
}
$cachedContent = $cache['bins']['value'];
if (is_numeric($cachedContent)) {
return $cachedContent;
}
return $this->_frontend->afterRetrieve($cachedContent);
}
/**
* {@inheritdoc}
*
* @param int|string $keyName
* @return boolean
*/
public function delete($keyName)
{
$prefixedKey = $this->getPrefixedIdentifier($keyName);
$aKey = $this->buildKey($prefixedKey);
$this->_lastKey = $prefixedKey;
$status = $this->db->remove($aKey);
return $status == \Aerospike::OK;
}
/**
* {@inheritdoc}
*
* @param string $keyName
* @param int $lifetime
* @return boolean
*/
public function exists($keyName = null, $lifetime = null)
{
if ($keyName === null) {
$prefixedKey = $this->_lastKey;
} else {
$prefixedKey = $this->getPrefixedIdentifier($keyName);
}
if (!$prefixedKey) {
return false;
}
$aKey = $this->buildKey($prefixedKey);
$status = $this->db->get($aKey, $cache);
return $status == \Aerospike::OK;
}
/**
* Increment of given $keyName by $value
*
* @param string $keyName
* @param int $value
* @return int|false
*/
public function increment($keyName = null, $value = null)
{
if ($keyName === null) {
$prefixedKey = $this->_lastKey;
} else {
$prefixedKey = $this->getPrefixedIdentifier($keyName);
}
if (!$prefixedKey) {
return false;
}
$this->_lastKey = $prefixedKey;
if (!$value) {
$value = 1;
}
$aKey = $this->buildKey($prefixedKey);
$this->db->increment($aKey, 'value', $value);
$status = $this->db->get($aKey, $cache);
if ($status != \Aerospike::OK) {
return false;
}
return $cache['bins']['value'];
}
/**
* Decrement of $keyName by given $value
*
* @param string $keyName
* @param int $value
* @return int|false
*/
public function decrement($keyName = null, $value = null)
{
if (!$value) {
$value = -1;
} elseif ($value > 0) {
$value = -1 * abs($value);
}
return $this->increment($keyName, $value);
}
/**
* Generates a unique key used for storing cache data in Aerospike DB.
*
* @param string $key Cache key
* @return array
*/
protected function buildKey($key)
{
return $this->db->initKey(
$this->namespace,
$this->set,
$key
);
}
}