From 9563fb0af3eceabb637ae50f0b02b0ade202967a Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Fri, 10 Jul 2020 16:41:26 +0000 Subject: [PATCH] [CACHE] Extend the static wrapper to support working with lists in caches that don't natively support them --- src/Core/Cache.php | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Core/Cache.php b/src/Core/Cache.php index b743c3bd9b..04bc04370c 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -56,18 +56,40 @@ abstract class Cache return self::$pool->get($key, $calculate, $beta); } - public static function delete(string $key) + public static function delete(string $key): bool { return self::$pool->delete($key); } - public static function getList(string $key, callable $calculate, int $max_count = 0, float $beta = 1.0) + public static function getList(string $key, callable $calculate, int $max_count = 64, float $beta = 1.0): SplFixedArray + { + $keys = self::getKeyList($key, $max_count, $beta); + + $list = new SplFixedArray($keys->count()); + foreach ($keys as $k) { + $list[] = self::get($k, $calculate, $beta); + } + + return $list; + } + + public static function deleteList(string $key, int $count = 0) + { + $keys = self::getKeyList($key, $max_count, $beta); + if (!F\every($keys, function ($k) { return self::delete($k); })) { + Log::warning("Some element of the list associated with {$key} was not deleted. There may be some memory leakage in the cache process"); + } + return self::delete($key); + } + + private static function getKeyList(string $key, int $max_count, float $beta): array { // Get the current keys associated with a list. If the cache // is not primed, the function is called and returns an empty - // list - $keys = self::get($key, function (ItemInterface $i) { return []; }, $beta); - if ($max_count == 0) { - } + // ring buffer + return self::get($key, + function (ItemInterface $i) use ($max_count) { + return new RingBuffer($max_count); + }, $beta); } }