[CONFIG][CACHE] Move cache configuration from environment variables to the configuration file

This commit is contained in:
Hugo Sales 2020-10-06 20:22:54 +00:00 committed by Hugo Sales
parent 6cf30f3f65
commit 44f51ce715
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 8 additions and 10 deletions

View File

@ -355,7 +355,10 @@ parameters:
mirror: mirror:
cache: cache:
early_recompute: 0.95
notice_max_count: 128 notice_max_count: 128
adapters:
default: redis://redis
avatar: avatar:
server: server:

View File

@ -19,6 +19,7 @@
namespace App\Core; namespace App\Core;
use App\Util\Common;
use Functional as F; use Functional as F;
use Redis; use Redis;
use RedisCluster; use RedisCluster;
@ -30,7 +31,6 @@ abstract class Cache
{ {
protected static $pools; protected static $pools;
protected static $redis; protected static $redis;
private static string $ENV_VAR = 'SOCIAL_CACHE_ADAPTER';
/** /**
* Configure a cache pool, with adapters taken from `ENV_VAR`. * Configure a cache pool, with adapters taken from `ENV_VAR`.
@ -39,16 +39,11 @@ abstract class Cache
*/ */
public static function setupCache() public static function setupCache()
{ {
if (!isset($_ENV[self::$ENV_VAR])) {
die("A cache adapter is needed in the environment variable {$ENV_VAR}");
}
self::$pools = []; self::$pools = [];
self::$redis = []; self::$redis = [];
$adapters = []; $adapters = [];
foreach (explode(';', $_ENV[self::$ENV_VAR]) as $a) { foreach (Common::config('cache', 'adapters') as $pool => $val) {
list($pool, $val) = explode('=', $a);
self::$pools[$pool] = []; self::$pools[$pool] = [];
self::$redis[$pool] = []; self::$redis[$pool] = [];
foreach (explode(',', $val) as $dsn) { foreach (explode(',', $val) as $dsn) {
@ -134,8 +129,8 @@ abstract class Cache
{ {
if (isset(self::$redis[$pool])) { if (isset(self::$redis[$pool])) {
if (!($recompute = $beta === INF || !(self::$redis[$pool]->exists($key)))) { if (!($recompute = $beta === INF || !(self::$redis[$pool]->exists($key)))) {
if (!($_ENV['REDIS_CACHE_USE_EXPONENTIAL_UPDATE'] ?? false)) { if (is_float($er = Common::config('cache', 'early_recompute'))) {
$recompute = (mt_rand() / mt_getrandmax() > ((float) ($_ENV['REDIS_CACHE_RANDOM_UPDATE'] ?? 0.95))); $recompute = (mt_rand() / mt_getrandmax() > $er);
Log::info('Item "{key}" elected for early recomputation', ['key' => $key]); Log::info('Item "{key}" elected for early recomputation', ['key' => $key]);
} else { } else {
if ($recompute = ($idletime = self::$redis[$pool]->object('idletime', $key) ?? false) && ($expiry = self::$redis[$pool]->ttl($key) ?? false) && $expiry <= $idletime / 1000 * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX)) { if ($recompute = ($idletime = self::$redis[$pool]->object('idletime', $key) ?? false) && ($expiry = self::$redis[$pool]->ttl($key) ?? false) && $expiry <= $idletime / 1000 * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX)) {
@ -147,7 +142,7 @@ abstract class Cache
} }
} }
if ($recompute) { if ($recompute) {
$save = true; $save = true; // Pass by reference
$res = $calculate(null, $save); $res = $calculate(null, $save);
if ($save) { if ($save) {
self::$redis[$pool]->del($key); self::$redis[$pool]->del($key);