Django:使用基于Cookie的会话存储时会话缓存未更新 [英] Django: Session Cache not updated when using Cookie Based Session Storage

查看:184
本文介绍了Django:使用基于Cookie的会话存储时会话缓存未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Django Session来缓存一些数据。我在我的web应用程序没有数据库,所以,我使用基于cookie的存储机制。我只能第一次成功地保存数据在会话中。从此,如果我尝试更新会话缓存,它不工作。这是我发现了:

  prior_states = request.session.get(workflow_id,[])
print prior_state>>>,则before_states
if state_id in prior_states:
request.session.update({workflow_id:prior_states [:prior_states.index(state_id)+ 1]})
else:
prior_states.append(state_id)
request.session.update({workflow_id:prior_states})




测试代码




 code>#first request:
print request.session.get(1) - >无
request.session [1] = [101] - > works
print request.session.get(1) - > [101]
#第二个请求:
print request.session.get(1) - > [101]
request.session [1] = [101,102] - > works
print request.session.get(1) - > [101,102]

#第三个请求:
print request.session.get(1) - > [101] - >不能跟随为什么?

提前感谢!

方案

根据Django文档:



https://docs.djangoproject.com/en/1.6/topics/http/sessions/#when-sessions-are-saved


默认情况下,Django仅在会话
被修改时保存到会话数据库 - 字典值已分配或删除




  request.session.modified = True 

要更改此默认行为,请将SESSION_SAVE_EVERY_REQUEST设置设置为True。设置为True时,Django会在每次请求时将会话保存到数据库。



请注意,会话cookie仅在创建或修改会话时发送。如果SESSION_SAVE_EVERY_REQUEST为True,则会在每个请求中发送会话Cookie。



同样,会话cookie的expires部分在每次发送会话cookie时更新。 / p>

I am trying to use Django Session to cache some data. I don't have a database in my web app so, I am using the cookie-based storage mechanism. I am able to successfully save the data in the session only for the first time. Henceforth, if I try to update the session cache it doesn't work. Here is what I had found:

prior_states = request.session.get(workflow_id, [])
print "prior_state >>> ", prior_states
if state_id in prior_states:
    request.session.update({workflow_id: prior_states[:prior_states.index(state_id) + 1]})
else:
     prior_states.append(state_id)
     request.session.update({workflow_id : prior_states})

Test Code

#1st request:
print request.session.get(1) --> None
request.session[1] = [101] --> works
print request.session.get(1) --> [101]
#2nd request:
print request.session.get(1) --> [101]
request.session[1] = [101, 102] --> works
print request.session.get(1) --> [101,102]

#3rd request:
print request.session.get(1) --> [101] --> Can't follow why?

Thanks in advance!

解决方案

According to the Django Documentation:

https://docs.djangoproject.com/en/1.6/topics/http/sessions/#when-sessions-are-saved

By default, Django only saves to the session database when the session has been modified – that is if any of its dictionary values have been assigned or deleted

request.session.modified = True

To change this default behavior, set the SESSION_SAVE_EVERY_REQUEST setting to True. When set to True, Django will save the session to the database on every single request.

Note that the session cookie is only sent when a session has been created or modified. If SESSION_SAVE_EVERY_REQUEST is True, the session cookie will be sent on every request.

Similarly, the expires part of a session cookie is updated each time the session cookie is sent.

这篇关于Django:使用基于Cookie的会话存储时会话缓存未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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