Skip to content

CacheRedis

Bases: Cache

A Redis-backed cache implementation.

Provides get, set, and delete operations for JSON and string values, with optional key prefixing and TTL support.

Attributes:

Name Type Description
client Redis

The underlying Redis client instance.

prefix str | None

An optional prefix prepended to all cache keys.

default_ttl_seconds int | None

The default TTL for cached entries in seconds. If None, entries will not expire.

__init__(client, prefix=None, default_ttl_seconds=None)

Initializes CacheRedis with a Redis client.

Parameters:

Name Type Description Default
client Redis

A configured Redis client instance.

required
prefix str | None

An optional prefix to prepend to all cache keys, separated by an underscore.

None
default_ttl_seconds int | None

The default TTL in seconds for cached entries. If None, entries will not expire.

None

delete(key)

Deletes a key from the cache.

Parameters:

Name Type Description Default
key str

The cache key to delete.

required

from_config(engine_config, prefix=None, default_ttl_seconds=None) classmethod

Creates a CacheRedis instance from a configuration dictionary.

Parameters:

Name Type Description Default
engine_config dict[str, Any]

A dictionary containing the Redis configuration. Must include a uri key with the Redis server address.

required
prefix str | None

An optional prefix to prepend to all cache keys.

None
default_ttl_seconds int | None

The default TTL in seconds for cached entries.

None

Returns:

Name Type Description
CacheRedis CacheRedis

A new instance connected to the specified server.

Raises:

Type Description
KeyError

If the uri key is missing from engine_config.

get_json(key)

Retrieves and deserializes a JSON value from the cache.

Parameters:

Name Type Description Default
key str

The cache key to look up.

required

Returns:

Type Description
Any | None

Any | None: The deserialized Python object if the key exists, otherwise None. Also returns None if an error occurs during retrieval.

get_object(key)

Retrieves a binary-serialized object from the cache.

Parameters:

Name Type Description Default
key str

The cache key to look up.

required

Raises:

Type Description
NotImplementedError

Binary deserialization is not yet implemented.

get_string(key)

Retrieves a string value from the cache.

If the value is returned as bytes by the Redis client, it will be decoded to a string before being returned.

Parameters:

Name Type Description Default
key str

The cache key to look up.

required

Returns:

Type Description
str | None

str | None: The cached string if the key exists, otherwise None. Also returns None if an error occurs during retrieval.

set_json(key, value, ttl_seconds=None)

Serializes a value to JSON and stores it in the cache.

Parameters:

Name Type Description Default
key str

The cache key to store the value under.

required
value Any

The Python object to serialize and cache.

required
ttl_seconds int | None

TTL in seconds for this entry. Falls back to default_ttl_seconds if not provided.

None

set_object(key, value, ttl_seconds=None)

Serializes an object to binary and stores it in the cache.

Parameters:

Name Type Description Default
key str

The cache key to store the value under.

required
value Any

The object to serialize and cache.

required
ttl_seconds int | None

TTL in seconds for this entry. Falls back to default_ttl_seconds if not provided.

None

Raises:

Type Description
NotImplementedError

Binary serialization is not yet implemented.

set_string(key, value, ttl_seconds=None)

Stores a string value in the cache.

Parameters:

Name Type Description Default
key str

The cache key to store the value under.

required
value str

The string to cache.

required
ttl_seconds int | None

TTL in seconds for this entry. Falls back to default_ttl_seconds if not provided.

None