ahvn.cache.callback_cache module

Callback-based cache implementation (no storage, only triggers callbacks on set).

class ahvn.cache.callback_cache.CallbackCache(callbacks=None, feeds=None, exclude=None, *args, **kwargs)[源代码]

基类:BaseCache

An implementation of BaseCache that does not cache any data, but calls callbacks on set, and feeds on get.

参数:
__init__(callbacks=None, feeds=None, exclude=None, *args, **kwargs)[源代码]

Initialization.

参数:
  • callbacks (Optional[Iterable[Callable[[int, Dict[str, Any]], None]]]) -- List of callback functions to call on set. Each callback function must has API callback(key: int, value: Dict[str, Any]), which handles a cache set event.

  • feeds (Optional[Iterable[Callable[[Union[Callable, str], Any], None]]]) -- List of feed functions to call on get. Each feed function must have API feed(func: Union[Callable, str], **kwargs), which handles a cache get event. The kwargs are the input to the function. Notice that feeds must be ordered: the first feed function with a non-Ellipsis return value will be used.

  • exclude (Optional[Iterable[str]]) -- Keys to exclude from inputs when creating cache entries.

  • *args -- Additional positional arguments.

  • **kwargs -- Additional keyword arguments.

get(func, **kwargs)[源代码]

Retrieves a cached value for the given function and inputs.

参数:
  • func (Union[Callable, str]) -- The function or its name to retrieve the cached value for. Notice that for CallbackCache, when all feed functions return ..., the function will be called: # (deprecated) If the func is callable, it will be called with the provided keyword arguments. # (deprecated) Otherwise, it will NOT be called. For better stability, it is recommend to use a default feed function that can handle missing values.

  • **kwargs -- Arbitrary keyword arguments representing the inputs to the function.

返回:

The cached output if found, otherwise Ellipsis (to avoid collisions with functions returning None).

返回类型:

Any