diff --git a/backend-php/include/config.php b/backend-php/include/config.php
index c671f69..e3d5e15 100644
--- a/backend-php/include/config.php
+++ b/backend-php/include/config.php
@@ -1,5 +1,21 @@
REDIS,
+
+/*----------------------------------------------------------------------------*\
+| MEMCACHED SPECIFIC SETTINGS |
+\*----------------------------------------------------------------------------*/
+
// Connection to memcached for data storage. To connect via UNIX socket instead
// of TCP, set host to 'unix:///path/to/memcached.sock' and port to 0.
"memcached_host" => 'localhost',
@@ -18,6 +34,28 @@
// shared memcached instance or run multiple instances of Hauk.
"memcached_prefix" => 'hauk',
+/*----------------------------------------------------------------------------*\
+| REDIS SPECIFIC SETTINGS |
+\*----------------------------------------------------------------------------*/
+
+// Connection to Redis for data storage. To connect via UNIX socket instead of
+// TCP, set host to '/path/to/redis.sock'.
+"redis_host" => 'localhost',
+"redis_port" => 6379,
+
+// If you use password authentication in Redis, set `redis_use_auth` to true and
+// enter the password in `redis_auth`.
+"redis_use_auth" => false,
+"redis_auth" => '',
+
+// A prefix to use for all variables sent to Redis. Useful if you have a shared
+// Redis instance or run multiple instances of Hauk.
+"redis_prefix" => 'hauk',
+
+/*----------------------------------------------------------------------------*\
+| GENERAL SETTINGS |
+\*----------------------------------------------------------------------------*/
+
// A hashed password that is required for creating sessions and posting location
// data to Hauk. To generate this value on the terminal:
// - MD5 (insecure!): openssl passwd -1
diff --git a/backend-php/include/inc.php b/backend-php/include/inc.php
index 6d17df6..fe6476c 100644
--- a/backend-php/include/inc.php
+++ b/backend-php/include/inc.php
@@ -21,6 +21,9 @@ const LINK_ID_RAND_BYTES = 32;
const GROUP_PIN_MIN = 100000;
const GROUP_PIN_MAX = 999999;
+const MEMCACHED = 0;
+const REDIS = 1;
+
const KILOMETERS_PER_HOUR = array(
// Relative distance per second multiplied by number of seconds per hour.
"mpsMultiplier" => 3.6,
@@ -42,6 +45,7 @@ const DEFAULTS = array(
// This is the default config. This file is used as a fallback for missing
// options in config.php.
+ "storage_backend" => MEMCACHED,
"memcached_host" => 'localhost',
"memcached_port" => 11211,
"memcached_binary" => false,
@@ -49,6 +53,11 @@ const DEFAULTS = array(
"memcached_sasl_user" => "",
"memcached_sasl_pass" => "",
"memcached_prefix" => 'hauk',
+ "redis_host" => 'localhost',
+ "redis_port" => 6379,
+ "redis_use_auth" => false,
+ "redis_auth" => '',
+ "redis_prefix" => 'hauk',
"password_hash" => '$2y$10$4ZP1iY8A3dZygXoPgsXYV.S3gHzBbiT9nSfONjhWrvMxVPkcFq1Ka',
"map_tile_uri" => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
"map_attribution" => 'Map data © OpenStreetMap contributors, CC-BY-SA',
@@ -91,9 +100,9 @@ function getConfig($item) {
if (array_key_exists($item, DEFAULTS)) return DEFAULTS[$item];
}
-define("PREFIX_SESSION", getConfig("memcached_prefix")."-session-");
-define("PREFIX_LOCDATA", getConfig("memcached_prefix")."-locdata-");
-define("PREFIX_GROUPID", getConfig("memcached_prefix")."-groupid-");
+define("PREFIX_SESSION", "-session-");
+define("PREFIX_LOCDATA", "-locdata-");
+define("PREFIX_GROUPID", "-groupid-");
// A base class for location shares. Shares contain a reference to all sessions
// that broadcasts location data to the share, but does not contain the location
@@ -555,16 +564,30 @@ function requirePOST(...$args) {
// Hauk maintains compatibility with both `memcache` and `memcached`, meaning we
// need an abstraction layer between Hauk and the extensions to ensure a unified
// interface for both. This code loads the correct memcache wrapper.
-if (extension_loaded("memcached")) {
- include_once(__DIR__."/wrapper/memcached.php");
-} else if (extension_loaded("memcache")) {
- include_once(__DIR__."/wrapper/memcache.php");
-} else {
- die("No compatible memcached extension (memcache or memcached) is enabled in your PHP config!\n");
+
+switch (getConfig("storage_backend")) {
+ case MEMCACHED:
+ if (extension_loaded("memcached")) {
+ include_once(__DIR__."/wrapper/memcached.php");
+ } else if (extension_loaded("memcache")) {
+ include_once(__DIR__."/wrapper/memcache.php");
+ } else {
+ die("No compatible memcached extension (memcache or memcached) is enabled in your PHP config!\n");
+ }
+ break;
+ case REDIS:
+ if (extension_loaded("redis")) {
+ include_once(__DIR__."/wrapper/redis.php");
+ } else {
+ die("No compatible redis extension (redis) is enabled in your PHP config!\n");
+ }
+ break;
+ default:
+ die("You have set an invalid storage_backend in Hauk!");
}
+
// Returns a memcached instance.
function memConnect() {
- $memcache = new MemWrapper(getConfig("memcached_host"), getConfig("memcached_port"));
- return $memcache;
+ return new MemWrapper();
}
diff --git a/backend-php/include/wrapper/memcache.php b/backend-php/include/wrapper/memcache.php
index acb3e39..6475ee0 100644
--- a/backend-php/include/wrapper/memcache.php
+++ b/backend-php/include/wrapper/memcache.php
@@ -8,28 +8,24 @@
class MemWrapper {
private $memcache = null;
- function __construct($host, $port) {
+ function __construct() {
$this->memcache = new Memcache();
$this->memcache->connect(getConfig("memcached_host"), getConfig("memcached_port"))
or die ("Server could not connect to memcached!\n");
}
function get($key) {
- $data = $this->memcache->get($key);
+ $data = $this->memcache->get(getConfig("memcached_prefix").$key);
if ($data === false) return false;
return json_decode($data, true);
}
function set($key, $data, $expire) {
- $this->memcache->set($key, json_encode($data), 0, $expire);
- }
-
- function replace($key, $data, $expire) {
- $this->memcache->replace($key, json_encode($data), 0, $expire);
+ $this->memcache->set(getConfig("memcached_prefix").$key, json_encode($data), 0, $expire);
}
function delete($key) {
- $this->memcache->delete($key);
+ $this->memcache->delete(getConfig("memcached_prefix").$key);
}
}
diff --git a/backend-php/include/wrapper/memcached.php b/backend-php/include/wrapper/memcached.php
index e6f2193..3eba8a2 100644
--- a/backend-php/include/wrapper/memcached.php
+++ b/backend-php/include/wrapper/memcached.php
@@ -8,7 +8,7 @@
class MemWrapper {
private $memcache = null;
- function __construct($host, $port) {
+ function __construct() {
// If the hostname starts with "unix://", this is a UNIX socket.
// The `memcached` extension only needs the full pathname and not the
// unix scheme, so we strip the scheme from the path.
@@ -27,21 +27,17 @@ class MemWrapper {
}
function get($key) {
- $data = $this->memcache->get($key);
+ $data = $this->memcache->get(getConfig("memcached_prefix").$key);
if ($data === false) return false;
return json_decode($data, true);
}
function set($key, $data, $expire) {
- $this->memcache->set($key, json_encode($data), $expire);
- }
-
- function replace($key, $data) {
- $this->memcache->replace($key, json_encode($data), $expire);
+ $this->memcache->set(getConfig("memcached_prefix").$key, json_encode($data), $expire);
}
function delete($key) {
- $this->memcache->delete($key);
+ $this->memcache->delete(getConfig("memcached_prefix").$key);
}
}
diff --git a/backend-php/include/wrapper/redis.php b/backend-php/include/wrapper/redis.php
new file mode 100644
index 0000000..1b1d538
--- /dev/null
+++ b/backend-php/include/wrapper/redis.php
@@ -0,0 +1,34 @@
+redis = new Redis();
+ if (getConfig("redis_use_auth")) {
+ $this->redis->auth(getConfig("redis_auth"))
+ or die("Incorrect Redis authentication!");
+ }
+ $this->redis->connect(getConfig("redis_host"), getConfig("redis_port"))
+ or die ("Server could not connect to Redis!\n");
+ }
+
+ function get($key) {
+ $data = $this->redis->get(getConfig("redis_prefix").$key);
+ if ($data === false) return false;
+ return json_decode($data, true);
+ }
+
+ function set($key, $data, $expire) {
+ $this->redis->setEx(getConfig("redis_prefix").$key, $expire - time(), json_encode($data));
+ }
+
+ function delete($key) {
+ $this->redis->del(getConfig("redis_prefix").$key);
+ }
+}
+
+?>