在Django中过期的特定会话变量 [英] Expiring specific session variable in Django

查看:49
本文介绍了在Django中过期的特定会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 request.session.set_expiry(seconds_until_end_of_day)或通过 SESSION_COOKIE_AGE ,可以很容易地使request.session字典过期.

It's easy to expire the request.session dictionary via request.session.set_expiry(seconds_until_end_of_day) or via SESSION_COOKIE_AGE.

但是如何设置特定会话变量的有效期?例如.我为一个未认证用户分配了 temp_id .我需要这个 temp_id 在30分钟内不复存在.实现这一目标的最好,最高效的方法是什么?

But how do I set the expiry of a specific session variable? E.g. I've alloted an unauth user a temp_id. I need this temp_id to cease to exist in 30 mins. What's the best, most performant way to achieve that?

推荐答案

如何将其放在缓存中

from django.core.cache import cache
cache.set('{0}_temp_id'.format(request.session.session_key, value, timeout

这与用户唯一关联,并自动过期.无需复杂的中间件

This is uniquely associated with the user and automatically expires. No need for complicated middleware

这篇关于在Django中过期的特定会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