RedisCache

RedisCache(seed: str | int, redis_url: str)

Implementation of AbstractCache using the Redis database.
This class provides a concrete implementation of the AbstractCache interface using the Redis database for caching data.
Attributes:
seed (Union[str, int]): A seed or namespace used as a prefix for cache keys.
cache (redis.Redis): The Redis client used for caching.
Methods:
init(self, seed, redis_url): Initializes the RedisCache with the given seed and Redis URL.
_prefixed_key(self, key): Internal method to get a namespaced cache key.
get(self, key, default=None): Retrieves an item from the cache.
set(self, key, value): Sets an item in the cache.
close(self): Closes the Redis client.
enter(self): Context management entry.
exit(self, exc_type, exc_value, traceback): Context management exit.
Initialize the RedisCache instance.

Parameters:
NameDescription
seedType: str | int
redis_urlType: str

Instance Methods

close

close(self) -> None

Close the Redis client.
Perform any necessary cleanup, such as closing network connections.


get

get(
    self,
    key: str,
    default: Any | None = None
) -> Any | None

Retrieve an item from the Redis cache.

Parameters:
NameDescription
keyThe key identifying the item in the cache.

Type: str
defaultThe default value to return if the key is not found.

Defaults to None.

Type: Any | None

Default: None
Returns:
TypeDescription
Any | NoneThe deserialized value associated with the key if found, else the default value.

set

set(
    self,
    key: str,
    value: Any
) -> None

Set an item in the Redis cache.

Parameters:
NameDescription
keyThe key under which the item is to be stored.

Type: str
valueThe value to be stored in the cache.

Type: Any