[CACHE] Add way of calculating hash map if key is not found

This commit is contained in:
Hugo Sales 2021-11-24 15:47:49 +00:00
parent e64fd5aaf6
commit f837df5753
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -299,7 +299,7 @@ abstract class Cache
/** /**
* Set the list * Set the list
*/ */
public static function setHashMap(string $map_key, array $value, string $pool = 'default', float $beta = 1.0): void public static function setHashMap(string $map_key, array $value, string $pool = 'default'): void
{ {
if (isset(self::$redis[$pool])) { if (isset(self::$redis[$pool])) {
if (empty($value)) { if (empty($value)) {
@ -309,7 +309,7 @@ abstract class Cache
->multi(Redis::MULTI) ->multi(Redis::MULTI)
->del($map_key); ->del($map_key);
foreach ($value as $field_key => $field_value) { foreach ($value as $field_key => $field_value) {
self::$redis[$pool]->hSet($map_key, $field_key, $field_value); self::$redis[$pool]->hSet($map_key, (string) $field_key, $field_value);
} }
self::$redis[$pool]->exec(); self::$redis[$pool]->exec();
} }
@ -318,14 +318,22 @@ abstract class Cache
} }
} }
public static function getHashMapKey(string $map_key, string|array $key, string $pool = 'default') public static function getHashMapKey(string $map_key, string|array $key, callable $calculate_map, string $pool = 'default')
{ {
if (isset(self::$redis[$pool])) { if (isset(self::$redis[$pool])) {
if (\is_string($key)) { $get = function () use ($key, $map_key, $pool) {
return self::$redis[$pool]->hget($map_key, $key); if (\is_string($key)) {
} else { return self::$redis[$pool]->hget($map_key, $key);
return self::$redis[$pool]->hmget($map_key, $key); } else {
return self::$redis[$pool]->hmget($map_key, $key);
}
};
$res = $get();
if (\is_null($res)) {
self::setHashMap($map_key, $calculate_map(), $pool);
$res = $get();
} }
return $res;
} else { } else {
throw new NotImplementedException; throw new NotImplementedException;
} }