fictive.cache.memoize

class fictive.cache.memoize(*, cache_factory, alias=None, additional_key=None)[source]

Decorator that memoizes the decorated callable using caches

__init__(*, cache_factory, alias=None, additional_key=None)[source]
Parameters
  • cache_factory (callable) – a callable that will return a suitable cache when called with a string key

  • aliased – a list of parameter names (to the decorated callable) that will be ignored when generating the unique string key for a memo cache. I.e., calls to the decorated callable that differ only in “aliased” parameters will map to the same cache value. This can be useful for e.g. ignoring self, temporary tokens, or other transient / insignificant parameters when accessing the cache(s).

  • additional_key – a function for furster specifying the cachening key (i.e, used to determine whether to use the memoized value or call the function). MUST take this instance as a first positional argument, the wrapped function as a second positional argument, and then any positional or keyword arguments that will be provided to the decorated function. The callable should return a string-like representation that encapsulates all of the additional attributes that would distinguish cached results. E.g.:: def additional_key(memoize_instance, wrapped_function, *args, **kwargs) -> str

Methods

__init__(*, cache_factory[, alias, …])

param callable cache_factory

get_cache(key)

Retrieve the memo cache for func based on the key

memo_key(func, *args, **kwargs)

Generate a hash key using serialized invocation details

Attributes

MEMO_KEY_PARTS

the methods that will be used to assemble the memoization key

__init__(*, cache_factory, alias=None, additional_key=None)[source]
Parameters
  • cache_factory (callable) – a callable that will return a suitable cache when called with a string key

  • aliased – a list of parameter names (to the decorated callable) that will be ignored when generating the unique string key for a memo cache. I.e., calls to the decorated callable that differ only in “aliased” parameters will map to the same cache value. This can be useful for e.g. ignoring self, temporary tokens, or other transient / insignificant parameters when accessing the cache(s).

  • additional_key – a function for furster specifying the cachening key (i.e, used to determine whether to use the memoized value or call the function). MUST take this instance as a first positional argument, the wrapped function as a second positional argument, and then any positional or keyword arguments that will be provided to the decorated function. The callable should return a string-like representation that encapsulates all of the additional attributes that would distinguish cached results. E.g.:: def additional_key(memoize_instance, wrapped_function, *args, **kwargs) -> str