diff --git a/.env.example b/.env.example index a657413fbab84166dc7b4b7f96af819494904f00..d0b8b81bcfa87727379ddd8c9db66e8032235303 100644 --- a/.env.example +++ b/.env.example @@ -15,7 +15,7 @@ REDIS_RESULT_CONNECTION=default REDIS_RESULT_CACHE_DURATION=60 BROADCAST_DRIVER=log -CACHE_DRIVER=file +CACHE_DRIVER=database SESSION_DRIVER=file QUEUE_CONNECTION=sync diff --git a/app/CacheHelper.php b/app/CacheHelper.php deleted file mode 100644 index 3a4aeaae55015b4c26f40d5ae2ebd0ed927a4d05..0000000000000000000000000000000000000000 --- a/app/CacheHelper.php +++ /dev/null @@ -1,24 +0,0 @@ -<?php - -namespace App; - -use Illuminate\Support\Facades\Redis; - -class CacheHelper -{ - - /** - * MetaGer uses a pretty slow harddrive for the configured cache - * That's why we have some processes running to write cache to disk in parallel - */ - public static function put($key, $value, $timeSeconds) - { - $cacherItem = [ - 'timeSeconds' => $timeSeconds, - 'key' => $key, - 'value' => $value, - ]; - Redis::rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, base64_encode(serialize($cacherItem))); - - } -} diff --git a/app/Console/Commands/RequestCacher.php b/app/Console/Commands/RequestCacher.php deleted file mode 100644 index bf52f428edd9f6474fb1c89b79a43f1b584ce5b4..0000000000000000000000000000000000000000 --- a/app/Console/Commands/RequestCacher.php +++ /dev/null @@ -1,67 +0,0 @@ -<?php - -namespace App\Console\Commands; - -use Cache; -use Illuminate\Console\Command; -use Illuminate\Support\Facades\Redis; - -class RequestCacher extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'requests:cacher'; - - const CACHER_QUEUE = 'cacher.queue'; - protected $shouldRun = true; - - /** - * The console command description. - * - * @var string - */ - protected $description = 'Listens to a buffer of fetched search results and writes them to the filesystem cache.'; - - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - pcntl_async_signals(true); - pcntl_signal(SIGINT, [$this, "sig_handler"]); - pcntl_signal(SIGTERM, [$this, "sig_handler"]); - pcntl_signal(SIGHUP, [$this, "sig_handler"]); - - while ($this->shouldRun) { - $cacheItem = Redis::blpop(self::CACHER_QUEUE, 1); - if (!empty($cacheItem)) { - $cacheItem = unserialize(base64_decode($cacheItem[1])); - if (empty($cacheItem["value"])) { - $cacheItem["value"] = "no-result"; - } - Cache::put($cacheItem["key"], $cacheItem["value"], now()->addSeconds($cacheItem["timeSeconds"])); - } - } - } - - public function sig_handler($sig) - { - $this->shouldRun = false; - echo ("Terminating Cacher Process\n"); - } -} diff --git a/app/Console/Commands/RequestFetcher.php b/app/Console/Commands/RequestFetcher.php index 3af939a7ea2fc5611ce71bd1842361cc4b745a17..9b986603af972b57e341d7327218940608b28bec 100644 --- a/app/Console/Commands/RequestFetcher.php +++ b/app/Console/Commands/RequestFetcher.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use Artisan; +use Cache; use Illuminate\Console\Command; use Illuminate\Support\Facades\Redis; use Log; @@ -51,23 +51,11 @@ class RequestFetcher extends Command public function handle() { $pids = []; - $pid = null; - for ($i = 0; $i < 5; $i++) { - $pid = \pcntl_fork(); - $pids[] = $pid; - if ($pid === 0) { - break; - } - } - if ($pid === 0) { - Artisan::call('requests:cacher'); - exit; - } else { - pcntl_async_signals(true); - pcntl_signal(SIGINT, [$this, "sig_handler"]); - pcntl_signal(SIGTERM, [$this, "sig_handler"]); - pcntl_signal(SIGHUP, [$this, "sig_handler"]); - } + + pcntl_async_signals(true); + pcntl_signal(SIGINT, [$this, "sig_handler"]); + pcntl_signal(SIGTERM, [$this, "sig_handler"]); + pcntl_signal(SIGHUP, [$this, "sig_handler"]); try { $blocking = false; @@ -115,12 +103,7 @@ class RequestFetcher extends Command Redis::pipeline(function ($pipe) use ($resulthash, $body, $cacheDurationMinutes) { $pipe->set($resulthash, $body); $pipe->expire($resulthash, 60); - $cacherItem = [ - 'timeSeconds' => $cacheDurationMinutes * 60, - 'key' => $resulthash, - 'value' => $body, - ]; - $pipe->rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, base64_encode(serialize($cacherItem))); + Cache::put($resulthash, $body, $cacheDurationMinutes * 60); }); \curl_multi_remove_handle($this->multicurl, $info["handle"]); } diff --git a/app/Http/Controllers/MetaGerSearch.php b/app/Http/Controllers/MetaGerSearch.php index 134b92beda984904081629189b197779d9318470..f6f9f434c8d1893cde7aa9fe0ba063d06db5f362 100644 --- a/app/Http/Controllers/MetaGerSearch.php +++ b/app/Http/Controllers/MetaGerSearch.php @@ -71,7 +71,7 @@ class MetaGerSearch extends Controller } } - \App\CacheHelper::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 60 * 60); + Cache::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 60 * 60); # Die Ausgabe erstellen: $resultpage = $metager->createView($quicktipResults); @@ -166,7 +166,7 @@ class MetaGerSearch extends Controller $result["finished"] = $finished; // Update new Engines - \App\CacheHelper::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 1 * 60); + Cache::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 1 * 60); return response()->json($result); } diff --git a/app/Http/Middleware/HumanVerification.php b/app/Http/Middleware/HumanVerification.php index f7c61dc15b4736cec50de395a483a38ebbcdeb16..d646e1fbd935c507be1482104067bf516e63ed97 100644 --- a/app/Http/Middleware/HumanVerification.php +++ b/app/Http/Middleware/HumanVerification.php @@ -2,11 +2,11 @@ namespace App\Http\Middleware; +use Cache; use Captcha; use Closure; use Cookie; use Illuminate\Http\Response; -use Illuminate\Support\Facades\Cache; use URL; class HumanVerification @@ -148,7 +148,7 @@ class HumanVerification // Lock must be acquired within 2 seconds $userList = Cache::get($prefix . "." . $user["id"], []); $userList[$user["uid"]] = $user; - \App\CacheHelper::put($prefix . "." . $user["id"], $userList, 2 * 7 * 24 * 60 * 60); + Cache::put($prefix . "." . $user["id"], $userList, 2 * 7 * 24 * 60 * 60); } public function removeOldUsers($prefix, $userList) @@ -168,7 +168,7 @@ class HumanVerification } if ($changed) { - \App\CacheHelper::put($prefix . "." . $user["id"], $newUserlist, 2 * 7 * 24 * 60 * 60); + Cache::put($prefix . "." . $user["id"], $newUserlist, 2 * 7 * 24 * 60 * 60); } return $newUserlist; diff --git a/app/MetaGer.php b/app/MetaGer.php index ea175f09edd33537bb15603ad4dbe7451c524672..2609e1a3152c7b1ef1b7bf648975436d2edbbb55 100644 --- a/app/MetaGer.php +++ b/app/MetaGer.php @@ -324,7 +324,7 @@ class MetaGer 'page' => $page, 'engines' => $this->next, ]; - \App\CacheHelper::put($this->getSearchUid(), serialize($this->next), 60 * 60); + Cache::put($this->getSearchUid(), serialize($this->next), 60 * 60); } else { $this->next = []; } diff --git a/database/migrations/2020_02_05_163522_create_cache_table.php b/database/migrations/2020_02_05_163522_create_cache_table.php new file mode 100644 index 0000000000000000000000000000000000000000..058afe69c98645601183f7b4adea77cd191bf882 --- /dev/null +++ b/database/migrations/2020_02_05_163522_create_cache_table.php @@ -0,0 +1,32 @@ +<?php + +use Illuminate\Support\Facades\Schema; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class CreateCacheTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('cache', function (Blueprint $table) { + $table->string('key')->unique(); + $table->mediumText('value'); + $table->integer('expiration'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('cache'); + } +}