Implement native HTTP application level caching (using currently implemented caching systems)

* To "bust" an HTTP cache, implement a timestamp GET parameter that changes whenever it should.
This commit is contained in:
Bofh 2022-12-17 18:07:13 +01:00
parent ba7102063e
commit f0c027e178
2 changed files with 18 additions and 0 deletions

View File

@ -1,3 +1,4 @@
<?php chdir('../../../../../../') ?> <?php chdir('../../../../../../') ?>
<?php require 'base.php' ?> <?php require 'base.php' ?>
<?php http_cache(60*30) ?>
<?php mod_php() ?> <?php mod_php() ?>

View File

@ -56,6 +56,10 @@ function apiresult($data, $code=200) {
if ($code !== 200) http_response_code($code); if ($code !== 200) http_response_code($code);
header('Content-Type: application/json'); header('Content-Type: application/json');
} }
if (isset($GLOBALS['http_cache_config']))
content_cache__put($GLOBALS['http_cache_config']['key'],
'ondemand,'.$GLOBALS['http_cache_config']['seconds'], $data);
if (gettype($data) !== 'string') if (gettype($data) !== 'string')
echo json_encode($data); echo json_encode($data);
else echo $data; else echo $data;
@ -611,6 +615,19 @@ function matches_comparing_expression($expr, $text, $debug=false) {
return in_array($result, [0,false]) ? false : true; return in_array($result, [0,false]) ? false : true;
} }
function http_cache($seconds, $data=null) {
if ($data === null)
$data = $_GET;
$cache_key = sha1('http_cache'.json_encode($data));
$cache = content_cache__get($cache_key);
if ($cache !== null)
return apiresult($cache);
$GLOBALS['http_cache_config'] = [
'seconds' => $seconds,
'key' => $cache_key
];
}
function _cachekey_construct(...$keys) { function _cachekey_construct(...$keys) {
$nkeys = []; $nkeys = [];
foreach ($keys as $key) foreach ($keys as $key)