简体   繁体   English

django缓存会话

[英]django cached session

I have two questions: 我有两个问题:

1) I am wondering whether using 1)我想知道是否使用

django.contrib.sessions.backends.cache

for storing sessions really improves performance of a website? 存储会话真的可以提高网站的性能吗? Assuming there are around 25k simultaneous users. 假设有大约25,000个并发用户。 Each user making many database changes (for example browser game). 每个用户进行许多数据库更改(例如浏览器游戏)。 Is the difference even noticable? 差异甚至是显而易见的吗?

2) Again while using cached session (without db): how to check whether given user is logged in (do not allow multiple logins on the same account)? 2)再次使用缓存会话(没有数据库):如何检查给定用户是否已登录(不允许在同一帐户上多次登录)?

re 1. 重新1。

from my experience difference is huge. 从我的经验来看差异很大。 in your scenario, every time session is created or changed - this will generate a db write. 在您的方案中,每次创建或更改会话时 - 这将生成数据库写入。 each time user is accessing a site - a db read will be generated. 每次用户访问站点时,都会生成数据库读取。 moving this out of the DB - saves at least one query per request. 将其移出数据库 - 每个请求至少保存一个查询。

this additional traffic, plus your normal app traffic may easily kill your db performance. 此额外流量加上您的正常应用流量可能会轻易导致数据库性能下降。

re. 回覆。 2. 2。

even for the db backed session there are no clear way to make sure that user is logged once (reading all session data, depickling it and then filtering is not a clear way :) ) 即使对于db支持的会话,也没有明确的方法来确保用户被记录一次(读取所有会话数据,去除它然后过滤不是一个明确的方式:))

to do this I'll probably use cache: each time user is logged in, check if here is a key (eg user:<user_id>) in the cache, stored value will be a session id, and if current session is different then stored one - expire old session, store new session id. 要做到这一点,我可能会使用缓存:每次用户登录时,检查缓存中是否有密钥(例如用户:<user_id>),存储的值将是会话ID,如果当前会话不同则存储一个 - 过期的旧会话,存储新的会话ID。

but even this one will not always work, sometimes browser tabs/windows share same cookies (especially when SESSION_EXPIRE_AT_BROWSER_CLOSE is False - default) and sessions are solely cookie based. 但即使这个也不总是有效,有时浏览器标签/窗口共享相同的cookie(特别是当SESSION_EXPIRE_AT_BROWSER_CLOSE为False时 - 默认)并且会话仅基于cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM