tkem/cachetools

Letting the user decide the TTL

impredicative opened this issue · 2 comments

I am currently using the TTL decorator over functions. What would be the best way to let the user decide the TTL? I don't want it predecided for me by the function itself. This is a different way of thinking about expiry. The acceptable TTL can be different for different use cases of the user.

For example, consider this call:

# Normal call: my_fun(*args, **kwargs)

# TTL-controlling calls:
fresh_result = cachetools.apply_ttl(0.1, target=my_fun, *args, **kwargs)
cached_result = cachetools.apply_ttl(1.0, target=my_fun, *args, **kwargs)
tkem commented

If you're referring to the func.ttl_cache decorator, the TTL can be set via the ttl parameter.
Please see the documentation for details: https://cachetools.readthedocs.io/en/latest/#module-cachetools.func

@tkem That's not what I asked for at all! Please re-read the initial comment.

The point is to support a variable TTL as decided by a user of the decorated target function. The decorator may itself set a high TTL of say 2.0 which defines the upper bound. A user of the decorated function may however want any smaller TTL, e.g. 0.1, for a particular use case. A different user may however be okay with the default TTL (of 2.0).