Changed caching to APCU (fixed #1)

This commit is contained in:
Markus Koch 2016-08-19 20:31:08 +02:00
parent ccd7c65d16
commit a04cdd7703

View File

@ -37,12 +37,12 @@
else
$selectedServer = "konachan";
$tag = $_GET['tag'];
$santag = checkTag($tag);
$tempfile = "/tmp/konasub/$selectedServer/$santag.xml";
$selfUrl = "https://shimatta.de/konasub/?tag=$tag";
$tag = $_GET['tag'];
$santag = checkTag($tag);
$cacheName = "konasub_$selectedServer_$santag";
$selfUrl = "https://shimatta.de/konasub/?tag=$tag";
dbg ("Tag: \"$tag\" ($santag, $tempfile)\n");
mkdir ("/tmp/konasub/$selectedServer");
// Configure server urls and meta data
$svr['konachan']['imgBase'] = "http://konachan.com/post/show/";
@ -52,20 +52,14 @@
$svr['konachan']['link'] = "http://konachan.com/post?tags=" . urlencode($tag);
$svr['konachan']['re'] = "/<post.* author=\\\"(?'author'[^\\ ]*)\\\".* created_at=\\\"(?'created_at'[^\\ ]*)\\\".* id=\\\"(?'id'[^\\ ]*)\\\".* jpeg_url=\\\"(?'jpeg_url'[^\\ ]*)\\\".* tags=\\\"(?'tags'[^\\\"]*)\\\"" . " .*\\/>/";
dbg ("File mtime delta: " . (time() - filemtime($tempfile)) . " (of $refreshTime)");
if ((time() - filemtime($tempfile)) > $refreshTime) {
$cacheFile = fopen($tempfile, "w");
dbg (" -> Retrieving file...");
if ($data = apcu_fetch($cacheName)) {
dbg (" -> Loading cached data...");
}
else {
dbg (" -> Retrieving latest data from server...");
$data = file_get_contents($svr[$selectedServer]['url']);
fwrite($cacheFile, $data);
flush();
apcu_store($cacheName, $data, $refreshTime);
}
else {
$cacheFile = fopen($tempfile, "r");
dbg (" -> Loading cached file...");
$data = fread($cacheFile,filesize($tempfile));
}
fclose($cacheFile);
dbg("BEGIN XML:\n");