forked from phpnode/YiiRedis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARedisList.php
executable file
·217 lines (206 loc) · 5.93 KB
/
ARedisList.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
<?php
/**
* Represents a redis list.
* <pre>
* $list = new ARedisList("myList");
* $list[] = "an item"; // instantly saved to redis
* $list[] = "another item"; // instantly saved to redis
* echo count($list); // 2
* echo $list->pop() // "another item"
* echo count($list); // 1
* </pre>
* @author Charles Pick
* @package packages.redis
*/
class ARedisList extends ARedisIterableEntity {
/**
* Adds an item to the list
* @param mixed $item the item to add
* @return boolean true if the item was added, otherwise false
*/
public function add($item) {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
if (!$this->getConnection()->getClient()->rpush($this->name,$item)) {
return false;
}
$this->_data = null;
$this->_count = null;
return true;
}
/**
* Removes an item from the list
* @param mixed $item the item to remove
* @return boolean true if the item was removed, otherwise false
*/
public function remove($item) {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
if (!$this->getConnection()->getClient()->lrem($this->name,$item,1)) {
return false;
}
$this->_data = null;
$this->_count = null;
return true;
}
/**
* Adds an item to the end of the list
* @param mixed $item the item to add
* @return boolean true if the item was added, otherwise false
*/
public function push($item) {
return $this->add($item);
}
/**
* Adds an item to the start of the list
* @param mixed $item the item to add
* @return boolean true if the item was added, otherwise false
*/
public function unshift($item) {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
if (!$this->getConnection()->getClient()->lpush($this->name,$item)) {
return false;
}
$this->_data = null;
$this->_count = null;
return true;
}
/**
* Removes and returns the first item from the list
* @return mixed the item that was removed from the list
*/
public function shift() {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
$item = $this->getConnection()->getClient()->lpop($this->name);
$this->_data = null;
$this->_count = null;
return $item;
}
/**
* Removes and returns the last item from the list
* @return mixed the item that was removed from the list
*/
public function pop() {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
$item = $this->getConnection()->getClient()->rpop($this->name);
$this->_data = null;
$this->_count = null;
return $item;
}
/**
* Gets a range of items in the list
* @param integer $start the 0 based index to start from
* @param integer $stop the 0 based index to end at
* @return array the items in the range
*/
public function range($start = 0, $stop = -1) {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
return $this->getConnection()->getClient()->lrange($this->name, $start, $stop);
}
/**
* Trims the list so that it will only contain the specified range of items
* @param integer $start the 0 based index to start from
* @param integer $stop the 0 based index to end at
* @return boolean true if the trim was successful
*/
public function trim($start, $stop) {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
return $this->getConnection()->getClient()->ltrim($this->name, $start, $stop) ? true : false;
}
/**
* Gets the number of items in the list
* @return integer the number of items in the list
*/
public function getCount() {
if ($this->_count === null) {
if ($this->name === null) {
throw new CException(get_class($this)." requires a name!");
}
$this->_count = (int) $this->getConnection()->getClient()->lSize($this->name);
}
return $this->_count;
}
/**
* Gets all the members in the list
* @param boolean $forceRefresh whether to force a refresh or not
* @return array the members in the list
*/
public function getData($forceRefresh = false) {
if ($forceRefresh || $this->_data === null) {
$this->_data = $this->range(0,-1);
}
return $this->_data;
}
/**
* Copies iterable data into the list.
* Note, existing data in the list will be cleared first.
* @param mixed $data the data to be copied from, must be an array or object implementing Traversable
* @throws CException If data is neither an array nor a Traversable.
*/
public function copyFrom($data)
{
if(is_array($data) || ($data instanceof Traversable))
{
if($this->_count>0)
$this->clear();
if($data instanceof CList)
$data=$data->_data;
foreach($data as $item) {
$this->add($item);
}
}
else if($data!==null)
throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.'));
}
/**
* Returns whether there is an item at the specified offset.
* This method is required by the interface ArrayAccess.
* @param integer $offset the offset to check on
* @return boolean
*/
public function offsetExists($offset)
{
return ($offset>=0 && $offset<$this->getCount());
}
/**
* Returns the item at the specified offset.
* This method is required by the interface ArrayAccess.
* @param integer $offset the offset to retrieve item.
* @return mixed the item at the offset
*/
public function offsetGet($offset)
{
return $this->_data[$offset];
}
/**
* Sets the item at the specified offset.
* This method is required by the interface ArrayAccess.
* @param integer $offset the offset to set item
* @param mixed $item the item value
*/
public function offsetSet($offset,$item)
{
$this->add($item);
}
/**
* Unsets the item at the specified offset.
* This method is required by the interface ArrayAccess.
* @param integer $offset the offset to unset item
*/
public function offsetUnset($offset)
{
$this->remove($this->_data[$offset]);
}
}