Skip to content

Cache

Base class for cache using data store.

Authors

Kadek Denaya (kadek.d.r.diana@gdplabs.id)

References

NONE

BaseCache

Bases: ABC

Base class for cache using data store.

cache(key_func=None, name='', matching_strategy=MatchingStrategy.EXACT, matching_config=None, **kwargs) abstractmethod

Decorator to cache the result of a function.

This method should be implemented by subclasses to provide the caching functionality.

Parameters:

Name Type Description Default
key_func Callable | None

Function to generate the cache key. Defaults to None.

None
name str

Name of the cache. Defaults to an empty string.

''
matching_strategy MatchingStrategy

The strategy to use for matching keys. This can be one of the values from the MatchingStrategy enum. Defaults to exact matching.

EXACT
matching_config dict[str, Any] | None

Configuration parameters for matching strategies. Defaults to None.

None
**kwargs

Additional parameters specific to the caching method.

{}

Returns:

Name Type Description
Callable Callable

A decorator that can be applied to a function to cache its result.

Raises:

Type Description
NotImplementedError

If the method is not implemented.

clear() abstractmethod

Clear all cached results.

This method should be implemented by subclasses to provide the clearing functionality.

Raises:

Type Description
NotImplementedError

If the method is not implemented.

delete(key) abstractmethod

Delete the cached result.

This method should be implemented by subclasses to provide the deletion functionality.

Parameters:

Name Type Description Default
key str | list[str]

The cache key to delete.

required

Raises:

Type Description
NotImplementedError

If the method is not implemented.

retrieve(key, **kwargs) abstractmethod

Retrieve the cached result.

This method should be implemented by subclasses to provide the retrieval functionality.

Parameters:

Name Type Description Default
key str

The cache key to retrieve.

required
**kwargs

Additional parameters specific to the retrieval method.

{}

Returns:

Type Description
Any | None

Any | None: The cached result if found, otherwise None.

Raises:

Type Description
NotImplementedError

If the method is not implemented.

store(key, value, **kwargs) abstractmethod

Store the cached result.

This method should be implemented by subclasses to provide the storage functionality.

Parameters:

Name Type Description Default
key str

The cache key to store.

required
value Any

The value to store in the cache.

required
**kwargs

Additional parameters specific to the storage method.

{}

Raises:

Type Description
NotImplementedError

If the method is not implemented.

MatchingStrategy

Bases: StrEnum

Defines how keys should be matched during retrieval.