-
Notifications
You must be signed in to change notification settings - Fork 56
/
ARedisSortedSet.php
executable file
·230 lines (214 loc) · 6.35 KB
/
ARedisSortedSet.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
<?php
/**
* Represents a redis sorted set.
*
* Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.
*
* <pre>
* $set = new ARedisSortedSet("mySortedSet");
* $set->add("myThing", 0.5);
* $set->add("myOtherThing", 0.6);
*
* foreach($set as $key => $score) {
* echo $key.":".$score."\n";
* }
* </pre>
*
* @author Charles Pick
* @package packages.redis
*/
class ARedisSortedSet extends ARedisIterableEntity {
/**
* Adds an item to the set
* @param string $key the key to add
* @param integer $value the score for this key
* @return boolean true if the item was added, otherwise false
*/
public function add($key, $value) {
if (!$this->getConnection()->getClient()->zadd($this->name,$value, $key)) {
return false;
}
$this->_data = null;
$this->_count = null;
return true;
}
/**
* Removes an item from the set
* @param string $key the item to remove
* @return boolean true if the item was removed, otherwise false
*/
public function remove($key) {
if (!$this->getConnection()->getClient()->zrem($this->name,$key)) {
return false;
}
$this->_data = null;
$this->_count = null;
return true;
}
/**
* Increment (or decrement if $byAmount is negative) the score of an item from the set
* @param integer $byAmount the amount to increment by, defaults to 1
* @return integer the new value of the score if was incremented, otherwise false
*/
public function increment($key, $byAmount = 1)
{
if (!($score = $this->getConnection()->getClient()->zincrby($this->name, $byAmount, $key))) {
return false;
}
$this->_data = null;
$this->_count = null;
return $score;
}
/**
* Gets the intersection between this set and the given set(s), stores it in a new set and returns it
* @param ARedisSortedSet|string $destination the destination to store the result in
* @param mixed $set The sets to compare to, either ARedisSortedSet instances or their names
* @param array $weights the weights for the sets, if any
* @return ARedisSortedSet a set that contains the intersection between this set and the given sets
*/
public function interStore($destination, $set, $weights = null) {
if ($destination instanceof ARedisSortedSet) {
$destination->_count = null;
$destination->_data = null;
}
else {
$destination = new ARedisSortedSet($destination,$this->getConnection());
}
if (is_array($set)) {
$sets = $set;
}
else {
$sets = array($set);
}
foreach($sets as $n => $set) {
if ($set instanceof ARedisSortedSet) {
$sets[$n] = $set->name;
}
}
array_unshift($sets,$this->name);
$parameters = array(
$destination->name,
$sets,
);
if ($weights !== null) {
$parameters[] = $weights;
}
$total = call_user_func_array(array(
$this->getConnection()->getClient(),
"zinter"
),$parameters);
$destination->_count = $total;
return $destination;
}
/**
* Gets the union of this set and the given set(s), stores it in a new set and returns it
* @param ARedisSortedSet|string $destination the destination to store the result in
* @param mixed $set The sets to compare to, either ARedisSortedSet instances or their names
* @param array $weights the weights for the sets, if any
* @return ARedisSortedSet a set that contains the union of this set and the given sets
*/
public function unionStore($destination, $set, $weights = null) {
if ($destination instanceof ARedisSortedSet) {
$destination->_count = null;
$destination->_data = null;
}
else {
$destination = new ARedisSortedSet($destination,$this->getConnection());
}
if (is_array($set)) {
$sets = $set;
}
else {
$sets = array($set);
}
foreach($sets as $n => $set) {
if ($set instanceof ARedisSortedSet) {
$sets[$n] = $set->name;
}
}
array_unshift($sets,$this->name);
$parameters = array(
$destination->name,
$sets,
);
if ($weights !== null) {
$parameters[] = $weights;
}
$total = call_user_func_array(array(
$this->getConnection()->getClient(),
"zunion"
),$parameters);
$destination->_count = $total;
return $destination;
}
/**
* Returns an iterator for traversing the items in the set.
* This method is required by the interface IteratorAggregate.
* @return Iterator an iterator for traversing the items in the set.
*/
public function getIterator()
{
return new CMapIterator($this->getData());
}
/**
* Gets the number of items in the set
* @return integer the number of items in the set
*/
public function getCount() {
if ($this->_count === null) {
$this->_count = $this->getConnection()->getClient()->zcard($this->name);
}
return $this->_count;
}
/**
* Gets all the members in the sorted set
* @param boolean $forceRefresh whether to force a refresh or not
* @return array the members in the set
*/
public function getData($forceRefresh = false) {
if ($forceRefresh || $this->_data === null) {
$this->_data = $this->getConnection()->getClient()->zrange($this->name,0, -1, true);
}
return $this->_data;
}
/**
* 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
* @throws CException if the offset is invalid
*/
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($offset,$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($offset);
}
}