Skip to content
Snippets Groups Projects
Commit ba85cb5b authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Merge branch '177-such-cache' into 'development'

Cache eingefügt. Im Standard werden Suchergebnisse von Suchmaschinen für 60 Minuten gechached.

Wenn der Wert geändert werden soll, kann man der Suchmaschine in der sumas.xml
das Attribut "cacheDuration" verpassen.
Dieses Attribut beinhaltet die Anzahl an Minuten für die der Cache vorgehalten werden soll.
ein Wert von 0 deaktiviert den Cache.
Closes #177

See merge request !252
parents 9dbb18b9 54cba58a
No related branches found
No related tags found
1 merge request!1365Resolve "Filter Options for MetaGer"
...@@ -4,9 +4,6 @@ namespace App\Http\Controllers; ...@@ -4,9 +4,6 @@ namespace App\Http\Controllers;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
#use App\MetaGer\Forwarder;
#use App\MetaGer\Results;
#use App\MetaGer\Search;
use App; use App;
use App\MetaGer; use App\MetaGer;
......
...@@ -596,18 +596,28 @@ class MetaGer ...@@ -596,18 +596,28 @@ class MetaGer
# aber natürlich nicht ewig. # aber natürlich nicht ewig.
# Die Verbindung steht zu diesem Zeitpunkt und auch unsere Request wurde schon gesendet. # Die Verbindung steht zu diesem Zeitpunkt und auch unsere Request wurde schon gesendet.
# Wir geben der Suchmaschine nun bis zu 500ms Zeit zu antworten. # Wir geben der Suchmaschine nun bis zu 500ms Zeit zu antworten.
$enginesToLoad = count($engines);
# Wir zählen die Suchmaschinen, die durch den Cache beantwortet wurden:
$enginesToLoad = 0;
$canBreak = false;
foreach($engines as $engine)
{
if( $engine->cached )
{
$enginesToLoad--;
if( $overtureEnabled && ( $engine->name === "overture" || $engine->name === "overtureAds" ) )
$canBreak = true;
}
}
$enginesToLoad += count($engines);
$loadedEngines = 0; $loadedEngines = 0;
$timeStart = microtime(true); $timeStart = microtime(true);
while( true ) while( true )
{ {
$time = (microtime(true) - $timeStart) * 1000; $time = (microtime(true) - $timeStart) * 1000;
$loadedEngines = intval(Redis::hlen('search.' . $this->getHashCode())); $loadedEngines = intval(Redis::hlen('search.' . $this->getHashCode()));
$canBreak = true; if( $overtureEnabled && (Redis::hexists('search.' . $this->getHashCode(), 'overture') || Redis::hexists('search.' . $this->getHashCode(), 'overtureAds')))
if( $overtureEnabled && !Redis::hexists('search.' . $this->getHashCode(), 'overture') && !Redis::hexists('search.' . $this->getHashCode(), 'overtureAds')) $canBreak = true;
$canBreak = false;
# Abbruchbedingung # Abbruchbedingung
if($time < 500) if($time < 500)
...@@ -625,7 +635,7 @@ class MetaGer ...@@ -625,7 +635,7 @@ class MetaGer
usleep(50000); usleep(50000);
} }
#exit;
foreach($engines as $engine) foreach($engines as $engine)
{ {
if(!$engine->loaded) if(!$engine->loaded)
......
...@@ -6,6 +6,7 @@ use Log; ...@@ -6,6 +6,7 @@ use Log;
use Redis; use Redis;
use App\Jobs\Search; use App\Jobs\Search;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Cache;
abstract class Searchengine abstract class Searchengine
...@@ -24,6 +25,7 @@ abstract class Searchengine ...@@ -24,6 +25,7 @@ abstract class Searchengine
public $write_time = 0; public $write_time = 0;
public $connection_time = 0; public $connection_time = 0;
public $loaded = false; public $loaded = false;
public $cached = false;
function __construct(\SimpleXMLElement $engine, MetaGer $metager) function __construct(\SimpleXMLElement $engine, MetaGer $metager)
{ {
...@@ -34,6 +36,9 @@ abstract class Searchengine ...@@ -34,6 +36,9 @@ abstract class Searchengine
$this->homepage = "https://metager.de"; $this->homepage = "https://metager.de";
$this->engine = $engine; $this->engine = $engine;
if( !isset($this->cacheDuration) )
$this->cacheDuration = 60;
# Wir registrieren die Benutzung dieser Suchmaschine # Wir registrieren die Benutzung dieser Suchmaschine
$this->uses = intval(Redis::hget($this->name, "uses")) + 1; $this->uses = intval(Redis::hget($this->name, "uses")) + 1;
Redis::hset($this->name, "uses", $this->uses); Redis::hset($this->name, "uses", $this->uses);
...@@ -73,14 +78,20 @@ abstract class Searchengine ...@@ -73,14 +78,20 @@ abstract class Searchengine
$q = $metager->getQ(); $q = $metager->getQ();
} }
$this->getString = $this->generateGetString($q, $metager->getUrl(), $metager->getLanguage(), $metager->getCategory()); $this->getString = $this->generateGetString($q, $metager->getUrl(), $metager->getLanguage(), $metager->getCategory());
$this->hash = $metager->getHashCode(); $this->hash = md5($this->host . $this->getString . $this->port . $this->name);
$this->resultHash = $metager->getHashCode();
# Die Anfragen an die Suchmaschinen werden nun von der Laravel-Queue bearbeitet: if( Cache::has($this->hash) )
# Hinweis: solange in der .env der QUEUE_DRIVER auf "sync" gestellt ist, werden die Abfragen {
# nacheinander abgeschickt. $this->cached = true;
# Sollen diese Parallel verarbeitet werden, muss ein anderer QUEUE_DRIVER verwendet werden. }else
# siehe auch: https://laravel.com/docs/5.2/queues {
$this->dispatch(new Search($this->hash, $this->host, $this->port, $this->name, $this->getString, $this->useragent, $metager->getSumaFile())); # Die Anfragen an die Suchmaschinen werden nun von der Laravel-Queue bearbeitet:
# Hinweis: solange in der .env der QUEUE_DRIVER auf "sync" gestellt ist, werden die Abfragen
# nacheinander abgeschickt.
# Sollen diese Parallel verarbeitet werden, muss ein anderer QUEUE_DRIVER verwendet werden.
# siehe auch: https://laravel.com/docs/5.2/queues
$this->dispatch(new Search($this->resultHash, $this->host, $this->port, $this->name, $this->getString, $this->useragent, $metager->getSumaFile()));
}
} }
public abstract function loadResults($result); public abstract function loadResults($result);
...@@ -136,16 +147,26 @@ abstract class Searchengine ...@@ -136,16 +147,26 @@ abstract class Searchengine
public function retrieveResults() public function retrieveResults()
{ {
if( Redis::hexists('search.' . $this->hash, $this->name)) $body = "";
if( $this->cacheDuration > 0 && Cache::has($this->hash) )
{
$body = Cache::get($this->hash);
}elseif ( Redis::hexists('search.' . $this->resultHash, $this->name) ) {
$body = Redis::hget('search.' . $this->resultHash, $this->name);
if( $this->cacheDuration > 0 )
Cache::put($this->hash, $body, $this->cacheDuration);
}
if( $body !== "" )
{ {
$body = Redis::hget('search.' . $this->hash, $this->name);
$this->loadResults($body); $this->loadResults($body);
$this->loaded = true; $this->loaded = true;
Redis::hdel('search.' . $this->hash, $this->name); Redis::hdel('search.' . $this->hash, $this->name);
return true; return true;
} }else
return false; {
return false;
}
} }
public function shutdown() public function shutdown()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment