Sobes.tech
Middle

How to invalidate the cache so that it does not grow infinitely? How to add TTL (time-to-live)?

sobes.tech AI

Answer from AI

To prevent the cache from growing indefinitely, you need to implement a mechanism for invalidating stale data and limiting size.

How to add TTL (time-to-live):

  • When saving a value in the cache, store the expiration time along with it (for example, current time + TTL).
  • When retrieving a value, check if the TTL has expired. If it has, delete the element and return no value.

Example of a simple TTL cache in Python:

import time

class TTLCache:
    def __init__(self, ttl_seconds):
        self.ttl = ttl_seconds
        self.store = {}

    def set(self, key, value):
        expire_at = time.time() + self.ttl
        self.store[key] = (value, expire_at)

    def get(self, key):
        item = self.store.get(key)
        if not item:
            return None
        value, expire_at = item
        if time.time() > expire_at:
            del self.store[key]
            return None
        return value

    def cleanup(self):
        now = time.time()
        keys_to_delete = [k for k, (_, exp) in self.store.items() if exp <= now]
        for k in keys_to_delete:
            del self.store[k]

How to limit cache size:

  • Use data structures with size constraints, such as an LRU cache (Least Recently Used).
  • When reaching the maximum size, remove the least used elements.

In Python, you can use functools.lru_cache or third-party libraries like cachetools.