summaryrefslogtreecommitdiffstats
path: root/redis.php
blob: 7c42d0b8ecb3e575f42c28891208c637b5ba947a (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
<?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;

if (PHP_VERSION_ID < 70400 || !extension_loaded("ffi")) {
	die("PHP 7.4 with ffi extension required\n");
}
if (PHP_SAPI != "cli") {
	Header('Content-Type: text/plain');
}
printf("PHP version %s\n", PHP_VERSION);
if (PHP_SAPI == "cli" && !class_exists("\\Remi\\Redis")) {
	printf("Fallback on manual load\n\n");
	require_once __DIR__ . '/preload-redis.inc';
} else {
	printf("Use preloaded class\n\n");
}

// ---------------- TESTS ------------------------
$r = new Redis("localhost", 6379, true);
try {
	var_dump($r->grrr());
} catch(\Exception $e) {
	printf("** Catched %s: %s **\n", get_class($e), $e->getMessage());
}

// del / set / get
var_dump($r->del('foo'));
var_dump($r->get('foo'));
var_dump($r->set('foo', date("Y/m/d H:i:s")));
var_dump($r->get('foo'));
unset($r);

// incr
$r = new Redis("localhost", 6379, true);
var_dump($r->set('foo', 41));
var_dump($r->get('foo'));
var_dump($r->incr('foo'));
var_dump($r->get('foo'));

// list
var_dump($r->rpush('mylist', 'one'));
var_dump($r->rpush('mylist', 'too'));
var_dump($r->lset('mylist', 1, 'two'));
try {
	var_dump($r->lset('mylist', 9, 'nine'));
} catch(\Exception $e) {
	printf("** Catched %s: %s **\n", get_class($e), $e->getMessage());
}
var_dump($r->llen('mylist'));
var_dump($r->lrange('mylist', 0, 100));
while($r->llen('mylist')) {
	var_dump($r->lpop('mylist'));
}
var_dump($r->llen('mylist'));

unset($r);

// Exception in connection
try {
	$r = new Redis("localhost", 1234, true);
} catch(\Exception $e) {
	printf("** Catched %s: %s **\n", get_class($e), $e->getMessage());
}