Commit b08a0808 authored by Dominik Hebeler's avatar Dominik Hebeler

important cache writes are now async

parent ff3409ed
<?php
namespace App;
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' => $time,
'key' => $key,
'value' => $value,
];
Redis::rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, json_encode($cacherItem));
}
}
......@@ -97,7 +97,7 @@ class RequestFetcher extends Command
$infos = curl_getinfo($info["handle"], CURLINFO_PRIVATE);
$infos = explode(";", $infos);
$resulthash = $infos[0];
$cacheDuration = intval($infos[1]);
$cacheDurationMinutes = intval($infos[1]);
$responseCode = curl_getinfo($info["handle"], CURLINFO_HTTP_CODE);
$body = "";
......@@ -112,17 +112,16 @@ class RequestFetcher extends Command
$body = \curl_multi_getcontent($info["handle"]);
}
Redis::pipeline(function ($pipe) use ($resulthash, $body, $cacheDuration) {
Redis::pipeline(function ($pipe) use ($resulthash, $body, $cacheDurationMinutes) {
$pipe->set($resulthash, $body);
$pipe->expire($resulthash, 60);
$cacherItem = [
'cacheDuration' => $cacheDuration,
'hash' => $resulthash,
'body' => $body,
'timeSeconds' => $cacheDurationMinutes * 60,
'key' => $resulthash,
'value' => $body,
];
$pipe->rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, json_encode($cacherItem));
});
#Cache::put($resulthash, $body, now()->addMinutes($cacheDuration));
\curl_multi_remove_handle($this->multicurl, $info["handle"]);
}
if (!$active && !$answerRead) {
......
......@@ -50,7 +50,7 @@ class HumanVerification
# Get all Users of this IP
$users = Cache::get($prefix . "." . $id, []);
$users = $this->removeOldUsers($users);
$users = $this->removeOldUsers($prefix, $users);
$user = [];
if (empty($users[$uid])) {
......@@ -148,10 +148,10 @@ class HumanVerification
// Lock must be acquired within 2 seconds
$userList = Cache::get($prefix . "." . $user["id"], []);
$userList[$user["uid"]] = $user;
Cache::put($prefix . "." . $user["id"], $userList, now()->addWeeks(2));
\App\CacheHelper::put($prefix . "." . $user["id"], $userList, 2 * 7 * 24 * 60 * 60);
}
public function removeOldUsers($userList)
public function removeOldUsers($prefix, $userList)
{
$newUserlist = [];
$now = now();
......@@ -168,10 +168,7 @@ class HumanVerification
}
if ($changed) {
// Lock must be acquired within 2 seconds
Cache::lock($prefix . "." . $user["id"])->block(2, function () {
Cache::put($prefix . "." . $user["id"], $newUserlist, now()->addWeeks(2));
});
\App\CacheHelper::put($prefix . "." . $user["id"], $newUserlist, 2 * 7 * 24 * 60 * 60);
}
return $newUserlist;
......
......@@ -323,7 +323,7 @@ class MetaGer
'page' => $page,
'engines' => $this->next,
];
Cache::put($this->getSearchUid(), serialize($this->next), 60);
\App\CacheHelper::put($this->getSearchUid(), serialize($this->next), 60 * 60);
} else {
$this->next = [];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment