问题
I'm using django 1.5.4 and django-redis 3.7.1
I'd like to extend cache's ttl(time-to-live) when I retrieved it.
Here is sample code
from django.core.cache import cache
foo = cache.get("foo)
if not foo:
cache.set("foo", 1, timeout=100)
else:
// Extend Cache's Time-To-Live something like it
cache.ttl("foo") = 200
I tried to search this option at django-redis-docs, but I couldn't find it.
However, I noticed that designate time-to-live value for existing cache is available at redis native command like "Expire foo 100
"
I know that using cache.set
once more make same effect, but I'd like to use more simple method with time-to-live attribute.
回答1:
I solved this problem.
(1) Using 'Raw Client Access' and (2) Extend TTL value without overwriting
Please refer following code.
from redis_cache import get_redis_connection
con = get_redis_connection('default')
con.expire(":{DB NUMBER at settings.py}:" + "foo", 100)
来源:https://stackoverflow.com/questions/25463030/how-to-extend-cache-ttl-time-to-live-in-django-redis