Skip to content

Commit 1c7b3dd

Browse files
committed
Memoizing cache decorator with cache lease.
def memoize_lease(cache, expire, lease, name=None, typed=False, tag=None): The `expire` argument is a "hard deadline" for evicting cache entries. Set `expire` to `None` to avoid evictions due to expiration. The `lease` represents a "soft deadline" for the memoized cache entry. Once the lease time has passed, cache entries will be updated asynchronously using a background thread. At most one background thread will be started for each cache entry. While the background thread is executing, memoized cache entries will continue to be treated as "cache hits" until expiration. If name is set to None (default), the callable name will be determined automatically. If typed is set to True, function arguments of different types will be cached separately. For example, f(3) and f(3.0) will be treated as distinct calls with distinct results. The original underlying function is accessible through the `__wrapped__` attribute. This is useful for introspection, for bypassing the cache, or for rewrapping the function with a different cache. >>> from diskcache import Cache >>> cache = Cache() >>> @memoize_lease(cache, expire=10, lease=1) ... def fib(number): ... if number == 0: ... return 0 ... elif number == 1: ... return 1 ... else: ... return fib(number - 1) + fib(number - 2) >>> print(fib(100)) 354224848179261915075 An additional `__cache_key__` attribute can be used to generate the cache key used for the given arguments. >>> key = fib.__cache_key__(100) >>> del cache[key] Remember to call memoize when decorating a callable. If you forget, then a TypeError will occur. :param cache: cache to store callable arguments and return values :param float expire: seconds until arguments expire :param float lease: minimum seconds after last execution we want to update the cache value :param str name: name given for callable (default None, automatic) :param bool typed: cache different types separately (default False) :param str tag: text to associate with arguments (default None) :return: callable decorator
1 parent fc38bd9 commit 1c7b3dd

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

diskcache/recipes.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,131 @@ def __cache_key__(*args, **kwargs):
466466
return wrapper
467467

468468
return decorator
469+
470+
471+
def memoize_lease(cache, expire, lease, name=None, typed=False, tag=None):
472+
"""Memoizing cache decorator with cache lease.
473+
474+
The `expire` argument is a "hard deadline" for evicting cache entries. Set
475+
`expire` to `None` to avoid evictions due to expiration.
476+
477+
The `lease` represents a "soft deadline" for the memoized cache entry. Once
478+
the lease time has passed, cache entries will be updated asynchronously
479+
using a background thread. At most one background thread will be started
480+
for each cache entry. While the background thread is executing, memoized
481+
cache entries will continue to be treated as "cache hits" until expiration.
482+
483+
If name is set to None (default), the callable name will be determined
484+
automatically.
485+
486+
If typed is set to True, function arguments of different types will be
487+
cached separately. For example, f(3) and f(3.0) will be treated as distinct
488+
calls with distinct results.
489+
490+
The original underlying function is accessible through the `__wrapped__`
491+
attribute. This is useful for introspection, for bypassing the cache, or
492+
for rewrapping the function with a different cache.
493+
494+
>>> from diskcache import Cache
495+
>>> cache = Cache()
496+
>>> @memoize_lease(cache, expire=10, lease=1)
497+
... def fib(number):
498+
... if number == 0:
499+
... return 0
500+
... elif number == 1:
501+
... return 1
502+
... else:
503+
... return fib(number - 1) + fib(number - 2)
504+
>>> print(fib(100))
505+
354224848179261915075
506+
507+
An additional `__cache_key__` attribute can be used to generate the cache
508+
key used for the given arguments.
509+
510+
>>> key = fib.__cache_key__(100)
511+
>>> del cache[key]
512+
513+
Remember to call memoize when decorating a callable. If you forget, then a
514+
TypeError will occur.
515+
516+
:param cache: cache to store callable arguments and return values
517+
:param float expire: seconds until arguments expire
518+
:param float lease: minimum seconds after last execution
519+
we want to update the cache value
520+
:param str name: name given for callable (default None, automatic)
521+
:param bool typed: cache different types separately (default False)
522+
:param str tag: text to associate with arguments (default None)
523+
:return: callable decorator
524+
525+
"""
526+
# Caution: Nearly identical code exists in recipes.memoize_stampede
527+
def decorator(func):
528+
"Decorator created by memoize call for callable."
529+
base = (full_name(func),) if name is None else (name,)
530+
531+
def timer(*args, **kwargs):
532+
"Time execution of `func` and return result and time delta."
533+
start = time.time()
534+
result = func(*args, **kwargs)
535+
delta = time.time() - start
536+
return result, delta, time.time()
537+
538+
@functools.wraps(func)
539+
def wrapper(*args, **kwargs):
540+
"Wrapper for callable to cache arguments and return values."
541+
key = wrapper.__cache_key__(*args, **kwargs)
542+
trio, expire_time = cache.get(
543+
key,
544+
default=ENOVAL,
545+
expire_time=True,
546+
retry=True,
547+
)
548+
549+
if trio is not ENOVAL:
550+
result, delta, last_exec = trio
551+
now = time.time()
552+
553+
if (now - last_exec) < lease:
554+
return result # Cache hit.
555+
556+
# Check whether a thread has started for early recomputation.
557+
558+
thread_key = key + (ENOVAL,)
559+
thread_added = cache.add(
560+
thread_key,
561+
None,
562+
expire=delta,
563+
retry=True,
564+
)
565+
566+
if thread_added:
567+
# Start thread for early recomputation.
568+
def recompute():
569+
with cache:
570+
trio = timer(*args, **kwargs)
571+
cache.set(
572+
key,
573+
trio,
574+
expire=expire,
575+
tag=tag,
576+
retry=True,
577+
)
578+
579+
thread = threading.Thread(target=recompute)
580+
thread.daemon = True
581+
thread.start()
582+
583+
return result
584+
585+
trio = timer(*args, **kwargs)
586+
cache.set(key, trio, expire=expire, tag=tag, retry=True)
587+
return trio[0]
588+
589+
def __cache_key__(*args, **kwargs):
590+
"Make key for cache given function arguments."
591+
return args_to_key(base, args, kwargs, typed)
592+
593+
wrapper.__cache_key__ = __cache_key__
594+
return wrapper
595+
596+
return decorator

0 commit comments

Comments
 (0)