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

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.
parent 4db335b1
Branches
No related tags found
3 merge requests!261Development,!255Development,!252Cache eingefügt. Im Standard werden Suchergebnisse von Suchmaschinen für 60 Minuten gechached.
......@@ -4,9 +4,6 @@ namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
#use App\MetaGer\Forwarder;
#use App\MetaGer\Results;
#use App\MetaGer\Search;
use App;
use App\MetaGer;
......
......@@ -596,18 +596,28 @@ class MetaGer
# aber natürlich nicht ewig.
# Die Verbindung steht zu diesem Zeitpunkt und auch unsere Request wurde schon gesendet.
# 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;
$timeStart = microtime(true);
while( true )
{
$time = (microtime(true) - $timeStart) * 1000;
$loadedEngines = intval(Redis::hlen('search.' . $this->getHashCode()));
$canBreak = true;
if( $overtureEnabled && !Redis::hexists('search.' . $this->getHashCode(), 'overture') && !Redis::hexists('search.' . $this->getHashCode(), 'overtureAds'))
$canBreak = false;
if( $overtureEnabled && (Redis::hexists('search.' . $this->getHashCode(), 'overture') || Redis::hexists('search.' . $this->getHashCode(), 'overtureAds')))
$canBreak = true;
# Abbruchbedingung
if($time < 500)
......@@ -625,7 +635,7 @@ class MetaGer
usleep(50000);
}
#exit;
foreach($engines as $engine)
{
if(!$engine->loaded)
......
......@@ -6,6 +6,7 @@ use Log;
use Redis;
use App\Jobs\Search;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Cache;
abstract class Searchengine
......@@ -24,6 +25,7 @@ abstract class Searchengine
public $write_time = 0;
public $connection_time = 0;
public $loaded = false;
public $cached = false;
function __construct(\SimpleXMLElement $engine, MetaGer $metager)
{
......@@ -34,6 +36,9 @@ abstract class Searchengine
$this->homepage = "https://metager.de";
$this->engine = $engine;
if( !isset($this->cacheDuration) )
$this->cacheDuration = 60;
# Wir registrieren die Benutzung dieser Suchmaschine
$this->uses = intval(Redis::hget($this->name, "uses")) + 1;
Redis::hset($this->name, "uses", $this->uses);
......@@ -73,14 +78,20 @@ abstract class Searchengine
$q = $metager->getQ();
}
$this->getString = $this->generateGetString($q, $metager->getUrl(), $metager->getLanguage(), $metager->getCategory());
$this->hash = $metager->getHashCode();
# 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->hash, $this->host, $this->port, $this->name, $this->getString, $this->useragent, $metager->getSumaFile()));
$this->hash = md5($this->host . $this->getString . $this->port . $this->name);
$this->resultHash = $metager->getHashCode();
if( Cache::has($this->hash) )
{
$this->cached = true;
}else
{
# 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);
......@@ -136,16 +147,26 @@ abstract class Searchengine
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->loaded = true;
Redis::hdel('search.' . $this->hash, $this->name);
return true;
}
return false;
}else
{
return false;
}
}
public function shutdown()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment