dweet/redis.php
2018-07-19 17:43:50 +02:00

65 lines
1.3 KiB
PHP

<?php
function g($key) {
if (isset($_GET[$key])) return $_GET[$key];
else return false;
}
function _die($type, $msg) {
header('Content-Type: application/json');
die(json_encode(array($type => $msg)));
}
function edie($msg) {
_die("error", $msg);
}
function sdie($msg) {
_die("success", $msg);
}
function ddie($msg) {
_die("data", $msg);
}
function get_redis($key) {
$client = new Predis\Client();
ddie($client->get($key));
}
function set_redis($key, $val) {
$client = new Predis\Client();
$client->set($key, $val);
$client->expire($key, 86400); // 1d
sdie("ok");
}
require 'vendor/autoload.php';
Predis\Autoloader::register();
if (g("action") === "get") {
$key = g("key");
if ($key) {
// get redis key
get_redis($key);
} else {
// no key
edie("no key given");
}
} elseif (g("action") === "set") {
$key = g("key");
if ($key) {
$val = g("val");
if ($val) {
// set redis val for key
set_redis($key, $val);
} else {
$data = file_get_contents('php://input');
if (!$data) {
edie("no val given");
} elseif (count($data) > 500) {
edie("data too long");
} else {
set_redis($key, $data);
}
}
} else {
// no key
edie("no key given");
}
} else {
edie("no action given");
}
?>