Skip to content

Commit

Permalink
Fix #131: Add cache_info() function to @cached decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Jan 22, 2023
1 parent 2216fb2 commit 1e478ef
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 118 deletions.
173 changes: 136 additions & 37 deletions src/cachetools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,64 +615,163 @@ def __getitem(self, key):
return value


def cached(cache, key=keys.hashkey, lock=None):
_CacheInfo = collections.namedtuple(
"CacheInfo", ["hits", "misses", "maxsize", "currsize"]
)


def cached(cache, key=keys.hashkey, lock=None, info=False):
"""Decorator to wrap a function with a memoizing callable that saves
results in a cache.
"""

def decorator(func):
if cache is None:
if info:
hits = misses = 0

def wrapper(*args, **kwargs):
return func(*args, **kwargs)
if isinstance(cache, Cache):

def clear():
pass
def getinfo():
nonlocal hits, misses
return _CacheInfo(hits, misses, cache.maxsize, cache.currsize)

elif lock is None:
elif isinstance(cache, collections.abc.Mapping):

def wrapper(*args, **kwargs):
k = key(*args, **kwargs)
try:
return cache[k]
except KeyError:
pass # key not found
v = func(*args, **kwargs)
try:
cache[k] = v
except ValueError:
pass # value too large
return v
def getinfo():
nonlocal hits, misses
return _CacheInfo(hits, misses, None, len(cache))

def clear():
cache.clear()
else:

else:
def getinfo():
nonlocal hits, misses
return _CacheInfo(hits, misses, 0, 0)

if cache is None:

def wrapper(*args, **kwargs):
nonlocal misses
misses += 1
return func(*args, **kwargs)

def cache_clear():
nonlocal hits, misses
hits = misses = 0

cache_info = getinfo

elif lock is None:

def wrapper(*args, **kwargs):
nonlocal hits, misses
k = key(*args, **kwargs)
try:
result = cache[k]
hits += 1
return result
except KeyError:
misses += 1
v = func(*args, **kwargs)
try:
cache[k] = v
except ValueError:
pass # value too large
return v

def cache_clear():
nonlocal hits, misses
cache.clear()
hits = misses = 0

def wrapper(*args, **kwargs):
k = key(*args, **kwargs)
try:
cache_info = getinfo

else:

def wrapper(*args, **kwargs):
nonlocal hits, misses
k = key(*args, **kwargs)
try:
with lock:
result = cache[k]
hits += 1
return result
except KeyError:
with lock:
misses += 1
v = func(*args, **kwargs)
# in case of a race, prefer the item already in the cache
try:
with lock:
return cache.setdefault(k, v)
except ValueError:
return v # value too large

def cache_clear():
nonlocal hits, misses
with lock:
return cache[k]
except KeyError:
pass # key not found
v = func(*args, **kwargs)
# in case of a race, prefer the item already in the cache
try:
cache.clear()
hits = misses = 0

def cache_info():
with lock:
return cache.setdefault(k, v)
except ValueError:
return v # value too large
return getinfo()

else:
if cache is None:

def wrapper(*args, **kwargs):
return func(*args, **kwargs)

def clear():
with lock:
def cache_clear():
pass

elif lock is None:

def wrapper(*args, **kwargs):
k = key(*args, **kwargs)
try:
return cache[k]
except KeyError:
pass # key not found
v = func(*args, **kwargs)
try:
cache[k] = v
except ValueError:
pass # value too large
return v

def cache_clear():
cache.clear()

else:

def wrapper(*args, **kwargs):
k = key(*args, **kwargs)
try:
with lock:
return cache[k]
except KeyError:
pass # key not found
v = func(*args, **kwargs)
# in case of a race, prefer the item already in the cache
try:
with lock:
return cache.setdefault(k, v)
except ValueError:
return v # value too large

def cache_clear():
with lock:
cache.clear()

cache_info = None

wrapper.cache = cache
wrapper.cache_key = key
wrapper.cache_lock = lock
wrapper.cache_clear = clear
wrapper.cache_clear = cache_clear
wrapper.cache_info = cache_info

return functools.update_wrapper(wrapper, func)

Expand Down
97 changes: 21 additions & 76 deletions src/cachetools/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

__all__ = ("fifo_cache", "lfu_cache", "lru_cache", "mru_cache", "rr_cache", "ttl_cache")

import collections
import functools
import math
import random
import time
Expand All @@ -14,24 +12,10 @@
from dummy_threading import RLock

from . import FIFOCache, LFUCache, LRUCache, MRUCache, RRCache, TTLCache
from . import cached
from . import keys


_CacheInfo = collections.namedtuple(
"CacheInfo", ["hits", "misses", "maxsize", "currsize"]
)


class _UnboundCache(dict):
@property
def maxsize(self):
return None

@property
def currsize(self):
return len(self)


