"""
[API] Provides interface (and built-in implementations)
how cache keys are constructed.
This interface is used in cache configuration.
"""
from abc import abstractmethod, ABCMeta
from typing import Tuple, Any, Dict
[docs]class EncodedMethodReferenceAndArgsKeyExtractor(KeyExtractor):
"""Encodes method reference, args & kwargs to string and uses that as cache entry key.
This KeyExtractor is object-centric and creates different keys for different objects of the same type
(so when you create new objects - for instance after app restart - old entries in external store
like Redis will be unreachable)."""
[docs]class EncodedMethodNameAndArgsKeyExtractor(KeyExtractor):
"""Encodes method name, args & kwargs to string and uses that as cache entry key.
This KeyExtractor is class-centric and creates same keys for all objects of the same type.
Note: If wrapped function is a method (has 'self' as first positional arg) you may want to exclude 'self' from key
by setting 'skip_first_arg_as_self' flag.
For static methods of ordinary functions flag should be set to 'False'.
Warning: uses method name only, so be cautious and do not wrap methods of different classes with the same names
while using same store and 'skip_first_arg_as_self' set to False."""
def __init__(self, skip_first_arg_as_self=False) -> None:
self._skip_first_arg_as_self = skip_first_arg_as_self
def __str__(self) -> str:
return self.__repr__()
def __repr__(self) -> str:
return "{name}[skip_first_arg_as_self={skip_first_arg_as_self}]".format(
name=self.__class__, skip_first_arg_as_self=self._skip_first_arg_as_self)