LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

commit 4488200d225f0dc1ab05b256168079008c2636d5
parent a8c4f4e6090b0bec609d39d4fbb8614ddee2ecab
Author: davidovski <david@davidovski.xyz>
Date: Wed, 30 Aug 2023 13:58:47 +0100

Add ttl on caches

Diffstat:
Mconfig.php.example | 3+++
Mmisc/cooldowns.php | 4++--
Mmisc/search_engine.php | 11++++-------

3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/config.php.example b/config.php.example
@@ -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",
diff --git a/misc/cooldowns.php b/misc/cooldowns.php
@@ -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) {
diff --git a/misc/search_engine.php b/misc/search_engine.php
@@ -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);