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 ...@@ -97,7 +97,7 @@ class RequestFetcher extends Command
$infos = curl_getinfo($info["handle"], CURLINFO_PRIVATE); $infos = curl_getinfo($info["handle"], CURLINFO_PRIVATE);
$infos = explode(";", $infos); $infos = explode(";", $infos);
$resulthash = $infos[0]; $resulthash = $infos[0];
$cacheDuration = intval($infos[1]); $cacheDurationMinutes = intval($infos[1]);
$responseCode = curl_getinfo($info["handle"], CURLINFO_HTTP_CODE); $responseCode = curl_getinfo($info["handle"], CURLINFO_HTTP_CODE);
$body = ""; $body = "";
...@@ -112,17 +112,16 @@ class RequestFetcher extends Command ...@@ -112,17 +112,16 @@ class RequestFetcher extends Command
$body = \curl_multi_getcontent($info["handle"]); $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->set($resulthash, $body);
$pipe->expire($resulthash, 60); $pipe->expire($resulthash, 60);
$cacherItem = [ $cacherItem = [
'cacheDuration' => $cacheDuration, 'timeSeconds' => $cacheDurationMinutes * 60,
'hash' => $resulthash, 'key' => $resulthash,
'body' => $body, 'value' => $body,
]; ];
$pipe->rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, json_encode($cacherItem)); $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"]); \curl_multi_remove_handle($this->multicurl, $info["handle"]);
} }
if (!$active && !$answerRead) { if (!$active && !$answerRead) {
......
...@@ -50,7 +50,7 @@ class HumanVerification ...@@ -50,7 +50,7 @@ class HumanVerification
# Get all Users of this IP # Get all Users of this IP
$users = Cache::get($prefix . "." . $id, []); $users = Cache::get($prefix . "." . $id, []);
$users = $this->removeOldUsers($users); $users = $this->removeOldUsers($prefix, $users);
$user = []; $user = [];
if (empty($users[$uid])) { if (empty($users[$uid])) {
...@@ -148,10 +148,10 @@ class HumanVerification ...@@ -148,10 +148,10 @@ class HumanVerification
// Lock must be acquired within 2 seconds // Lock must be acquired within 2 seconds
$userList = Cache::get($prefix . "." . $user["id"], []); $userList = Cache::get($prefix . "." . $user["id"], []);
$userList[$user["uid"]] = $user; $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 = []; $newUserlist = [];
$now = now(); $now = now();
...@@ -168,10 +168,7 @@ class HumanVerification ...@@ -168,10 +168,7 @@ class HumanVerification
} }
if ($changed) { if ($changed) {
// Lock must be acquired within 2 seconds \App\CacheHelper::put($prefix . "." . $user["id"], $newUserlist, 2 * 7 * 24 * 60 * 60);
Cache::lock($prefix . "." . $user["id"])->block(2, function () {
Cache::put($prefix . "." . $user["id"], $newUserlist, now()->addWeeks(2));
});
} }
return $newUserlist; return $newUserlist;
......
...@@ -323,7 +323,7 @@ class MetaGer ...@@ -323,7 +323,7 @@ class MetaGer
'page' => $page, 'page' => $page,
'engines' => $this->next, 'engines' => $this->next,
]; ];
Cache::put($this->getSearchUid(), serialize($this->next), 60); \App\CacheHelper::put($this->getSearchUid(), serialize($this->next), 60 * 60);
} else { } else {
$this->next = []; $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