-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reimplement ttl_cache with no threading lock (#39)
* feat: reimplement ttl_cache with no threading lock we dont care about race conditions when checksumming addresses, the lock adds more friction than value --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent
9b1acc0
commit 1ae24ef
Showing
2 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from importlib.metadata import version | ||
from time import monotonic | ||
|
||
from cachetools import cached, keys | ||
from cachetools.func import TTLCache, _UnboundTTLCache | ||
|
||
|
||
_CACHETOOLS_VERSION = tuple(int(i) for i in version("cachetools").split(".")) | ||
|
||
|
||
def ttl_cache(maxsize=128, ttl=600, timer=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) | ||
|
||
|
||
def _cache(cache, maxsize, typed, info: bool = False): | ||
# reimplement ttl_cache with no RLock for race conditions | ||
|
||
key = keys.typedkey if typed else keys.hashkey | ||
get_params = lambda: {"maxsize": maxsize, "typed": typed} | ||
|
||
# `info` param was added in 5.3 | ||
if _CACHETOOLS_VERSION >= (5, 3): | ||
|
||
def decorator(func): | ||
wrapper = cached(cache=cache, key=key, lock=None, info=info)(func) | ||
wrapper.cache_parameters = get_params | ||
return wrapper | ||
|
||
elif info: | ||
raise ValueError( | ||
"You cannot use the `info` param with cachetools versions < 5.3" | ||
) | ||
|
||
else: | ||
|
||
def decorator(func): | ||
wrapper = cached(cache=cache, key=key, lock=None)(func) | ||
wrapper.cache_parameters = get_params | ||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters