diff --git a/app/Http/Controllers/MetaGerSearch.php b/app/Http/Controllers/MetaGerSearch.php index d1b179e30ee8b9040ea413a9ff117767eb76903a..b659fe161aef9e5303eec84dac9221337b42f29c 100644 --- a/app/Http/Controllers/MetaGerSearch.php +++ b/app/Http/Controllers/MetaGerSearch.php @@ -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; diff --git a/app/MetaGer.php b/app/MetaGer.php index 8f3e4678419bc2e830a21f8897abacf9e730efae..c0c04a24adf024ec7685f2569287e30e27adbf77 100644 --- a/app/MetaGer.php +++ b/app/MetaGer.php @@ -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) diff --git a/app/Models/Searchengine.php b/app/Models/Searchengine.php index 8f0deb05772110d47676444ef3c8667f13a53bd3..42337d285dc44b91f8ea23a732b3dfe85e83d4ff 100644 --- a/app/Models/Searchengine.php +++ b/app/Models/Searchengine.php @@ -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()