dweet/redis.php

64 lines
1.3 KiB
PHP
Raw Normal View History

2018-07-18 12:31:42 +02:00
<?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 = json_decode(file_get_contents('php://input'), true);
if ($data === NULL) {
// got key but no val
edie("no val given");
} else {
set_redis($key, $data);
}
}
} else {
// no key
edie("no key given");
}
} else {
edie("no action given");
}
?>