-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRedis.php
165 lines (136 loc) · 5.49 KB
/
Redis.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
<?php
/**
* Functions in this file is mostly undocumented.
* Because everything is really just a thin layer to a specific Redis command.
* For details on each Redis command, it will be more useful to check out:
* http://redis.io/commands
*/
class Redis {
private $conn = null;
/**
* Constructor opens a persistent connection to Redis server on a given host and port.
* Saved as an instance variable which gets used throughout the object's life.
*
* @return Redis
**/
public function __construct($host, $port) {
$this->conn = phpiredis_connect($host, $port); // use connect, not pconnect, with apache
if (empty($this->conn)) {
error_log('Failed to connect to redis at '.$host.':'.$port);
}
}
/************************************************************************************************************************************
*
* Below are various Redis commands we currently use.
* See: http://redis.io/commands
*
************************************************************************************************************************************/
public function get($key) {
return phpiredis_command_bs($this->conn, array('GET', strval($key)));
}
public function set($key, $val) {
return phpiredis_command_bs($this->conn, array('SET', strval($key), strval($val)));
}
public function publish($key, $message) {
return phpiredis_command_bs($this->conn, array('PUBLISH', strval($key), strval($message)));
}
public function hmset($key, $fieldsValues) {
// this function is implemented with eval, no better way to do it.
$command = '$result = phpiredis_command_bs($this->conn, array(\'HMSET\', strval($key)';
$fields = array();
$values = array();
foreach ($fieldsValues as $field => $value) {
$fields[] = strval($field);
$values[] = strval($value);
}
for ($i = 0; $i < count($fields); $i++) {
$command .= ', strval($fields['.$i.']), strval($values['.$i.'])';
}
$command .= '));';
eval($command);
return $result;
}
public function hset($key, $field, $value) {
return phpiredis_command_bs($this->conn, array('hset', strval($key), strval($field), strval($value)));
}
public function hmget($key, $fields) {
// this function is implemented with eval, no better way to do it.
for ($i = 0; $i < count($fields); $i++) {
$fields[$i] = strval($fields[$i]);
}
$command = '$result = phpiredis_command_bs($this->conn, array(\'HMGET\', strval($key)';
for ($i = 0; $i < count($fields); $i++) {
$command .= ', strval($fields['.$i.'])';
}
$command .= '));';
eval($command);
return $result;
}
public function hincrby($key, $field, $incrBy) {
return phpiredis_command_bs($this->conn, array('hincrby', strval($key), strval($field), strval($incrBy)));
}
public function hget($key, $field) {
return phpiredis_command_bs($this->conn, array('hget', strval($key), strval($field)));
}
public function hgetall($key) {
return phpiredis_command_bs($this->conn, array('hgetall', strval($key)));
}
public function hlen($key) {
return phpiredis_command_bs($this->conn, array('hlen', strval($key)));
}
public function incr($key) {
return phpiredis_command_bs($this->conn, array('INCR', strval($key)));
}
public function expire($key, $seconds) {
return phpiredis_command_bs($this->conn, array('EXPIRE', strval($key), strval($seconds)));
}
public function hdel($key, $field) {
return phpiredis_command_bs($this->conn, array('HDEL', strval($key), strval($field)));
}
public function del($key) {
return phpiredis_command_bs($this->conn, array('DEL', strval($key)));
}
// Note: redis < 2.4 supports only 1 value, where >= 2.4 supports unlimited values.
// We just use 1 value here, so all redis versions can use this, as a simplified version.
public function lpush($key, $value) {
return phpiredis_command_bs($this->conn, array('LPUSH', strval($key), strval($value)));
}
public function lrem($key, $count, $value) {
return phpiredis_command_bs($this->conn, array('LREM', strval($key), strval($count), strval($value)));
}
public function lrange($key, $start, $stop) {
return phpiredis_command_bs($this->conn, array('LRANGE', strval($key), strval($start), strval($stop)));
}
public function brpop($key) {
return phpiredis_command_bs($this->conn, array('BRPOP', strval($key), '0'));
}
public function rpop($key) {
return phpiredis_command_bs($this->conn, array('RPOP', strval($key)));
}
public function llen($key) {
return phpiredis_command_bs($this->conn, array('LLEN', strval($key)));
}
public function sadd($key, $member) {
return phpiredis_command_bs($this->conn, array('SADD', strval($key), strval($member)));
}
public function srem($key, $member) {
return phpiredis_command_bs($this->conn, array('SREM', strval($key), strval($member)));
}
public function sismember($key, $member) {
return phpiredis_command_bs($this->conn, array('SISMEMBER', strval($key), strval($member)));
}
// Note: These new geo-spatial features are only available redis >= 3.2.
public function geoadd($key, $member, $latitude, $longitude) {
return phpiredis_command_bs($this->conn, array('GEOADD', strval($key), strval($longitude), strval($latitude), strval($member)));
}
public function georadius($key, $latitude, $longitude, $radius, $unit = 'mi', $withcoord = false, $withdist = false) {
$params = array('GEORADIUS', strval($key), strval($longitude), strval($latitude), strval($radius), strval($unit));
if ($withcoord) {
$params[] = 'WITHCOORD';
}
if ($withdist) {
$params[] = 'WITHDIST';
}
return phpiredis_command_bs($this->conn, $params);
}
}