redis_func_cache.policies.abstract module#

class redis_func_cache.policies.abstract.AbstractPolicy[source]#

Bases: ABC

Abstract base class for cache eviction policies used by RedisFuncCache.

Inheritance diagram of AbstractPolicy

Subclasses MUST implement:
Optionally, subclasses may define:
  • __key__: A string component used in Redis key naming.

  • __scripts__: A tuple of two Lua script filenames (get, put).

The use of __key__ or __scripts__ depends on the implementation of calc_keys() and calc_hash().

Parameters:

cache – Optional weakref proxy to the RedisFuncCache instance using this policy.

Note

The cache argument may be omitted when instantiating a policy. The RedisFuncCache will bind itself to the policy instance by setting this attribute to a weakref proxy during cache construction.

async aget_size()[source]#

Asynchronously get the number of items in the cache.

Return type:

int

Returns:

The cache size.

Raises:

NotImplementedError – If not implemented by subclass.

async apurge()[source]#

Asynchronously purge the cache.

Return type:

int

Returns:

Number of items removed (if implemented).

Raises:

NotImplementedError – If not implemented by subclass.

property cache: CallableProxyType[RedisFuncCache]#

Returns: The RedisFuncCache instance (via weakref proxy) that uses this policy.

calc_ext_args(f=None, args=None, kwds=None)[source]#

Optionally calculate extra arguments to pass to the Lua script.

Parameters:
Return type:

Iterable[bytes | bytearray | memoryview | str | int | float] | None

Returns:

Iterable of extra encodable arguments, or None.

abstractmethod calc_hash(f=None, args=None, kwds=None)[source]#

Calculate a unique hash for the function and its arguments.

Parameters:
Return type:

bytes | str | memoryview

Returns:

The calculated hash value.

abstractmethod calc_keys(f=None, args=None, kwds=None)[source]#

Calculate the Redis key pair for caching.

Parameters:
Return type:

tuple[str, str]

Returns:

Tuple of two Redis key names (e.g., for set and hash).

get_size()[source]#

Get the number of items in the cache.

Return type:

int

Returns:

The cache size.

Raises:

NotImplementedError – If not implemented by subclass.

property lua_scripts: tuple[Script, Script] | tuple[AsyncScript, AsyncScript]#

Register and return Lua scripts as Redis Script/AsyncScript objects.

Returns:

Tuple of registered Script or AsyncScript objects.

purge()[source]#

Purge the cache.

Return type:

int

Returns:

Number of items removed (if implemented).

Raises:

NotImplementedError – If not implemented by subclass.

read_lua_scripts()[source]#

Read and clean the Lua scripts from package resources.

Return type:

tuple[bytes | str | memoryview, bytes | str | memoryview]

Returns:

Tuple of cleaned Lua script texts (get, put).