Add ttl on caches

This commit is contained in:
davidovski 2023-08-30 13:58:47 +01:00
parent a8c4f4e609
commit 4488200d22
3 changed files with 9 additions and 9 deletions

View File

@ -23,6 +23,9 @@
// how long in minutes to put google/other instances on cooldown if they aren't responding
"request_cooldown" => 25,
// how long in minutes to store results for in the cache
"cache_time" => 20,
/*
Preset privacy friendly frontends for users, these can be overwritten by users in the settings
e.g.: Preset the invidious instance URL: "instance_url" => "https://yewtu.be",

View File

@ -31,9 +31,9 @@
return false;
}
function store_cached_results($url, $results) {
function store_cached_results($url, $results, $ttl = 0) {
if (function_exists("apcu_store") && !empty($results))
return apcu_store("cached:$url", $results);
return apcu_store("cached:$url", $results, $ttl);
}
function fetch_cached_results($url) {

View File

@ -38,10 +38,8 @@
if (!isset($this->url))
return $this->parse_results(null);
if ($this->DO_CACHING && has_cached_results($this->url)) {
error_log("used cache for $this->url");
if ($this->DO_CACHING && has_cached_results($this->url))
return fetch_cached_results($this->url);
}
if (!isset($this->ch))
return $this->parse_results(null);
@ -49,10 +47,8 @@
$response = curl_multi_getcontent($this->ch);
$results = $this->parse_results($response) ?? array();
if ($this->DO_CACHING) {
store_cached_results($this->url, $results);
error_log("caching $this->url in cache");
}
if ($this->DO_CACHING)
store_cached_results($this->url, $results, $this->opts->cache_time * 60);
return $results;
}
@ -64,6 +60,7 @@
$opts = require "config.php";
$opts->request_cooldown ??= 25;
$opts->cache_time ??= 25;
$opts->query = trim($_REQUEST["q"] ?? "");
$opts->type = (int) ($_REQUEST["t"] ?? 0);