How to extend cache ttl (time-to-live) in django-r

2019-08-12 15:49发布

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条回答
爷的心禁止访问
2楼-- · 2019-08-12 16:39

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)
查看更多
登录 后发表回答