diff --git a/redis_decorators/cache_element.py b/redis_decorators/cache_element.py index 9c6509a..d5b79e0 100644 --- a/redis_decorators/cache_element.py +++ b/redis_decorators/cache_element.py @@ -5,7 +5,7 @@ from redis import Redis -from .cacheable import Cacheable, StringCacheable +from .cacheable import Cacheable, StringCacheable, BoolCacheable FetchType = TypeVar('FetchType') StoreType = TypeVar('StoreType') diff --git a/redis_decorators/cacheable.py b/redis_decorators/cacheable.py index f5a8292..b26823c 100644 --- a/redis_decorators/cacheable.py +++ b/redis_decorators/cacheable.py @@ -82,3 +82,11 @@ def store(self, client: Redis, key: str, value: ListCacheType): def fetch(self, client: Redis, key: str) -> Optional[ListCacheType]: return client.lrange(key, 0, -1) or None + +class BoolCacheable(Cacheable[bool]): + def store(self, client: Redis, key: str, value: bool): + client.set(key, str(value)) + + def fetch(self, client: Redis, key: bool) -> Optional[bool]: + res = client.get(key) + return bool(res) if res else None diff --git a/redis_decorators/caching.py b/redis_decorators/caching.py index 2d760df..daa2c23 100644 --- a/redis_decorators/caching.py +++ b/redis_decorators/caching.py @@ -6,6 +6,7 @@ from .cache_element import CacheDateTime, CacheElement, CacheElementSingleType from .cacheable import ( + BoolCacheable, DictCacheable, DictCacheType, DictStringCacheable, @@ -129,6 +130,14 @@ def cache_datetime(self, get_cache_key: Callable[..., str] = None, **kwargs): return self.cache_value(CacheDateTime(), get_cache_key, **kwargs) + def cache_bool(self, get_cache_key: Callable[..., str] = None, **kwargs): + """Decorate a function to store a string.""" + return self.cache_value( + CacheElementSingleType[str](cacheable=BoolCacheable()), + get_cache_key, + **kwargs, + ) + class CacheValueWrapper: def __init__( self,