dweet/redis.php

65 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 {
2018-07-19 17:43:50 +02:00
$data = file_get_contents('php://input');
if (!$data) {
2018-07-18 12:31:42 +02:00
edie("no val given");
2018-07-19 17:47:14 +02:00
} elseif (strlen($data) > 500) {
2018-07-19 17:43:50 +02:00
edie("data too long");
2018-07-18 12:31:42 +02:00
} else {
set_redis($key, $data);
}
}
} else {
// no key
edie("no key given");
}
} else {
edie("no action given");
}
?>