linotp.lib.cache_utils module

This module contains helper functions for caching

linotp.lib.cache_utils.cache_in_request(_func=None, *, key_generator=<function <lambda>>)

Decorator to use for caching function calls in the request context

This decorator can be used to cache the function calls within the context of a request

Parameters

key_generator – the function which takes exactly the same arguments

as the function being cached and returns a unique key for its output when the key_generator is not passed, it will use args+tuple(kwargs.items()) as the key. This can obviously fail if the args are not hashable. The key can be any object which is allowed to be used as the key of a dictionary.

Returns

the cached output of the decorated function

or the output of a new call to the decorated function

Note

In case of an optional key_generator, it can be fatal if

the generated keys are not unique for each functional call

Example

>>>@cache_in_request

func_to_be_decorated(*args, **kwargs)

>>>def key_generator_for_func_to_be_decorated(*foo, **bar): lambda ….

@cache_in_request(key_generator=key_generator_for_func_to_be_decorated) def func_to_be_decorated(*foo, **bar)