carlogtt_python_library.database.redis_cache_manager module

This module …

class carlogtt_python_library.database.redis_cache_manager.RedisCacheManager(*, host: str, port: int = 6379, username: str = 'default', password: str | None = None, db: int = 0, ssl: bool = True, decode_responses=True, category_keys: Iterable[str], **redis_kwargs: Any)[source]

Bases: object

Cache manager using Redis.

Parameters:
  • host – The Redis server host.

  • port – The Redis server port (default is 6379).

  • username – The username for Redis authentication (default is ‘default’).

  • password – The password for Redis authentication (optional).

  • db – The Redis database number (default is 0).

  • ssl – Whether to use SSL for the Redis connection (default is True).

  • decode_responses – Whether to decode responses from Redis (default is True).

  • category_keys – An iterable of strings representing the valid cache categories.

clear(category: str | None = None) bool[source]

Clears all keys in the specified category, or all categories if none is specified.

Parameters:

category – The cache category to clear (optional).

Returns:

True if all keys were successfully cleared, False otherwise.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

delete(category: str, key: str) bool[source]

Invalidates a specific key in the cache.

Parameters:
  • category – The cache category.

  • key – The specific key within the category.

Returns:

True if the key was successfully invalidated, False otherwise.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

get(category: str, key: str) Any | None[source]

Retrieves a value from the cache.

Parameters:
  • category – The cache category.

  • key – The specific key within the category.

Returns:

The cached value, or None if not found.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

get_category(category: str) Iterator[tuple[str, Any]][source]

Retrieves key-value pairs for all items in the specified cache category.

Parameters:

category – The cache category.

Returns:

An iterator of the cached key-value pairs.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

get_keys(category: str) Generator[str, None, None][source]

Retrieves all keys in the specified cache category.

Parameters:

category – The cache category.

Returns:

An iterator of the cached keys.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

get_values(category: str) Iterator[Any][source]

Retrieves all values in the specified cache category.

Parameters:

category – The cache category.

Returns:

An iterator of the cached values, or None if no values in cache.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

has(category: str, key: str) bool[source]

Checks if a key exists in the cache.

Parameters:
  • category – The cache category.

  • key – The specific key within the category.

Returns:

True if the key exists, False otherwise.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

invalidate_client_cache() None[source]

Clears the cached client.

This method allows manually invalidating the cached client, forcing a new client instance to be created on the next access.

Returns:

None.

Raises:

RedisCacheManagerError – Raises an error if caching is not enabled for this instance.

keys_count(category: str | None = None) int[source]

Returns the total number of keys in the cache for the specified category.

Parameters:

category – (optional) a string representing the name of the category. If None, all the available categories will be considered.

Returns:

An integer representing the total number of keys in the cache for the specified category.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.

set(category: str, key: str, value: Any) bool[source]

Sets a value in the cache.

Parameters:
  • category – The cache category.

  • key – The specific key within the category.

  • value – The value to cache.

Returns:

True if the value was successfully set, False otherwise.

Raise:

RedisCacheManagerError if an error occurs while accessing Redis.