From 816f3429a300212af1cb3a867fda1fffd830f9e7 Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Thu, 6 Feb 2020 13:13:35 +0100
Subject: [PATCH] reduced requests to the cache

---
 app/MetaGer.php             | 26 ++++++++++++++++++++++++++
 app/Models/Searchengine.php | 17 ++---------------
 2 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/app/MetaGer.php b/app/MetaGer.php
index 93c4e927a..550755979 100644
--- a/app/MetaGer.php
+++ b/app/MetaGer.php
@@ -634,10 +634,36 @@ class MetaGer
 
     public function startSearch(&$timings)
     {
+        if (!empty($timings)) {
+            $timings["startSearch"]["start"] = microtime(true) - $timings["starttime"];
+        }
+
+        # Check all engines for Cached responses
+        if ($this->canCache()) {
+            $keys = [];
+            foreach ($this->engines as $engine) {
+                $keys[] = $engine->hash;
+            }
+            $cacheValues = Cache::many($keys);
+            foreach ($this->engines as $engine) {
+                if ($cacheValues[$engine->hash] !== null) {
+                    $engine->cached = true;
+                    $engine->retrieveResults($this, $cacheValues[$engine->hash]);
+                }
+            }
+        }
+        if (!empty($timings)) {
+            $timings["startSearch"]["cache checked"] = microtime(true) - $timings["starttime"];
+        }
+
         # Wir starten alle Suchen
         foreach ($this->engines as $engine) {
             $engine->startSearch($this, $timings);
         }
+        if (!empty($timings)) {
+            $timings["startSearch"]["searches started"] = microtime(true) - $timings["starttime"];
+        }
+
     }
 
     # Spezielle Suchen und Sumas
diff --git a/app/Models/Searchengine.php b/app/Models/Searchengine.php
index 614faded6..ef364a50c 100644
--- a/app/Models/Searchengine.php
+++ b/app/Models/Searchengine.php
@@ -3,7 +3,6 @@
 namespace App\Models;
 
 use App\MetaGer;
-use Cache;
 use Illuminate\Support\Facades\Redis;
 
 abstract class Searchengine
@@ -111,17 +110,7 @@ abstract class Searchengine
             $timings["startSearch"][$this->name]["start"] = microtime(true) - $timings["starttime"];
         }
 
-        if ($this->canCache && Cache::has($this->hash)) {
-            if (!empty($timings)) {
-                $timings["startSearch"][$this->name]["checked cache"] = microtime(true) - $timings["starttime"];
-            }
-            $this->cached = true;
-            $this->retrieveResults($metager, true);
-            if (!empty($timings)) {
-                $timings["startSearch"][$this->name]["retrieved results"] = microtime(true) - $timings["starttime"];
-            }
-
-        } else {
+        if (!$this->cached) {
             if (!empty($timings)) {
                 $timings["startSearch"][$this->name]["checked cache"] = microtime(true) - $timings["starttime"];
             }
@@ -193,15 +182,13 @@ abstract class Searchengine
     }
 
     # Fragt die Ergebnisse von Redis ab und lädt Sie
-    public function retrieveResults(MetaGer $metager)
+    public function retrieveResults(MetaGer $metager, $body = null)
     {
         if ($this->loaded) {
             return true;
         }
 
-        $body = null;
         if ($this->cached) {
-            $body = Cache::get($this->hash);
             if ($body === "no-result") {
                 $body = "";
             }
-- 
GitLab