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

Merge branch '931-use-mariadb-as-cache-backend' into 'development'

Resolve "Use Mariadb as Cache Backend"

Closes #931

See merge request !1505
parents 54b2e351 dcd4e2d0
No related branches found
No related tags found
2 merge requests!1508Development,!1505Resolve "Use Mariadb as Cache Backend"
......@@ -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
......
<?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)));
}
}
<?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");
}
}
......@@ -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"]);
}
......
......@@ -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);
}
......
......@@ -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;
......
......@@ -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 = [];
}
......
<?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');
}
}
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