dgilland / cacheout

A caching library for Python

Home Page:https://cacheout.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

single key info

silencesmile opened this issue · comments

1、How to get a single key and how long it will expire
2、How to extend the expiration time of a single key

1、How to get a single key

Do you mean fetching the key value?

cache = cacheout.Cache()
value = cache.get(key)

and how long it will expire

expires_on = cache.expire_times.get(key)
# will be seconds from epoch

2、How to extend the expiration time of a single key

You can set the key again to give it a key expiration.

value = cache.get(key)
if value is not None:
    cache.set(value)
    # Or with custom expiration
    cache.set(key, value, ttl=expiration)

There currently isn't a more formal way to do this with a single Cache method but you could subclass Cache if you wanted to encapsulate that logic. This kind of functionality could be added to Cache directly if there's a strong desire for it (e.g. I could see there being a method that sets the expiration of an existing key with the default ttl or a custom one and a method that could extend the key by a given amount [i.e. current expiration += offset]).