Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion redis_decorators/cache_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 8 additions & 0 deletions redis_decorators/cacheable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions redis_decorators/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .cache_element import CacheDateTime, CacheElement, CacheElementSingleType
from .cacheable import (
BoolCacheable,
DictCacheable,
DictCacheType,
DictStringCacheable,
Expand Down Expand Up @@ -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,
Expand Down