2020-07-10 00:58:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ License
|
2020-11-06 19:47:15 +00:00
|
|
|
|
2020-07-10 00:58:00 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2020-11-06 19:47:15 +00:00
|
|
|
|
2020-07-10 00:58:00 +01:00
|
|
|
// }}}
|
|
|
|
|
2020-07-10 14:13:53 +01:00
|
|
|
namespace App\Core;
|
2020-07-10 00:58:00 +01:00
|
|
|
|
2021-09-21 15:35:07 +01:00
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Entity\Note;
|
2020-10-06 21:22:54 +01:00
|
|
|
use App\Util\Common;
|
2021-04-01 23:26:17 +01:00
|
|
|
use App\Util\Exception\ConfigurationException;
|
2020-07-10 00:58:00 +01:00
|
|
|
use Functional as F;
|
2020-07-17 23:58:00 +01:00
|
|
|
use Redis;
|
|
|
|
use RedisCluster;
|
|
|
|
use Symfony\Component\Cache\Adapter;
|
2020-07-10 00:58:00 +01:00
|
|
|
use Symfony\Component\Cache\Adapter\ChainAdapter;
|
|
|
|
|
|
|
|
abstract class Cache
|
|
|
|
{
|
2020-07-16 01:04:25 +01:00
|
|
|
protected static $pools;
|
|
|
|
protected static $redis;
|
2020-07-10 00:58:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure a cache pool, with adapters taken from `ENV_VAR`.
|
|
|
|
* We may want multiple of these in the future, but for now it seems
|
|
|
|
* unnecessary
|
|
|
|
*/
|
2020-07-11 23:43:05 +01:00
|
|
|
public static function setupCache()
|
2020-07-10 00:58:00 +01:00
|
|
|
{
|
2020-07-16 01:04:25 +01:00
|
|
|
self::$pools = [];
|
2021-04-01 23:26:17 +01:00
|
|
|
self::$redis = null;
|
2020-07-16 01:04:25 +01:00
|
|
|
|
2020-07-11 23:43:05 +01:00
|
|
|
$adapters = [];
|
2020-10-06 21:22:54 +01:00
|
|
|
foreach (Common::config('cache', 'adapters') as $pool => $val) {
|
2020-07-16 01:04:25 +01:00
|
|
|
self::$pools[$pool] = [];
|
|
|
|
self::$redis[$pool] = [];
|
2020-07-11 23:43:05 +01:00
|
|
|
foreach (explode(',', $val) as $dsn) {
|
2021-07-20 15:06:29 +01:00
|
|
|
if (str_contains($dsn, '://')) {
|
|
|
|
[$scheme, $rest] = explode('://', $dsn);
|
|
|
|
} else {
|
|
|
|
$scheme = $dsn;
|
|
|
|
$rest = '';
|
|
|
|
}
|
2020-07-11 23:43:05 +01:00
|
|
|
switch ($scheme) {
|
2020-07-16 01:04:25 +01:00
|
|
|
case 'redis':
|
|
|
|
// Redis can have multiple servers, but we want to take proper advantage of
|
|
|
|
// redis, not just as a key value store, but using it's datastructures
|
2021-07-20 15:06:29 +01:00
|
|
|
$dsns = explode(';', $dsn);
|
2020-07-16 01:04:25 +01:00
|
|
|
if (count($dsns) === 1) {
|
2020-07-17 23:58:00 +01:00
|
|
|
$class = Redis::class;
|
|
|
|
$r = new Redis();
|
2021-07-20 15:06:29 +01:00
|
|
|
$r->pconnect($rest);
|
2020-07-16 01:04:25 +01:00
|
|
|
} else {
|
2021-07-20 15:06:29 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
// This requires extra server configuration, but the code was tested
|
|
|
|
// manually and works, so it'll be excluded from automatic tests, for now, at least
|
|
|
|
if (F\Every($dsns, function ($str) { [$scheme, $rest] = explode('://', $str); return str_contains($rest, ':'); }) == false) {
|
2021-04-01 23:26:17 +01:00
|
|
|
throw new ConfigurationException('The configuration of a redis cluster requires specifying the ports to use');
|
|
|
|
}
|
2020-07-17 23:58:00 +01:00
|
|
|
$class = RedisCluster::class; // true for persistent connection
|
2021-07-20 15:06:29 +01:00
|
|
|
$seeds = F\Map($dsns, fn ($str) => explode('://', $str)[1]);
|
2021-09-06 19:49:03 +01:00
|
|
|
$r = new RedisCluster(name: null, seeds: $seeds, timeout: null, readTimeout: null, persistent: true);
|
2020-07-17 23:58:00 +01:00
|
|
|
// Distribute reads randomly
|
|
|
|
$r->setOption($class::OPT_SLAVE_FAILOVER, $class::FAILOVER_DISTRIBUTE);
|
2021-07-20 15:06:29 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-07-16 01:04:25 +01:00
|
|
|
}
|
|
|
|
// Improved serializer
|
|
|
|
$r->setOption($class::OPT_SERIALIZER, $class::SERIALIZER_MSGPACK);
|
|
|
|
// Persistent connection
|
|
|
|
$r->setOption($class::OPT_TCP_KEEPALIVE, true);
|
|
|
|
// Use LZ4 for the improved decompression speed (while keeping an okay compression ratio)
|
|
|
|
$r->setOption($class::OPT_COMPRESSION, $class::COMPRESSION_LZ4);
|
2020-08-28 21:14:31 +01:00
|
|
|
self::$redis[$pool] = $r;
|
|
|
|
$adapters[$pool][] = new Adapter\RedisAdapter($r);
|
2020-07-16 01:04:25 +01:00
|
|
|
break;
|
2020-07-11 23:43:05 +01:00
|
|
|
case 'memcached':
|
2021-07-20 15:06:29 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
// These all are excluded from automatic testing, as they require an unreasonable amount
|
|
|
|
// of configuration in the testing environment. The code is really simple, so it should work
|
2020-07-16 01:04:25 +01:00
|
|
|
// memcached can also have multiple servers
|
2021-07-20 15:06:29 +01:00
|
|
|
$dsns = explode(';', $dsn);
|
2020-07-16 01:04:25 +01:00
|
|
|
$adapters[$pool][] = new Adapter\MemcachedAdapter($dsns);
|
2020-07-11 23:43:05 +01:00
|
|
|
break;
|
|
|
|
case 'filesystem':
|
|
|
|
$adapters[$pool][] = new Adapter\FilesystemAdapter($rest);
|
|
|
|
break;
|
|
|
|
case 'apcu':
|
|
|
|
$adapters[$pool][] = new Adapter\ApcuAdapter();
|
|
|
|
break;
|
|
|
|
case 'opcache':
|
2021-09-06 19:49:03 +01:00
|
|
|
$adapters[$pool][] = new Adapter\PhpArrayAdapter($rest, new Adapter\FilesystemAdapter($rest . '.fallback'));
|
2020-07-11 23:43:05 +01:00
|
|
|
break;
|
|
|
|
case 'doctrine':
|
|
|
|
$adapters[$pool][] = new Adapter\PdoAdapter($dsn);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Log::error("Unknown or discouraged cache scheme '{$scheme}'");
|
|
|
|
return;
|
2021-07-20 15:06:29 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-07-11 23:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 01:04:25 +01:00
|
|
|
|
2021-04-01 23:26:17 +01:00
|
|
|
if (self::$redis[$pool] == null) {
|
|
|
|
unset(self::$redis[$pool]);
|
|
|
|
}
|
|
|
|
|
2020-07-11 23:43:05 +01:00
|
|
|
if (count($adapters[$pool]) === 1) {
|
|
|
|
self::$pools[$pool] = array_pop($adapters[$pool]);
|
|
|
|
} else {
|
2021-07-20 15:06:29 +01:00
|
|
|
self::$pools[$pool] = new ChainAdapter($adapters[$pool]);
|
2020-07-11 23:43:05 +01:00
|
|
|
}
|
2020-07-10 00:58:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 01:04:25 +01:00
|
|
|
public static function set(string $key, mixed $value, string $pool = 'default')
|
2020-07-10 14:13:53 +01:00
|
|
|
{
|
2020-07-16 01:04:25 +01:00
|
|
|
// there's no set method, must be done this way
|
|
|
|
return self::$pools[$pool]->get($key, function ($i) use ($value) { return $value; }, INF);
|
2020-07-10 14:13:53 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 01:04:25 +01:00
|
|
|
public static function get(string $key, callable $calculate, string $pool = 'default', float $beta = 1.0)
|
2020-07-10 00:58:00 +01:00
|
|
|
{
|
2020-07-16 01:04:25 +01:00
|
|
|
return self::$pools[$pool]->get($key, $calculate, $beta);
|
2020-07-10 14:13:53 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 01:04:25 +01:00
|
|
|
public static function delete(string $key, string $pool = 'default'): bool
|
2020-07-10 17:41:26 +01:00
|
|
|
{
|
2020-07-16 01:04:25 +01:00
|
|
|
return self::$pools[$pool]->delete($key);
|
|
|
|
}
|
2020-07-10 17:41:26 +01:00
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Retrieve a list from the cache, with a different implementation
|
|
|
|
* for redis and others, trimming to $max_count if given
|
|
|
|
*/
|
2021-09-21 15:35:07 +01:00
|
|
|
public static function getList(string $key, callable $calculate, string $pool = 'default', ?int $max_count = null, ?int $left = null, ?int $right = null, float $beta = 1.0): array
|
2020-07-16 01:04:25 +01:00
|
|
|
{
|
|
|
|
if (isset(self::$redis[$pool])) {
|
2020-08-28 21:14:31 +01:00
|
|
|
if (!($recompute = $beta === INF || !(self::$redis[$pool]->exists($key)))) {
|
2020-10-06 21:22:54 +01:00
|
|
|
if (is_float($er = Common::config('cache', 'early_recompute'))) {
|
|
|
|
$recompute = (mt_rand() / mt_getrandmax() > $er);
|
2020-08-28 21:14:31 +01:00
|
|
|
Log::info('Item "{key}" elected for early recomputation', ['key' => $key]);
|
|
|
|
} 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)) {
|
2021-07-20 15:06:29 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-08-28 21:14:31 +01:00
|
|
|
Log::info('Item "{key}" elected for early recomputation {delta}s before its expiration', [
|
|
|
|
'key' => $key,
|
|
|
|
'delta' => sprintf('%.1f', $expiry - microtime(true)),
|
|
|
|
]);
|
2021-07-20 15:06:29 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-08-28 21:14:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($recompute) {
|
2020-10-06 21:22:54 +01:00
|
|
|
$save = true; // Pass by reference
|
2020-08-28 21:14:31 +01:00
|
|
|
$res = $calculate(null, $save);
|
|
|
|
if ($save) {
|
2021-04-01 23:26:17 +01:00
|
|
|
self::setList($key, $res, $pool, $max_count, $beta);
|
2021-09-21 15:35:07 +01:00
|
|
|
return array_slice($res, $left ?? 0, $right - ($left ?? 0));
|
2020-08-28 21:14:31 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 15:35:07 +01:00
|
|
|
return self::$redis[$pool]->lRange($key, $left ?? 0, ($right ?? $max_count ?? 0) - 1);
|
2020-07-16 01:04:25 +01:00
|
|
|
} else {
|
2021-04-01 23:26:17 +01:00
|
|
|
return self::get($key, function () use ($calculate, $max_count) {
|
|
|
|
$res = $calculate(null);
|
|
|
|
if ($max_count != -1) {
|
|
|
|
$res = array_slice($res, 0, $max_count);
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}, $pool, $beta);
|
2020-07-10 17:41:26 +01:00
|
|
|
}
|
2020-07-16 01:04:25 +01:00
|
|
|
}
|
2020-07-10 17:41:26 +01:00
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
2021-04-01 23:26:17 +01:00
|
|
|
* Set the list
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2021-04-01 23:26:17 +01:00
|
|
|
public static function setList(string $key, array $value, string $pool = 'default', ?int $max_count = null, float $beta = 1.0): void
|
2020-07-16 01:04:25 +01:00
|
|
|
{
|
|
|
|
if (isset(self::$redis[$pool])) {
|
|
|
|
self::$redis[$pool]
|
2021-04-01 23:26:17 +01:00
|
|
|
// Ensure atomic
|
|
|
|
->multi(Redis::MULTI)
|
|
|
|
->del($key)
|
|
|
|
->rPush($key, ...$value)
|
2020-07-16 01:04:25 +01:00
|
|
|
// trim to $max_count, unless it's 0
|
2021-07-20 15:06:29 +01:00
|
|
|
->lTrim($key, -$max_count ?? 0, -1)
|
2020-07-16 01:04:25 +01:00
|
|
|
->exec();
|
|
|
|
} else {
|
2021-09-06 20:59:36 +01:00
|
|
|
self::set($key, $value, $pool);
|
2020-07-16 01:04:25 +01:00
|
|
|
}
|
2020-07-10 17:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
2021-04-01 23:26:17 +01:00
|
|
|
* Push a value to the list
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2021-04-01 23:26:17 +01:00
|
|
|
public static function pushList(string $key, mixed $value, string $pool = 'default', ?int $max_count = null, float $beta = 1.0): void
|
2020-07-10 17:41:26 +01:00
|
|
|
{
|
2020-07-16 01:04:25 +01:00
|
|
|
if (isset(self::$redis[$pool])) {
|
2021-04-01 23:26:17 +01:00
|
|
|
self::$redis[$pool]
|
|
|
|
// doesn't need to be atomic, adding at one end, deleting at the other
|
|
|
|
->multi(Redis::PIPELINE)
|
2021-09-21 15:35:07 +01:00
|
|
|
->lPush($key, $value)
|
2021-04-01 23:26:17 +01:00
|
|
|
// trim to $max_count, if given
|
2021-07-20 15:06:29 +01:00
|
|
|
->lTrim($key, -$max_count ?? 0, -1)
|
2021-04-01 23:26:17 +01:00
|
|
|
->exec();
|
2020-07-16 01:04:25 +01:00
|
|
|
} else {
|
2021-04-01 23:26:17 +01:00
|
|
|
$res = self::get($key, function () { return []; }, $pool, $beta);
|
|
|
|
$res[] = $value;
|
|
|
|
if ($max_count != null) {
|
2021-07-20 15:06:29 +01:00
|
|
|
$count = count($res);
|
|
|
|
$res = array_slice($res, $count - $max_count, $count); // Trim the older values
|
2020-07-16 01:04:25 +01:00
|
|
|
}
|
2021-09-06 20:59:36 +01:00
|
|
|
self::set($key, $res, $pool);
|
2020-07-10 17:41:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
2021-04-01 23:26:17 +01:00
|
|
|
* Delete a whole list at $key
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2021-04-01 23:26:17 +01:00
|
|
|
public static function deleteList(string $key, string $pool = 'default'): bool
|
2020-07-10 14:13:53 +01:00
|
|
|
{
|
2021-04-01 23:26:17 +01:00
|
|
|
if (isset(self::$redis[$pool])) {
|
|
|
|
return self::$redis[$pool]->del($key) == 1;
|
|
|
|
} else {
|
|
|
|
return self::delete($key, $pool);
|
|
|
|
}
|
2020-07-10 00:58:00 +01:00
|
|
|
}
|
2021-09-21 15:35:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|callable(int $offset, int $lenght): Note[] $getter
|
|
|
|
*/
|
|
|
|
public static function pagedStream(string $key, ?callable $getter = null, ?string $query = null, array $query_args = null, int $page = 1, ?int $per_page = null, string $pool = 'default', ?int $max_count = null, float $beta = 1.0)
|
|
|
|
{
|
|
|
|
// TODO scope
|
|
|
|
|
|
|
|
if (is_null($per_page)) {
|
|
|
|
$per_page = Common::config('streams', 'notes_per_page');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($max_count) && $per_page > $max_count || !(is_null($getter) ^ is_null($query))) {
|
|
|
|
throw new \InvalidArgumentException;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_callable($getter)) {
|
|
|
|
$getter = function (int $offset, int $lenght) use ($query, $query_args) {
|
|
|
|
return DB::dql($query, $query_args, options: ['offset' => $offset, 'limit' => $lenght]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
$requested_left = $offset = $per_page * ($page - 1);
|
|
|
|
$requested_right = $requested_left + $per_page;
|
|
|
|
[$stored_left, $stored_right] = F\map(explode(':', self::get("{$key}-bounds", fn () => "{$requested_left}:{$requested_right}")), fn (string $v) => (int) $v);
|
|
|
|
$lenght = $stored_right - $stored_left;
|
|
|
|
|
|
|
|
if (!is_null($max_count) && $lenght > $max_count) {
|
|
|
|
$lenght = $max_count;
|
|
|
|
$requested_right = $requested_left + $max_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stored_left > $requested_left || $stored_right < $requested_right) {
|
|
|
|
$res = $getter($stored_left, $stored_right);
|
|
|
|
self::setList($key, value: $res, pool: $pool, max_count: $max_count, beta: $beta);
|
|
|
|
self::set("{$key}-bounds", "{$stored_left}:{$stored_right}");
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getList($key, fn () => $getter($requested_left, $lenght), max_count: $max_count, left: $requested_left, right: $requested_right, beta: $beta);
|
|
|
|
}
|
2020-07-10 00:58:00 +01:00
|
|
|
}
|