serhii.net

In the middle of the desert you can say anything you want

17 Nov 2021

211117-1127 python simple TTL time-based caching

functools has lru_cache, really easy to add it as decorator to a function to cache the responses! Example directly copied from caching - Python in-memory cache with time to live - Stack Overflow:

from functools import lru_cache
import time


@lru_cache()
def my_expensive_function(a, b, ttl_hash=None):
    del ttl_hash  # to emphasize we don't use it and to shut pylint up
    return a + b  # horrible CPU load...


def get_ttl_hash(seconds=3600):
    """Return the same value withing `seconds` time period"""
    return round(time.time() / seconds)


# somewhere in your code...
res = my_expensive_function(2, 2, ttl_hash=get_ttl_hash())
# cache will be updated once in an hour

Used it practically in some code that called an expensive external function multiple times. Bad code I didn’t have time to fix, but it took 2.5 seconds to run. Adding the lines above shortened the runtime from ~2.5 seconds to 0.02 seconds with cache lifetime of 60 seconds.

Didn’t update the function at all without the del ttl_hash and default none parameter bit, TODO understand what’s really happening there.

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus