Commit 138438ae authored by Dominik Hebeler's avatar Dominik Hebeler

Merge branch '990-make-the-cache-connection-optional' into 'development'

Resolve "Make the Cache connection optional"

Closes #990

See merge request !1630
parents 0985e888 0b4fbc9d
......@@ -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);
}
......
......@@ -7,6 +7,7 @@ use Captcha;
use Closure;
use Cookie;
use Illuminate\Http\Response;
use Log;
use URL;
class HumanVerification
......@@ -66,7 +67,6 @@ class HumanVerification
} else {
$user = $users[$uid];
}
# Lock out everyone in a Bot network
# Find out how many requests this IP has made
$sum = 0;
......@@ -130,14 +130,21 @@ class HumanVerification
}
}
} catch (\Exception $e) {
Log::error($e->getMessage());
} finally {
if ($update) {
if ($update && $user != null) {
if ($user["whitelist"]) {
$user["expiration"] = now()->addWeeks(2);
} else {
$user["expiration"] = now()->addHours(72);
}
$this->setUser($prefix, $user);
try {
$this->setUser($prefix, $user);
\App\PrometheusExporter::HumanVerificationSuccessfull();
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}
......@@ -148,7 +155,6 @@ class HumanVerification
public function setUser($prefix, $user)
{
// Lock must be acquired within 2 seconds
$userList = Cache::get($prefix . "." . $user["id"], []);
$userList[$user["uid"]] = $user;
Cache::put($prefix . "." . $user["id"], $userList, 2 * 7 * 24 * 60 * 60);
......
......@@ -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);
}
......
......@@ -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();
......
......@@ -2,24 +2,34 @@
namespace App;
class PrometheusExporter {
class PrometheusExporter
{
public static function CaptchaShown() {
public static function CaptchaShown()
{
$registry = \Prometheus\CollectorRegistry::getDefault();
$counter = $registry->getOrRegisterCounter('metager', 'captcha_shown', 'counts how often the captcha was shown', []);
$counter->inc();
}
public static function CaptchaCorrect() {
public static function CaptchaCorrect()
{
$registry = \Prometheus\CollectorRegistry::getDefault();
$counter = $registry->getOrRegisterCounter('metager', 'captcha_correct', 'counts how often the captcha was solved correctly', []);
$counter->inc();
}
public static function CaptchaAnswered() {
public static function CaptchaAnswered()
{
$registry = \Prometheus\CollectorRegistry::getDefault();
$counter = $registry->getOrRegisterCounter('metager', 'captcha_answered', 'counts how often the captcha was answered', []);
$counter->inc();
}
}
\ No newline at end of file
public static function HumanVerificationSuccessfull()
{
$registry = \Prometheus\CollectorRegistry::getDefault();
$counter = $registry->getOrRegisterCounter('metager', 'humanverification', 'counts how often humanverification middleware was successfull', []);
$counter->inc();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment