如何使 Django 数据库缓存的条目过期?
How can I expire entries to Django database cache?
我有一个带有已注册数据库缓存的 Django 应用程序:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'exchange_rate_cache',
}
}
我希望缓存中的条目在一周后过期并被删除。要从缓存中删除条目,只需执行以下操作:
from django.core.cache import cache
cache.delete(key)
但是,只有当条目在缓存中存储超过 1 周时,我才必须执行此操作。
如何做到这一点?谢谢。
我认为您在错误的级别上解决了问题:CACHES
设置 具有 自动 过期设置: 'TIMEOUT'
键:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'exchange_rate_cache',
<b>'TIMEOUT': 604800</b> # 7 days
}
}
此设置指定值 "expires" 之前的秒数,或如 documentation [Django-doc] 中所述:
TIMEOUT
: The default timeout, in seconds, to use for the cache. This
argument defaults to 300 seconds (5 minutes). You can set TIMEOUT
to
None
so that, by default, cache keys never expire. A value of 0
causes
keys to immediately expire (effectively “don’t cache”).
一天需要60×60×24秒,周是7天,所以一周有 604800
秒。
通过在设置中添加此项,您可以在以后改变主意时轻松更改到期时间。
默认情况下,缓存也包含有限数量的元素(请参阅文档中的其他设置),此外还有其他因素可能导致缓存删除元素(例如,如果您使用 memory缓存 重启服务器通常会清除缓存)。
此外你可以 - 喜欢 - also ad-hoc specify an expiration for a specific key when you set(..)
it; as is specified in the documentation:
The basic interface is set(key, value, timeout)
and get(key)
(..)
The timeout
argument is optional and defaults to the timeout
argument of the appropriate backend in the CACHES
setting
(explained above). It’s the number of seconds the value should be
stored in the cache. Passing in None
for timeout
will cache the
value forever. A timeout of 0 won’t cache the value.
redis_time_cache = 180 # seconds
cache.set(key, data, redis_time_cache)
- 关键='my_key'
- 数据 = JSON 或 STRING
- redis_time_cache = 超时
我有一个带有已注册数据库缓存的 Django 应用程序:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'exchange_rate_cache',
}
}
我希望缓存中的条目在一周后过期并被删除。要从缓存中删除条目,只需执行以下操作:
from django.core.cache import cache
cache.delete(key)
但是,只有当条目在缓存中存储超过 1 周时,我才必须执行此操作。
如何做到这一点?谢谢。
我认为您在错误的级别上解决了问题:CACHES
设置 具有 自动 过期设置: 'TIMEOUT'
键:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'exchange_rate_cache',
<b>'TIMEOUT': 604800</b> # 7 days
}
}
此设置指定值 "expires" 之前的秒数,或如 documentation [Django-doc] 中所述:
TIMEOUT
: The default timeout, in seconds, to use for the cache. This argument defaults to 300 seconds (5 minutes). You can setTIMEOUT
toNone
so that, by default, cache keys never expire. A value of0
causes keys to immediately expire (effectively “don’t cache”).
一天需要60×60×24秒,周是7天,所以一周有 604800
秒。
通过在设置中添加此项,您可以在以后改变主意时轻松更改到期时间。
默认情况下,缓存也包含有限数量的元素(请参阅文档中的其他设置),此外还有其他因素可能导致缓存删除元素(例如,如果您使用 memory缓存 重启服务器通常会清除缓存)。
此外你可以 - 喜欢 set(..)
it; as is specified in the documentation:
The basic interface is
set(key, value, timeout)
andget(key)
(..)
The
timeout
argument is optional and defaults to thetimeout
argument of the appropriate backend in theCACHES
setting (explained above). It’s the number of seconds the value should be stored in the cache. Passing inNone
fortimeout
will cache the value forever. A timeout of 0 won’t cache the value.
redis_time_cache = 180 # seconds
cache.set(key, data, redis_time_cache)
- 关键='my_key'
- 数据 = JSON 或 STRING
- redis_time_cache = 超时