Cache heavy computation functions with a timeout value
Cache heavy computation functions with a timeout value
Traditional lru_cache
# to mimic heavy computation
= 12345
return
Limitation
lru_cache
you can use as a decorator to cache the return value from a function.- It has
maxsize
argument to set a limit to the size of the cache, but not aseconds
argument to set an expiry time for the cache.
Solution
The following code snippet overcomes the limitation:
""" Extension over existing lru_cache with timeout
:param seconds: timeout value
:param maxsize: maximum size of the cache
:param typed: whether different keys for different types of cache keys
"""
# create a function wrapped with traditional lru_cache
=
# convert seconds to nanoseconds to set the expiry time in nanoseconds
= * 10 ** 9
= +
# wraps is used to access the decorated function attributes
# if the current cache expired of the decorated function then
# clear cache for that function and set a new cache value with new expiration time
= +
return
=
=
return
# To allow decorator to be used without arguments
return
return
Unit test case for the above function:
= 0
= 0
nonlocal
+= 1
return
nonlocal
+= 1
return
assert == 1,
assert ,
assert == 2,
assert ,
assert ,
assert ,
# Let's now wait for the cache to expire
assert ,
assert ,
# func.cache_clear clear func's cache, not all lru cache
assert ,