使Django中的特定会话变量过期
Expiring specific session variable in Django
通过 request.session.set_expiry(seconds_until_end_of_day)
或 SESSION_COOKIE_AGE
很容易使 request.session 字典过期。
但是如何设置特定会话变量的到期时间?例如。我已经为未授权用户分配了 temp_id
。我需要这个 temp_id
在 30 分钟内不复存在。实现该目标的最佳、最高效的方法是什么?
不如把它放到缓存里怎么样
from django.core.cache import cache
cache.set('{0}_temp_id'.format(request.session.session_key, value, timeout
这与用户唯一关联并自动过期。无需复杂的中间件
通过 request.session.set_expiry(seconds_until_end_of_day)
或 SESSION_COOKIE_AGE
很容易使 request.session 字典过期。
但是如何设置特定会话变量的到期时间?例如。我已经为未授权用户分配了 temp_id
。我需要这个 temp_id
在 30 分钟内不复存在。实现该目标的最佳、最高效的方法是什么?
不如把它放到缓存里怎么样
from django.core.cache import cache
cache.set('{0}_temp_id'.format(request.session.session_key, value, timeout
这与用户唯一关联并自动过期。无需复杂的中间件