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

ぐ巨炮叔叔 提交于 2020-01-05 06:55:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!