From babf8e73a3a314411ea78d31f4d693f5287e6cc7 Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Fri, 28 Aug 2020 10:26:27 +0200
Subject: [PATCH] Cache is now optional in every step of metager search

---
 app/Http/Controllers/MetaGerSearch.php | 21 +++++++++++++++------
 app/Models/Quicktips/Quicktips.php     | 19 +++++++++++++++----
 app/Models/Searchengine.php            |  9 +++++++--
 3 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/app/Http/Controllers/MetaGerSearch.php b/app/Http/Controllers/MetaGerSearch.php
index 4d594cb5b..55589b09c 100644
--- a/app/Http/Controllers/MetaGerSearch.php
+++ b/app/Http/Controllers/MetaGerSearch.php
@@ -7,6 +7,7 @@ use App\MetaGer;
 use Cache;
 use Illuminate\Http\Request;
 use LaravelLocalization;
+use Log;
 use View;
 
 class MetaGerSearch extends Controller
@@ -63,7 +64,7 @@ class MetaGerSearch extends Controller
 
         # Search query can be empty after parsing the formdata
         # we will cancel the search in that case and show an error to the user
-        if(empty($metager->getQ())){
+        if (empty($metager->getQ())) {
             return $metager->createView();
         }
 
@@ -109,7 +110,11 @@ class MetaGerSearch extends Controller
             }
         }
 
-        Cache::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 60 * 60);
+        try {
+            Cache::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 60 * 60);
+        } catch (\Exception $e) {
+            Log::error($e->getMessage());
+        }
         if (!empty($timings)) {
             $timings["Filled resultloader Cache"] = microtime(true) - $time;
         }
@@ -117,7 +122,11 @@ class MetaGerSearch extends Controller
         # Die Ausgabe erstellen:
         $resultpage = $metager->createView();
         if ($spamEntry !== null) {
-            Cache::put('spam.' . $metager->getFokus() . "." . md5($spamEntry), $resultpage->render(), 604800);
+            try {
+                Cache::put('spam.' . $metager->getFokus() . "." . md5($spamEntry), $resultpage->render(), 604800);
+            } catch (\Exception $e) {
+                Log::error($e->getMessage());
+            }
         }
 
         if (!empty($timings)) {
@@ -133,7 +142,7 @@ class MetaGerSearch extends Controller
         $counter->incBy(sizeof($metager->getResults()));
         $counter = $registry->getOrRegisterCounter('metager', 'query_counter', 'counts total number of search queries', []);
         $counter->inc();
-        
+
         return $resultpage;
     }
 
@@ -225,7 +234,7 @@ class MetaGerSearch extends Controller
 
         $result["finished"] = $finished;
 
-        if($newResults > 0){
+        if ($newResults > 0) {
             $registry = \Prometheus\CollectorRegistry::getDefault();
             $counter = $registry->getOrRegisterCounter('metager', 'result_counter', 'counts total number of returned results', []);
             $counter->incBy($newResults);
@@ -290,7 +299,7 @@ class MetaGerSearch extends Controller
     {
         $search = $request->input('search', '');
         $quotes = $request->input('quotes', 'on');
-        if(empty($search)){
+        if (empty($search)) {
             abort(404);
         }
 
diff --git a/app/Models/Quicktips/Quicktips.php b/app/Models/Quicktips/Quicktips.php
index ba558afcf..1210568fe 100644
--- a/app/Models/Quicktips/Quicktips.php
+++ b/app/Models/Quicktips/Quicktips.php
@@ -32,12 +32,23 @@ class Quicktips
         $url = $this->quicktipUrl . "?search=" . $this->normalize_search($search) . "&locale=" . $locale . "&quotes=" . $quotes;
         $this->hash = md5($url);
 
-        if (!Cache::has($this->hash)) {
+        $results = null;
+
+        try {
+            if (!Cache::has($this->hash)) {
+                $results = file_get_contents($url);
+                Cache::put($this->hash, $results, Quicktips::CACHE_DURATION);
+            } else {
+                $results = Cache::get($this->hash);
+            }
+        } catch (\Exception $e) {
+            Log::error($e->getMessage());
+        }
+
+        if ($results === null) {
             $results = file_get_contents($url);
-            Cache::put($this->hash, $results, Quicktips::CACHE_DURATION);
-        } else {
-            $results = Cache::get($this->hash);
         }
+
         $this->results = $this->loadResults($results);
     }
 
diff --git a/app/Models/Searchengine.php b/app/Models/Searchengine.php
index eba7c83bd..9cafcf0c6 100644
--- a/app/Models/Searchengine.php
+++ b/app/Models/Searchengine.php
@@ -5,6 +5,7 @@ namespace App\Models;
 use App\MetaGer;
 use Cache;
 use Illuminate\Support\Facades\Redis;
+use Log;
 
 abstract class Searchengine
 {
@@ -94,7 +95,7 @@ abstract class Searchengine
             $tmpPara = true;
             $engineParameterKey = $filter->sumas->{$name}->{"get-parameter"};
             $engineParameterValue = $filter->sumas->{$name}->values->{$inputParameter};
-            if(stripos($engineParameterValue, "dyn-") === 0){
+            if (stripos($engineParameterValue, "dyn-") === 0) {
                 $functionname = substr($engineParameterValue, stripos($engineParameterValue, "dyn-") + 4);
                 $engineParameterValue = \App\DynamicEngineParameters::$functionname();
             }
@@ -207,7 +208,11 @@ abstract class Searchengine
         }
 
         if ($body !== null) {
-            Cache::put($this->hash, $body, $this->cacheDuration * 60);
+            try {
+                Cache::put($this->hash, $body, $this->cacheDuration * 60);
+            } catch (\Exception $e) {
+                Log::error($e->getMessage());
+            }
             $this->loadResults($body);
             $this->getNext($metager, $body);
             $this->markNew();
-- 
GitLab