| 1 |
"""`functools.lru_cache` compatible memoizing function decorators.""" |
| 2 |
|
| 3 |
__all__ = ("fifo_cache", "lfu_cache", "lru_cache", "rr_cache", "ttl_cache") |
| 4 |
|
| 5 |
import functools |
| 6 |
import math |
| 7 |
import random |
| 8 |
import time |
| 9 |
from threading import Condition |
| 10 |
|
| 11 |
from . import FIFOCache, LFUCache, LRUCache, RRCache, TTLCache |
| 12 |
from . import cached |
| 13 |
from . import keys |
| 14 |
|
| 15 |
|
| 16 |
class _UnboundTTLCache(TTLCache): |
| 17 |
def __init__(self, ttl, timer): |
| 18 |
TTLCache.__init__(self, math.inf, ttl, timer) |
| 19 |
|
| 20 |
@property |
| 21 |
def maxsize(self): |
| 22 |
return None |
| 23 |
|
| 24 |
|
| 25 |
def _cache(cache, maxsize, typed): |
| 26 |
def decorator(func): |
| 27 |
# like functools.lru_cache, this has to be thread-safe; |
| 28 |
# additionally, this also prevents cache stampede scenarios |
| 29 |
# using a condition variable |
| 30 |
key = keys.typedkey if typed else keys.hashkey |
| 31 |
wrapper = cached(cache=cache, key=key, condition=Condition(), info=True)(func) |
| 32 |
wrapper.cache_parameters = lambda: {"maxsize": maxsize, "typed": typed} |
| 33 |
return wrapper |
| 34 |
|
| 35 |
return decorator |
| 36 |
|
| 37 |
|
| 38 |
def fifo_cache(maxsize=128, typed=False): |
| 39 |
"""Decorator to wrap a function with a memoizing callable that saves |
| 40 |
up to `maxsize` results based on a First In First Out (FIFO) |
| 41 |
algorithm. |
| 42 |
|
| 43 |
""" |
| 44 |
if maxsize is None: |
| 45 |
return _cache({}, None, typed) |
| 46 |
elif callable(maxsize): |
| 47 |
return _cache(FIFOCache(128), 128, typed)(maxsize) |
| 48 |
else: |
| 49 |
return _cache(FIFOCache(maxsize), maxsize, typed) |
| 50 |
|
| 51 |
|
| 52 |
def lfu_cache(maxsize=128, typed=False): |
| 53 |
"""Decorator to wrap a function with a memoizing callable that saves |
| 54 |
up to `maxsize` results based on a Least Frequently Used (LFU) |
| 55 |
algorithm. |
| 56 |
|
| 57 |
""" |
| 58 |
if maxsize is None: |
| 59 |
return _cache({}, None, typed) |
| 60 |
elif callable(maxsize): |
| 61 |
return _cache(LFUCache(128), 128, typed)(maxsize) |
| 62 |
else: |
| 63 |
return _cache(LFUCache(maxsize), maxsize, typed) |
| 64 |
|
| 65 |
|
| 66 |
def lru_cache(maxsize=128, typed=False): |
| 67 |
"""Decorator to wrap a function with a memoizing callable that saves |
| 68 |
up to `maxsize` results based on a Least Recently Used (LRU) |
| 69 |
algorithm. |
| 70 |
|
| 71 |
""" |
| 72 |
if maxsize is None: |
| 73 |
return _cache({}, None, typed) |
| 74 |
elif callable(maxsize): |
| 75 |
return _cache(LRUCache(128), 128, typed)(maxsize) |
| 76 |
else: |
| 77 |
return _cache(LRUCache(maxsize), maxsize, typed) |
| 78 |
|
| 79 |
|
| 80 |
def rr_cache(maxsize=128, choice=random.choice, typed=False): |
| 81 |
"""Decorator to wrap a function with a memoizing callable that saves |
| 82 |
up to `maxsize` results based on a Random Replacement (RR) |
| 83 |
algorithm. |
| 84 |
|
| 85 |
""" |
| 86 |
if maxsize is None: |
| 87 |
return _cache({}, None, typed) |
| 88 |
elif callable(maxsize): |
| 89 |
return _cache(RRCache(128, choice), 128, typed)(maxsize) |
| 90 |
else: |
| 91 |
return _cache(RRCache(maxsize, choice), maxsize, typed) |
| 92 |
|
| 93 |
|
| 94 |
def ttl_cache(maxsize=128, ttl=600, timer=time.monotonic, typed=False): |
| 95 |
"""Decorator to wrap a function with a memoizing callable that saves |
| 96 |
up to `maxsize` results based on a Least Recently Used (LRU) |
| 97 |
algorithm with a per-item time-to-live (TTL) value. |
| 98 |
|
| 99 |
""" |
| 100 |
if maxsize is None: |
| 101 |
return _cache(_UnboundTTLCache(ttl, timer), None, typed) |
| 102 |
elif callable(maxsize): |
| 103 |
return _cache(TTLCache(128, ttl, timer), 128, typed)(maxsize) |
| 104 |
else: |
| 105 |
return _cache(TTLCache(maxsize, ttl, timer), maxsize, typed) |