[CACHE] Add way to fetch limit,offset values from a list

This commit is contained in:
Hugo Sales 2021-09-20 16:13:12 +01:00
parent 15a87055a6
commit 6715a036e9
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -146,7 +146,7 @@ abstract class Cache
* Retrieve a list from the cache, with a different implementation
* for redis and others, trimming to $max_count if given
*/
public static function getList(string $key, callable $calculate, string $pool = 'default', ?int $max_count = null, float $beta = 1.0): array
public static function getList(string $key, callable $calculate, string $pool = 'default', ?int $max_count = null, ?int $offset = null, ?int $limit = null, float $beta = 1.0): array
{
if (isset(self::$redis[$pool])) {
if (!($recompute = $beta === INF || !(self::$redis[$pool]->exists($key)))) {
@ -169,10 +169,10 @@ abstract class Cache
$res = $calculate(null, $save);
if ($save) {
self::setList($key, $res, $pool, $max_count, $beta);
return $res;
return array_slice($res, $offset ?? 0, $limit);
}
}
return self::$redis[$pool]->lRange($key, 0, $max_count ?? -1);
return self::$redis[$pool]->lRange($key, $offset ?? 0, ($offset ?? 0) + ($limit ?? $max_count ?? -1));
} else {
return self::get($key, function () use ($calculate, $max_count) {
$res = $calculate(null);