Same as DiskCache.memoize but ignoring the first argument(self) for the keys.
(name=None, typed=False, expire=None, tag=None)
| 9 | |
| 10 | |
| 11 | def memoize(name=None, typed=False, expire=None, tag=None): |
| 12 | """Same as DiskCache.memoize but ignoring the first argument(self) for the keys.""" |
| 13 | # Caution: Nearly identical code exists in DjangoCache.memoize |
| 14 | if callable(name): |
| 15 | raise TypeError(f"name {name} cannot be callable") |
| 16 | |
| 17 | def decorator(func): |
| 18 | """Decorator created by memoize() for callable `func`.""" |
| 19 | base = (full_name(func),) if name is None else (name,) |
| 20 | |
| 21 | @ft.wraps(func) |
| 22 | def wrapper(*args, **kwargs): |
| 23 | cls = args[0] |
| 24 | if not cls.use_cache: |
| 25 | return func(*args, **kwargs) |
| 26 | key = wrapper.__cache_key__(*args[1:], **kwargs) |
| 27 | result = cls.cache.get(key, default=ENOVAL, retry=True) |
| 28 | |
| 29 | if result is ENOVAL: |
| 30 | result = func(*args, **kwargs) |
| 31 | if expire is None or expire > 0: |
| 32 | cls.cache.set(key, result, expire, tag=tag, retry=True) |
| 33 | |
| 34 | return result |
| 35 | |
| 36 | def __cache_key__(*args, **kwargs): |
| 37 | """Make key for cache given function arguments.""" |
| 38 | return args_to_key(base, args, kwargs, typed) |
| 39 | |
| 40 | wrapper.__cache_key__ = __cache_key__ |
| 41 | return wrapper |
| 42 | |
| 43 | return decorator |
nothing calls this directly
no outgoing calls
no test coverage detected