mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-01 22:00:31 +02:00
add really fast track cache script
This commit is contained in:
60
webroot/imgf.php
Normal file
60
webroot/imgf.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Fast track cache, read entries from the cache before processing image
|
||||||
|
* the ordinary way.
|
||||||
|
*/
|
||||||
|
// Include debug functions
|
||||||
|
function debug($msg)
|
||||||
|
{
|
||||||
|
$file = "/tmp/cimage";
|
||||||
|
if (!is_writable($file)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$msg .= ":" . count(get_included_files());
|
||||||
|
$msg .= ":" . round(memory_get_peak_usage()/1024/1024, 3) . "MB";
|
||||||
|
$msg .= ":" . (string) round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 6) . "ms";
|
||||||
|
file_put_contents($file, "$msg\n", FILE_APPEND);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$query = $_GET;
|
||||||
|
|
||||||
|
// Remove parts from querystring that should not be part of filename
|
||||||
|
$clear = array("nc", "no-cache");
|
||||||
|
foreach ($clear as $value) {
|
||||||
|
unset($query[$value]);
|
||||||
|
}
|
||||||
|
|
||||||
|
arsort($query);
|
||||||
|
$queryAsString = http_build_query($query);
|
||||||
|
|
||||||
|
$filename = md5($queryAsString);
|
||||||
|
if (is_readable($filename)) {
|
||||||
|
$item = json_decode(file_get_contents($filename), true);
|
||||||
|
|
||||||
|
if (is_readable($item["source"])) {
|
||||||
|
foreach ($item["header"] as $value) {
|
||||||
|
header($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])
|
||||||
|
&& strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) == $item["last-modified"]) {
|
||||||
|
header("HTTP/1.0 304 Not Modified");
|
||||||
|
debug("really fast track 304");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($item["header-output"] as $value) {
|
||||||
|
header($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
readfile($item["source"]);
|
||||||
|
debug("really fast track 200");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// No fast track cache, proceed as usual
|
||||||
|
include __DIR__ . "/img.php";
|
Reference in New Issue
Block a user