I am looking for a way to use python's cachetools
built-in library functionality of a cache, but also support hit/miss statistics, use a custom key function, and if possible support an unbounded cache?
Unfortunately, I could only find these ways:
- If I want to use an unbound cache, and have hit/miss statistics:
from cachetools.func import lru_cache @lru_cache(maxsize=None) def foo(a, b, c=None): print("foo")
- If I want to use an unbound cache, and use a custom key function:
or, use this "hack":from cachetools import cached @cached( cache={}, key=lambda a, b, c=None: "a" if c is None else "b" ) def foo(a, b, c=None): print("foo")
from cachetools import cached, LRUCache @cached( cache=LRUCache(maxsize=1, getsizeof=lambda _: 0), # will always pass the test of maxsize check key=lambda a, b, c=None: "a" if c is None else "b" ) def foo(a, b, c=None): print("foo")
How would I go about if I want to use hit/miss statistics and a custom key function?
I know how to implement one on my own, I was just wondering if there is some already built-in way in python's cachetools/functools that supports this?