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