forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAerospike.php
245 lines (214 loc) · 5.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
<?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\Session\Adapter;
use Aerospike as AerospikeDb;
use Phalcon\Session\Adapter;
use Phalcon\Session\AdapterInterface;
use Phalcon\Session\Exception;
/**
* Phalcon\Session\Adapter\Aerospike
*
* This adapter store sessions in Aerospike
*
* <code>
* use Phalcon\Session\Adapter\Aerospike as AerospikeSession;
*
* $session = new AerospikeSession([
* 'hosts' => [
* ['addr' => '127.0.0.1', 'port' => 3000]
* ],
* 'persistent' => true,
* 'namespace' => 'test',
* 'prefix' => 'session_',
* 'lifetime' => 8600,
* 'uniqueId' => '3Hf90KdjQ18',
* 'options' => [
* \Aerospike::OPT_CONNECT_TIMEOUT => 1250,
* \Aerospike::OPT_WRITE_TIMEOUT => 1500
* ]
* ]);
*
* $session->start();
*
* $session->set('var', 'some-value');
*
* echo $session->get('var');
* </code>
*/
class Aerospike extends Adapter implements AdapterInterface
{
/**
* The Aerospike DB
* @var AerospikeDb
*/
protected $db;
/**
* Default Aerospike namespace
* @var string
*/
protected $namespace = 'test';
/**
* The Aerospike Set for store sessions
* @var string
*/
protected $set = 'session';
/**
* Key prefix
* @var string
*/
protected $prefix = '';
/**
* Session lifetime
* @var int
*/
protected $lifetime = 8600;
/**
* Phalcon\Session\Adapter\Aerospike constructor
*
* @param array $options Constructor options
* @throws Exception
*/
public function __construct(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'];
}
if (isset($options['prefix'])) {
$this->prefix = $options['prefix'];
}
if (isset($options['lifetime'])) {
$this->lifetime = $options['lifetime'];
}
$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 AerospikeDb(['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($options);
session_set_save_handler(
[$this, 'open'],
[$this, 'close'],
[$this, 'read'],
[$this, 'write'],
[$this, 'destroy'],
[$this, 'gc']
);
}
/**
* Gets the Aerospike instance.
*
* @return AerospikeDb
*/
public function getDb()
{
return $this->db;
}
/**
* {@inheritdoc}
*
* @return bool
*/
public function open()
{
return true;
}
/**
* {@inheritdoc}
*
* @return bool
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*
* @param string $sessionId Session variable name
* @return string
*/
public function read($sessionId)
{
$key = $this->buildKey($sessionId);
$status = $this->db->get($key, $record);
if ($status != AerospikeDb::OK) {
return false;
}
return base64_decode($record['bins']['value']);
}
/**
* {@inheritdoc}
*
* @param string $sessionId Session variable name
* @param string $data Session data
*/
public function write($sessionId, $data)
{
$key = $this->buildKey($sessionId);
$bins = ['value' => base64_encode($data)];
$this->db->put($key, $bins, $this->lifetime);
}
/**
* {@inheritdoc}
*
* @param string $sessionId Session variable name [Optional]
* @return bool
*/
public function destroy($sessionId = null)
{
$sessionId = $sessionId ?: $this->getId();
$key = $this->buildKey($sessionId);
$status = $this->db->remove($key);
return $status == AerospikeDb::OK;
}
/**
* {@inheritdoc}
*
* @return bool
*/
public function gc()
{
return true;
}
/**
* Generates a unique key used for storing session data in Aerospike DB.
*
* @param string $sessionId Session variable name
* @return array
*/
protected function buildKey($sessionId)
{
return $this->db->initKey(
$this->namespace,
$this->set,
$this->prefix . $sessionId
);
}
}