feat(gcache): Support load_all

This commit is contained in:
SunnyQjm 2023-10-26 19:42:52 +08:00
parent f6f66d3e9b
commit d60f4c63c7
2 changed files with 24 additions and 9 deletions

View File

@ -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:

View File

@ -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)