From d60f4c63c70b098018b1c8d4c1ba6c6cbb891425 Mon Sep 17 00:00:00 2001 From: SunnyQjm Date: Thu, 26 Oct 2023 19:42:52 +0800 Subject: [PATCH] feat(gcache): Support load_all --- environment/1_sdk/gcache_base/gcache.py | 6 ++++- .../1_sdk/gcache_redis/redis_gcache.py | 27 +++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/environment/1_sdk/gcache_base/gcache.py b/environment/1_sdk/gcache_base/gcache.py index fe8aa8cf..48123d40 100644 --- a/environment/1_sdk/gcache_base/gcache.py +++ b/environment/1_sdk/gcache_base/gcache.py @@ -8,7 +8,7 @@ Description: """ import importlib from abc import ABCMeta, abstractmethod -from typing import Union, Optional +from typing import Union, Optional, Dict from threading import Lock from clogger import logger from .exceptions import GCacheProtoAlreadyExistsException, \ @@ -43,6 +43,10 @@ class GCache(metaclass=ABCMeta): @abstractmethod def load(self, key: str) -> Union[None, int, float, dict, str]: pass + + @abstractmethod + def load_all(self) -> Dict[str, Union[int, float, dict, str]]: + pass @abstractmethod def delete(self, key: str) -> bool: diff --git a/environment/1_sdk/gcache_redis/redis_gcache.py b/environment/1_sdk/gcache_redis/redis_gcache.py index 11058420..431e4e52 100644 --- a/environment/1_sdk/gcache_redis/redis_gcache.py +++ b/environment/1_sdk/gcache_redis/redis_gcache.py @@ -7,7 +7,7 @@ File redis_gcache.py Description: """ import json -from typing import Union +from typing import Union, Dict from gcache_base import GCache, GCacheUrl, GCacheException from redis_lua import XRedisHashTable from .common import ClientBase, StaticConst @@ -59,15 +59,12 @@ class RedisGCache(GCache, ClientBase): f"{self._get_store_value(value)}", expire=expire ) - - def load(self, key: str) -> Union[None, int, float, dict, str]: - res = self._x_redis_hash_table.hget(self._table_name, key) - if res is None: - return None - type_value = res.split(SEPARATOR) + + def _get_format_value(self, value: str) -> Union[None, int, float, dict, str]: + type_value = value.split(SEPARATOR) if len(type_value) < 2: raise GCacheException( - f"Load failed, expect value which is {res} start " + f"Load failed, expect value which is {value} start " f"with {SEPARATOR}, " ) type_v = type_value[0] @@ -79,6 +76,20 @@ class RedisGCache(GCache, ClientBase): ) return self._from_str(type_v, value) + def load(self, key: str) -> Union[None, int, float, dict, str]: + res = self._x_redis_hash_table.hget(self._table_name, key) + if res is None: + return None + return self._get_format_value(res) + + def load_all(self) -> Dict[str, Union[int, float, dict, str]]: + res = self._x_redis_hash_table.hgetall(self._table_name) + if res is None: + return {} + for k in res: + res[k] = self._get_format_value(res[k]) + return res + def clean(self): self._x_redis_hash_table.hdrop_table(self._table_name)