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 // how long in minutes to put google/other instances on cooldown if they aren't responding
"request_cooldown" => 25, "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 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", e.g.: Preset the invidious instance URL: "instance_url" => "https://yewtu.be",

View file

@ -31,9 +31,9 @@
return false; return false;
} }
function store_cached_results($url, $results) { function store_cached_results($url, $results, $ttl = 0) {
if (function_exists("apcu_store") && !empty($results)) 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) { function fetch_cached_results($url) {

View file

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