summaryrefslogtreecommitdiffstats
path: root/preload-redis.inc
blob: cc8f9dde8afefe7f725a944927ef203a450c1957 (plain)
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
<?php
/** 
 * Redis connector using FFI and libhiredis
 * PoC, only for documentation purpose
 *
 * Copyright (c) 2019 Remi Collet
 * License: CC-BY-SA
 * http://creativecommons.org/licenses/by-sa/4.0/
 */
namespace Remi;

class Redis {
	// Singleton
	static private $ffi = NULL;
	// Redis connection: redisContext
	private $conn = NULL;
	// Log method calls
	private $debug = false;

	// From hiredis/read.h, REDIS_REPLY_* macros
	const REDIS_REPLY_STRING  = 1;
	const REDIS_REPLY_ARRAY   = 2;
	const REDIS_REPLY_INTEGER = 3;
	const REDIS_REPLY_NIL     = 4;
	const REDIS_REPLY_STATUS  = 5;
	const REDIS_REPLY_ERROR   = 6;

	/**
	 * Display debug information
	 */
	private function log($f, ...$a) {
		if ($this->debug) {
			vprintf($f, $a);
		}
	}

	/**
	 * Parser the header and and init the FFI Singleton
	 */
	private function initFFI() {
		if (self::$ffi) {
			return;
		}
		$this->log("+ %s()\n", __METHOD__);
		// Try if preloaded
		try {
			self::$ffi = \FFI::scope("_REMI_REDIS_");
		} catch (\FFI\Exception $e) {
			// Try direct load
			self::$ffi = \FFI::load(__DIR__ . '/preload-redis.h');
		}
		if (!self::$ffi) {
			throw new \RuntimeException("FFI parse fails");
		}
	}

	/**
	 * Free redisContext memory
	 */
	public function cleanup() {
		if (!is_null($this->conn)) {
			self::$ffi->redisFree($this->conn);
			$this->conn = NULL;
		}
	}

	/**
	 * Constructor + connection
	 */
	public function __construct($path, $port=6379, $debug=false) {
		$this->debug = $debug;
		$this->log("+ %s(%s, %d)\n", __METHOD__, $path, $port);
		$this->initFFI();
		if ($path[0] === '/') {
			$this->conn = self::$ffi->redisConnectUnix($path);
		} else {
			$this->conn = self::$ffi->redisConnect($path, $port);
		}
		if ($this->conn->err) {
			$msg = '';
			for($i=0 ; $i<128 && $this->conn->errstr[$i] ; $i++) {
				$msg .= $this->conn->errstr[$i];
			}
			$this->cleanup();
			throw new \RuntimeException($msg);
		}
	}

	/**
	 * Destructor
	 */
	public function __destruct() {
		$this->log("+ %s\n\n", __METHOD__);
		$this->cleanup();
	}

	/**
	 * Parse the redisReply
	 */
	private function resp($rep, $free=true) {
		if (is_null($rep)) {
			throw new \RuntimeException('Command fails');
		}
		switch ($rep->type) {
			case self::REDIS_REPLY_STATUS:
			case self::REDIS_REPLY_STRING:
			case self::REDIS_REPLY_ERROR:
				$msg = '';
				for($i=0 ; $i<$rep->len ; $i++) {
					$msg .= $rep->str[$i];
				}
				if ($rep->type == self::REDIS_REPLY_ERROR) {
					if ($free) self::$ffi->freeReplyObject($rep);
					throw new \RuntimeException($msg);
				}
				$ret = $msg;
				break;
			case self::REDIS_REPLY_ARRAY:
				$ret = [];
				for ($i=0 ; $i<$rep->elements ; $i++) {
					$ret[] = $this->resp($rep->element[$i], false);
				}
				break;
			case self::REDIS_REPLY_INTEGER:
				$ret = $rep->integer;
				break;
			case self::REDIS_REPLY_NIL:
				$ret = NULL;
				break;
			default:
				if ($free) self::$ffi->freeReplyObject($rep);
				throw new \RuntimeException('Unkown response type');
		}
		if ($free) self::$ffi->freeReplyObject($rep);
		return $ret;
	}

	/**
	 * Unkown command
	 */
	public function grrr() {
		$this->log("+ %s()\n", __METHOD__);
		return($this->resp(self::$ffi->redisCommand($this->conn, 'GRRR')));
	}

	/**
	 * DEL command
	 */
	public function del($name) {
		$this->log("+ %s(%s)\n", __METHOD__, $name);
		return($this->resp(self::$ffi->redisCommand($this->conn, "DEL $name")));
	}

	/**
	 * SET command
	 */
	public function set($name, $value) {
		$this->log("+ %s(%s, %s)\n", __METHOD__, $name, $value);
		return($this->resp(self::$ffi->redisCommand($this->conn, "SET $name %s", (string)$value)));
	}

	/**
	 * GET command
	 */
	public function get($name) {
		$this->log("+ %s(%s)\n", __METHOD__, $name);
		return($this->resp(self::$ffi->redisCommand($this->conn, "GET $name")));
	}

	/**
	 * INCR command
	 */
	public function incr($name) {
		$this->log("+ %s(%s)\n", __METHOD__, $name);
		return($this->resp(self::$ffi->redisCommand($this->conn, "INCR $name")));
	}

	/**
	 * DECR command
	 */
	public function decr($name) {
		$this->log("+ %s(%s)\n", __METHOD__, $name);
		return($this->resp(self::$ffi->redisCommand($this->conn, "DECR $name")));
	}

	/**
	 * LPUSH command
	 */
	public function lpush($name, $elt) {
		$this->log("+ %s(%s, %s)\n", __METHOD__, $name, $elt);
		return($this->resp(self::$ffi->redisCommand($this->conn, "LPUSH $name %s", (string)$elt)));
	}

	/**
	 * RPUSH command
	 */
	public function rpush($name, $elt) {
		$this->log("+ %s(%s, %s)\n", __METHOD__, $name, $elt);
		return($this->resp(self::$ffi->redisCommand($this->conn, "RPUSH $name %s", (string)$elt)));
	}

	/**
	 * LSET command
	 */
	public function lset($name, $ind, $elt) {
		$this->log("+ %s(%s, %d, %s)\n", __METHOD__, $name, $ind, $elt);
		return($this->resp(self::$ffi->redisCommand($this->conn, "LSET $name %d %s", (int)$ind, (string)$elt)));
	}

	/**
	 * LRANGE command
	 */
	public function lrange($name, $start, $end) {
		$this->log("+ %s(%s, %d, %s)\n", __METHOD__, $name, $start, $end);
		return($this->resp(self::$ffi->redisCommand($this->conn, "LRANGE $name %d %d", (int)$start, (int)$end)));
	}

	/**
	 * LPOP command
	 */
	public function lpop($name) {
		$this->log("+ %s(%s)\n", __METHOD__, $name);
		return($this->resp(self::$ffi->redisCommand($this->conn, "LPOP $name")));
	}

	/**
	 * RPOP command
	 */
	public function rpop($name) {
		$this->log("+ %s(%s)\n", __METHOD__, $name);
		return($this->resp(self::$ffi->redisCommand($this->conn, "RPOP $name")));
	}

	/**
	 * LLEN command
	 */
	public function llen($name) {
		$this->log("+ %s(%s)\n", __METHOD__, $name);
		return($this->resp(self::$ffi->redisCommand($this->conn, "LLEN $name")));
	}
}