class _UnboundTTLCache(TTLCache):
def __init__(self, ttl, timer):
TTLCache.__init__(self, math.inf, ttl, timer)
Expand All @@ -41,50 +25,11 @@ def maxsize(self):
return None


def _cache(cache, typed):
maxsize = cache.maxsize

def _cache(cache, maxsize, typed):
def decorator(func):
key = keys.typedkey if typed else keys.hashkey
hits = misses = 0
lock = RLock()

def wrapper(*args, **kwargs):
nonlocal hits, misses
k = key(*args, **kwargs)
with lock:
try:
v = cache[k]
hits += 1
return v
except KeyError:
misses += 1
v = func(*args, **kwargs)
# in case of a race, prefer the item already in the cache
try:
with lock:
return cache.setdefault(k, v)
except ValueError:
return v # value too large

def cache_info():
with lock:
maxsize = cache.maxsize
currsize = cache.currsize
return _CacheInfo(hits, misses, maxsize, currsize)

def cache_clear():
nonlocal hits, misses
with lock:
try:
cache.clear()
finally:
hits = misses = 0

wrapper.cache_info = cache_info
wrapper.cache_clear = cache_clear
wrapper = cached(cache=cache, key=key, lock=RLock(), info=True)(func)
wrapper.cache_parameters = lambda: {"maxsize": maxsize, "typed": typed}
functools.update_wrapper(wrapper, func)
return wrapper

return decorator
Expand All @@ -97,11 +42,11 @@ def fifo_cache(maxsize=128, typed=False):
"""
if maxsize is None:
return _cache(_UnboundCache(), typed)
return _cache({}, None, typed)
elif callable(maxsize):
return _cache(FIFOCache(128), typed)(maxsize)
return _cache(FIFOCache(128), 128, typed)(maxsize)
else:
return _cache(FIFOCache(maxsize), typed)
return _cache(FIFOCache(maxsize), maxsize, typed)


def lfu_cache(maxsize=128, typed=False):
Expand All @@ -111,11 +56,11 @@ def lfu_cache(maxsize=128, typed=False):
"""
if maxsize is None:
return _cache(_UnboundCache(), typed)
return _cache({}, None, typed)
elif callable(maxsize):
return _cache(LFUCache(128), typed)(maxsize)
return _cache(LFUCache(128), 128, typed)(maxsize)
else:
return _cache(LFUCache(maxsize), typed)
return _cache(LFUCache(maxsize), maxsize, typed)


def lru_cache(maxsize=128, typed=False):
Expand All @@ -125,11 +70,11 @@ def lru_cache(maxsize=128, typed=False):
"""
if maxsize is None:
return _cache(_UnboundCache(), typed)
return _cache({}, None, typed)
elif callable(maxsize):
return _cache(LRUCache(128), typed)(maxsize)
return _cache(LRUCache(128), 128, typed)(maxsize)
else:
return _cache(LRUCache(maxsize), typed)
return _cache(LRUCache(maxsize), maxsize, typed)


def mru_cache(maxsize=128, typed=False):
Expand All @@ -138,11 +83,11 @@ def mru_cache(maxsize=128, typed=False):
algorithm.
"""
if maxsize is None:
return _cache(_UnboundCache(), typed)
return _cache({}, None, typed)
elif callable(maxsize):
return _cache(MRUCache(128), typed)(maxsize)
return _cache(MRUCache(128), 128, typed)(maxsize)
else:
return _cache(MRUCache(maxsize), typed)
return _cache(MRUCache(maxsize), maxsize, typed)


def rr_cache(maxsize=128, choice=random.choice, typed=False):
Expand All @@ -152,11 +97,11 @@ def rr_cache(maxsize=128, choice=random.choice, typed=False):
"""
if maxsize is None:
return _cache(_UnboundCache(), typed)
return _cache({}, None, typed)
elif callable(maxsize):
return _cache(RRCache(128, choice), typed)(maxsize)
return _cache(RRCache(128, choice), 128, typed)(maxsize)
else:
return _cache(RRCache(maxsize, choice), typed)
return _cache(RRCache(maxsize, choice), maxsize, typed)


def ttl_cache(maxsize=128, ttl=600, timer=time.monotonic, typed=False):
Expand All @@ -165,8 +110,8 @@ def ttl_cache(maxsize=128, ttl=600, timer=time.monotonic, typed=False):
algorithm with a per-item time-to-live (TTL) value.
"""
if maxsize is None:
return _cache(_UnboundTTLCache(ttl, timer), typed)
return _cache(_UnboundTTLCache(ttl, timer), None, typed)
elif callable(maxsize):
return _cache(TTLCache(128, ttl, timer), typed)(maxsize)
return _cache(TTLCache(128, ttl, timer), 128, typed)(maxsize)
else:
return _cache(TTLCache(maxsize, ttl, timer), typed)
return _cache(TTLCache(maxsize, ttl, timer), maxsize, typed)
Loading

0 comments on commit 1e478ef

Please sign in to comment.