Give a function its own TTL

Contents

Give a function its own TTL#

This guide shows you how to cache a function of your own add-on for a lifetime other than the default.

plone.memoize.ram.cache has no way to say how long a value lives, so every value in Redis gets REDIS_MEMOIZE_TTL, a day by default. collective.redismemoize.api.cache takes the same key function, plus a ttl in seconds:

from collective.redismemoize import api


def _key(fun, lang):
    return lang


@api.cache(_key, ttl=300)
def country_names(lang: str) -> dict[str, str]:
    """Return country names in a language, cached for five minutes."""
    if lang == "pt":
        return {"br": "Brasil", "pt": "Portugal"}
    return {"br": "Brazil", "pt": "Portugal"}

The value goes through the same chooser, the same checks, and the same routing as any other cached function. ttl only matters when the value ends up in Redis. In RAM, and on a site where the package is not configured, the decorator behaves like ram.cache.

ttl must be a positive integer; anything else raises ValueError when the module is imported.

Check it#

Call the function, then look its key up in Redis:

redis-cli --scan --pattern 'plone.memoize:*country_names:*'
redis-cli ttl <the key printed above>