summaryrefslogtreecommitdiffstats
path: root/redis.php
diff options
context:
space:
mode:
Diffstat (limited to 'redis.php')
-rw-r--r--redis.php69
1 files changed, 69 insertions, 0 deletions
diff --git a/redis.php b/redis.php
new file mode 100644
index 0000000..aacc581
--- /dev/null
+++ b/redis.php
@@ -0,0 +1,69 @@
+<?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");
+}
+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());
+}
+