如何在django-redis中扩展缓存ttl(生存时间)? [英] How to extend cache ttl (time-to-live) in django-redis?
问题描述
我正在使用django 1.5.4和django-redis 3.7.1
I'm using django 1.5.4 and django-redis 3.7.1
我想在检索缓存时延长其ttl(生存时间).
这是示例代码
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
我尝试在 django-redis-docs 上搜索此选项,但是我找不到它.
I tried to search this option at django-redis-docs, but I couldn't find it.
但是,我注意到可以在redis本机命令(例如" Expire foo 100
"
However, I noticed that designate time-to-live value for existing cache is available at redis native command like "Expire foo 100
"
我知道再次使用 cache.set
会产生相同的效果,但是我想使用具有生存时间属性的更简单方法.
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.
推荐答案
要扩展django-redis缓存记录的ttl(生存时间),请使用 expire(key,timeout)
To extend a ttl(time-to-live) of the django-redis cache record use expire(key, timeout)
Django-Redis:到期并&坚持
来自django.core.cache的
from django.core.cache import cache
cache.set("foo", 1, timeout=100)
cache.ttl("foo")
>>> 100
如果密钥已经过期,则不能延长ttl(生存时间)
You cannot extend ttl(time-to-live) if the key already expired
if cache.ttl("foo") > 0:
cache.expire("foo", timeout=500)
cache.ttl("foo")
>>> 500
这篇关于如何在django-redis中扩展缓存ttl(生存时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!