-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.php
126 lines (91 loc) · 2.94 KB
/
list.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
<?php
namespace Predis;
error_reporting(E_ALL);
require_once(dirname(__FILE__)."/predis.php");
// TODO
// * Option to serialize sets, unserialize gets. Will require custom Iterator
// * Option to load an existing php array
// * Option to use pipelining
// * Inheret from a base RedisArrayObject
class RedisList extends \ArrayObject {
static $LPUSH_KEYS = array("lpush", "<<", "l");
public $redis_key;
public function __construct($redis_key, $expiration=null) {
$this->redis_key = $redis_key;
$this->redis = new Client('redis://localhost:6379/');
if ($expiration !== null) {
$this->expire($expiration);
}
}
public function expire($expiration) {
return $this->redis->expire($this->redis_key, $expiration);
}
public function ttl() {
return $this->redis->ttl($this->redis_key);
}
public function persist() {
return $this->expire(0);
}
public function rename($new_redis_key, $play_nice=true) {
if ($play_nice === true) {
$this->redis->renamenx($this->redis_key, $new_redis_key);
} else {
$this->redis->rename($this->redis_key, $new_redis_key);
}
$this->redis_key = $new_redis_key;
}
public function getIterator($start=0, $end=null){
if ($end === null) {
$end = $this->count();
}
$range = $this->redis->lrange($this->redis_key, $start, --$end);
return new \ArrayIterator($range);
}
public function each($opts, $block) {
$default_opts = array("start" => 0, "end" => 25);
if (! is_array($opts)) {
$opts = array("end" => $opts);
}
$opts = array_merge($default_opts, $opts);
$iterator = $this->getIterator($opts["start"], $opts["end"]);
while ($iterator->valid()) {
$block($iterator->key(), $iterator->current());
$iterator->next();
}
}
public function count() {
return $this->redis->llen($this->redis_key);
}
public function offsetExists($index) {
return parent::offsetExists($index);
}
public function offsetGet($index) {
return $this->redis->lindex($this->redis_key, $index);
}
public function offsetSet($index, $value) {
if (!is_numeric($index) && $index !== null && ! in_array($index, self::$LPUSH_KEYS)) throw new Exception("key must be integer");
if ($index === null) {
$this->redis->rpush($this->redis_key, $value);
} else {
if (in_array($index, self::$LPUSH_KEYS)) {
$this->redis->lpush($this->redis_key, $value);
$index = null;
} else {
$this->redis->lset($this->redis_key, $index, $value);
}
}
return parent::offsetSet($index, $value);
}
public function offsetUnset($index) {
$uuid = uniqid();
$this->redis->lset($this->redis_key, $index, $uuid);
$this->redis->lrem($this->redis_key, 1, $uuid);
return parent::offsetUnset($index);
}
public function pop() {
return $this->redis->rpop($this->redis_key);
}
public function shift() {
return $this->redis->lpop($this->redis_key);
}
}