-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redframe.php
338 lines (263 loc) · 6.44 KB
/
Redframe.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
<?php
/*
*
* Red Frame
*
*
* Simple redis model abstraction class - in dev. Do NOT use for production. Learning redis project.
* Requires a Predis\Client instance (Redis Client for PHP) in the constructor. Thank you nrk!
* See: https://github.com/nrk/predis
*
* @author Nicolas Roy <nr@commun.ca>
* @version 0.1
*
*
*/
abstract class RedFrame {
/*
*
* Override these two - see samples folder for sample on how to configure your model
*
*/
protected $superStructure = array(
'name' => 'names',
'separator' => ':',
'type' => 'set', //must be set for now
'id' => 'numeric' //for v0.2
);
protected $infraStructure = array(
'title' => array(
'type' => 'string'
),
/*...*/
);
/*
*
* Basic stuff
*
*/
//The Redis Client Object
protected $redis;
//Name of the master key
protected $name;
//Item separator
protected $separator;
protected $_id;
protected $_fetched;
protected $_items;
public function __construct($redis = null, $id = null) {
if(isset($redis)) {
$this->redis = $redis;
}else{
return false;
}
if(isset($id) && !empty($id)){
$this->_id = $id;
}
$this->superStructure['next'] = $this->infraStructure;
$this->name = $this->superStructure['name'];
$this->separator = $this->superStructure['separator'];
}
/*
*
* Main getters
*
*/
public function getRedis(){
return $this->redis;
}
public function getPath($path = '', $id = null, $safe = true){
if(isset($this->_id) && $id == null){
$string = $this->name.$this->separator.$this->_id;
}
else if(isset($id) && !empty($id)){
$string = $this->name.$this->separator.$id;
}
if(!empty($string)){
if(!empty($path)){
if($safe){
//to impl.
$string .= $this->separator.$path;
}else{
$string .= $this->separator.$path;
}
}
return $string;
}else{
return false;
}
}
public function getTypeFromDb($path){
return $this->redis->type($path);
}
public function getKeyValue($path){
//to impl;
}
public function getItem($id = null){
try{
if($id == null && !empty($this->_id)){
$id = $this->_id;
}
if(!isset($id) || empty($id)){
return false;
}
$items = array();
if($this->redis->exists($this->name.$this->separator.$id)){
$path = $this->name.$this->separator.$id;
foreach($this->infraStructure as $k => $i){
$items[$k] = $this->recursiveGet($path.$this->separator.$k, $i);
}
}
if(count($items) > 0){
$this->_items = $items;
return $items;
}
else{
return false;
}
}catch(Exception $e){
return false;
}
}
public function getAllItems(){
try{
$items = array();
if($this->redis->exists($this->name) && $this->redis->type($this->name) == 'set'){
$set = $this->redis->smembers($this->name);
foreach($set as $s){
$items[] = $this->redis->getItem($s);
}
}
if(count($items) > 0){
return $items;
}else{
return false;
}
}catch (Exception $e){
return false;
}
}
protected function recursiveGet($path, $value){
if(isset($value['next']) && is_array($value['next']) && !empty($value['next'])){
$items = array();
foreach($value['next'] as $k => $i){
$items[$k] = $this->recursiveGet($path.$this->separator.$k, $i);
}
return $items;
}else{
return $this->getItemByType($path, $value['type']);
}
}
public function getItemByType($path, $type){
try{
if($type == 'set'){
return $this->redis->smembers($path);
}
else if($type == 'sortedset'){
return $this->redis->zrange($path, 0, -1);
}
else if($type == 'string'){
return $this->redis->get($path);
}
else if($type == 'hash'){
return $this->redis->hgetall($path);
}
else if($type == 'list'){
return $this->redis->lrange($path, 0, -1);
}
else{
return false;
}
}catch(Exception $e){
return false;
}
}
/*
*
* Main setters
*
*/
public function setItem($value = array(), $id = null){
try{
if($id == null && !empty($this->_id)){
$id = $this->_id;
}
if(!isset($id) || empty($id)){
return false;
}
//superStructure type must be 'set' for this to work with getAllItems()
$this->setValueByType($this->name, $this->superStructure['type'], $id);
if(is_array($value)){
$this->recursiveSet($this->infraStructure, $value, $this->name.$this->separator.$id);
//So the main key for the item exists, i.e. keys:01
$this->redis->set($this->name.$this->separator.$id, 1);
}
return true;
}catch(Exception $e){
return false;
}
}
protected function recursiveSet($modelArray, $array, $path){
foreach($array as $k => $v){
if(array_key_exists($k, $modelArray)){
if(isset($modelArray[$k]['next']) && is_array($modelArray[$k]['next']) && is_array($v) && !empty($v)){
$this->recursiveSet($modelArray[$k]['next'], $v, $path.$this->separator.$k);
}
else{
if(isset($modelArray[$k]['params']) && is_array($modelArray[$k]['params'])){
$this->setValueByType($path.$this->separator.$k, $modelArray[$k]['type'], $v, $modelArray[$k]['params']);
}else{
$this->setValueByType($path.$this->separator.$k, $modelArray[$k]['type'], $v, null);
}
}
}
}
}
public function setValueByType($key, $type, $value, $params = null){
try{
//clear the key if present
if(isset($params)){
if(isset($params['autodel']) && $params['autodel']){
$this->redis->del($key);
}
}
if($type == 'set'){
//can be array or not
$this->redis->sadd($key, $value);
}
else if($type == 'sortedset'){
//if multiple values for sorted set, scores = scores array, $value = value array
if(is_array($value)){
foreach($value['scores'] as $k => $v){
$this->redis->zadd($key, $v, $value['values'][$k]);
}
}else{
//default, score = 1
$this->redis->zadd($key, 1, $value);
}
}
else if($type == 'string'){
$this->redis->set($key, $value);
}
else if($type == 'hash'){
//filters the hash members with the provided list
//value _must_ be associative array
if(isset($params)){
if(isset($params['members']) && is_array($params['members'])){
foreach($value as $k => $v){
if(!in_array($k, $params['members'])) unset($value[$k]);
}
}
}
$this->redis->hmset($key, $value);
}
else if($type == 'list'){
//value car be array or item
$this->redis->rpush($key, $value);
}
return true;
}catch(Exception $e){
return false;
}
}
}