Skip to content

Instantly share code, notes, and snippets.

@AlexisTM
Last active January 31, 2018 15:57
Show Gist options
  • Save AlexisTM/7707f4db84cd040e51a613f18ac42891 to your computer and use it in GitHub Desktop.
Save AlexisTM/7707f4db84cd040e51a613f18ac42891 to your computer and use it in GitHub Desktop.
cache_set.php for Medium post
<?php
include "json.php";
use \Simple\json;
function cache_set($key, $val) {
 $val = var_export($val, true);
 $val = str_replace("stdClass::__set_state", "(object)", $val);
 $tmp = "/tmp/$key." . uniqid("", true) . ".tmp";
 file_put_contents($tmp, "<?php $val = " . $val . ";", LOCK_EX);
 rename($tmp, "/tmp/$key");
}
$response = new \Simple\json();
if(isset($_POST["name"])){
 $name = $_POST["name"];
} else if(isset($_GET["name"])) {
 $name = $_GET["name"];
} else {
 $response->success = False;
 $response->send();
 exit();
}
$response->name = $name;
if(isset($_POST["data"])){
 $data = $_POST["data"];
} else if(isset($_GET["data"])) {
 $data = $_GET["data"];
} else {
 $response->success = False;
 $response->send();
 exit();
}
cache_set($name, $data);
$response->success = True;
$response->send();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment