Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python in-memory cache with time to live

Tags:

python

caching

I have multiple threads running the same process that need to be able to to notify each other that something should not be worked on for the next n seconds its not the end of the world if they do however.

My aim is to be able to pass a string and a TTL to the cache and be able to fetch all the strings that are in the cache as a list. The cache can live in memory and the TTL's will be no more than 20 seconds.

Does anyone have a any suggestions for how this can be accomplished?

like image 977
Louise McMahon Avatar asked Aug 02 '15 11:08

Louise McMahon


People also ask

What is TTL Python?

ttl is the time to live in seconds. Follow this answer to receive notifications. edited Oct 11, 2021 at 14:37. firelynx.

What does @cache do in Python?

Caching is one approach that, when used correctly, makes things much faster while decreasing the load on computing resources. Python's functools module comes with the @lru_cache decorator, which gives you the ability to cache the result of your functions using the Least Recently Used (LRU) strategy.

Does Python automatically cache?

No, it's not. The call will be done twice. So, there's room for optimizing the code. – Klaus D.

What is LRU cache Python?

LRU (Least Recently Used) Cache discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item.


1 Answers

In case you don't want to use any 3rd libraries, you can add one more parameter to your expensive function: ttl_hash=None. This new parameter is so-called "time sensitive hash", its the only purpose is to affect lru_cache.

For example:

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  
like image 175
iutinvg Avatar answered Oct 14 '22 00:10

iutinvg