forked from shumkov/rediska
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.php
276 lines (242 loc) · 6.88 KB
/
Hash.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
<?php
// Require Rediska
require_once dirname(__FILE__) . '/../../Rediska.php';
/**
* Rediska hash key
*
* @author Ivan Shumkov
* @package Rediska
* @subpackage Key objects
* @version @package_version@
* @link http://rediska.geometria-lab.net
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class Rediska_Key_Hash extends Rediska_Key_Abstract implements IteratorAggregate, ArrayAccess, Countable
{
/**
* Construct key
*
* @param string $name Key name
* @param integer $options Options:
* expire - Expire time
* expireIsTimestamp - Expire time is timestamp. For default false (in seconds)
* serverAlias - Server alias or connection object
* rediska - Rediska instance name, Rediska object or Rediska options for new instance
* @param string|Rediska_Connection $serverAlias Server alias or Rediska_Connection object where key is placed. Deprecated!
*/
public function __construct($name, $options = array(), $serverAlias = null)
{
parent::__construct($name, $options, $serverAlias);
$this->_throwIfNotSupported();
}
/**
* Set value to a hash field or fields
*
* @param array|string $fieldOrData Field or array of many fields and values: field => value
* @param mixed $value Value for single field
* @param boolean $overwrite Overwrite for single field (if false don't set and return false if key already exist). For default true.
* @return boolean
*/
public function set($fieldOrData, $value = null, $overwrite = true)
{
$result = $this->_getRediskaOn()->setToHash($this->getName(), $fieldOrData, $value, $overwrite);
if (!is_null($this->getExpire()) && ((!$overwrite && $result) || ($overwrite))) {
$this->expire($this->getExpire(), $this->isExpireTimestamp());
}
return $result;
}
/**
* Magic for set a field
*
* @param string $field
* @param mixed $value
* @return boolean
*/
public function __set($field, $value)
{
$this->set($field, $value);
return $value;
}
/**
* Array magic for set a field
*
* @param string $field
* @param mixed $value
* @return boolean
*/
public function offsetSet($field, $value)
{
if (is_null($field)) {
throw new Rediska_Key_Exception('Field must be present');
}
$this->set($field, $value);
return $value;
}
/**
* Get value from hash field or fields
*
* @param string $name Key name
* @param string|array $fieldOrFields Field or fields
* @return mixed
*/
public function get($fieldOrFields)
{
return $this->_getRediskaOn()->getFromHash($this->getName(), $fieldOrFields);
}
/**
* Magic for get a field
*
* @param string $field
* @return mixed
*/
public function __get($field)
{
return $this->get($field);
}
/**
* Array magic for get a field
*
* @param string $name
* @return mixed
*/
public function offsetGet($field)
{
return $this->get($field);
}
/**
* Increment field value in hash
*
* @param mixed $field Field
* @param number $amount[optional] Increment amount. Default: 1
* @return integer
*/
public function increment($field, $amount = 1)
{
$result = $this->_getRediskaOn()->incrementInHash($this->getName(), $field, $amount);
if (!is_null($this->getExpire()) && $result) {
$this->expire($this->getExpire(), $this->isExpireTimestamp());
}
return $result;
}
/**
* Test if field is present in hash
*
* @param mixed $field Field
* @return boolean
*/
public function exists($field)
{
return $this->_getRediskaOn()->existsInHash($this->getName(), $field);
}
/**
* Magic for test if field is present in hash
*
* @param string $field
* @return boolean
*/
public function __isset($field)
{
return $this->exists($field);
}
/**
* Array magic for test if field is present in hash
*
* @param string $field
* @return boolean
*/
public function offsetExists($field)
{
return $this->exists($field);
}
/**
* Remove field from hash
*
* @param mixed $field Field
* @return boolean
*/
public function remove($field)
{
$result = $this->_getRediskaOn()->deleteFromHash($this->getName(), $field);
if (!is_null($this->getExpire()) && $result) {
$this->expire($this->getExpire(), $this->isExpireTimestamp());
}
return $result;
}
/**
* Magic for remove field from hash
*
* @param string $field
* @return boolean
*/
public function __unset($field)
{
return $this->remove($field);
}
/**
* Array magic for remove field from hash
*
* @param string $field
* @return boolean
*/
public function offsetUnset($field)
{
return $this->remove($field);
}
/**
* Get hash fields
*
* @return array
*/
public function getFields()
{
return $this->_getRediskaOn()->getHashFields($this->getName());
}
/**
* Get hash values
*
* @return array
*/
public function getValues()
{
return $this->_getRediskaOn()->getHashValues($this->getName());
}
/**
* Get hash length
*
* @return intger
*/
public function getLength()
{
return $this->_getRediskaOn()->getHashLength($this->getName());
}
/**
* Get hash as array
*
* @return array
*/
public function toArray()
{
return $this->_getRediskaOn()->getHash($this->getName());
}
/* Countable implementation */
public function count()
{
return $this->getLength();
}
/* IteratorAggregate implementation */
public function getIterator()
{
return new ArrayObject($this->toArray());
}
/**
* Throw if PubSub not supported by Redis
*/
protected function _throwIfNotSupported()
{
$version = '1.3.10';
$redisVersion = $this->getRediska()->getOption('redisVersion');
if (version_compare($version, $this->getRediska()->getOption('redisVersion')) == 1) {
throw new Rediska_PubSub_Exception("Publish/Subscribe requires {$version}+ version of Redis server. Current version is {$redisVersion}. To change it specify 'redisVersion' option.");
}
}
}