简体   繁体   English

在Django中使用会话

[英]Using sessions in Django

I'm using sessions in Django to store login user information as well as some other information. 我在Django中使用会话来存储登录用户信息以及一些其他信息。 I've been reading through the Django session website and still have a few questions. 我一直在浏览Django会话网站,但仍然有一些问题。

From the Django website: 来自Django网站:

By default, Django stores sessions in your database (using the model django.contrib.sessions.models.Session ). 默认情况下,Django将会话存储在您的数据库中(使用模型django.contrib.sessions.models.Session )。 Though this is convenient, in some setups it's faster to store session data elsewhere, so Django can be configured to store session data on your filesystem or in your cache. 虽然这很方便,但在某些设置中,将会话数据存储在其他地方的速度更快,因此可以将Django配置为在文件系统或缓存中存储会话数据。

Also: 也:

For persistent, cached data, set SESSION_ENGINE to django.contrib.sessions.backends.cached_db . 对于持久性缓存数据,请将SESSION_ENGINE设置为django.contrib.sessions.backends.cached_db This uses a write-through cache – every write to the cache will also be written to the database. 这使用直写高速缓存 - 每次写入高速缓存也将写入数据库。 Session reads only use the database if the data is not already in the cache. 如果数据尚未存在于缓存中,则会话只读使用数据库。

Is there a good rule of thumb for which one to use? 有一个很好的经验法则可以使用哪一个? cached_db seems like it would always be a better choice because best case, the data is in the cache, and worst case it's in the database where it would be anyway. cached_db似乎总是一个更好的选择,因为最好的情况是,数据在缓存中,最坏的情况是它在数据库中无论如何都是如此。 The one downside is I have to setup memcached. 一个缺点是我必须设置memcached。

By default, SESSION_EXPIRE_AT_BROWSER_CLOSE is set to False , which means session cookies will be stored in users' browsers for as long as SESSION_COOKIE_AGE . 默认情况下, SESSION_EXPIRE_AT_BROWSER_CLOSE设置为False ,这意味着会话cookie将存储在用户的浏览器中,持续时间为SESSION_COOKIE_AGE Use this if you don't want people to have to log in every time they open a browser. 如果您不希望每次打开浏览器时都必须登录,请使用此选项。

Is it possible to have both, the session expire at the browser close AND give an age? 是否可以同时使用,会话在浏览器关闭时到期并给出年龄?

If value is an integer, the session will expire after that many seconds of inactivity. 如果value是一个整数,则会话将在该多次不活动后过期。 For example, calling request.session.set_expiry(300) would make the session expire in 5 minutes. 例如,调用request.session.set_expiry(300)会使会话在5分钟后到期。

What is considered "inactivity"? 什么被认为是“不活跃”?

If you're using the database backend, note that session data can accumulate in the django_session database table and Django does not provide automatic purging. 如果您正在使用数据库后端,请注意会话数据可以在django_session数据库表中累积,而Django不提供自动清除。 Therefore, it's your job to purge expired sessions on a regular basis. 因此,您的工作是定期清除过期的会话。

So that means, even if the session is expired there are still records in my database. 这意味着,即使会话过期,我的数据库中仍有记录。 Where exactly would one put code to "purge the db"? 究竟哪里会把代码放到“清除数据库”? I feel like you would need a seperate thread to just go through the db every once in awhile (Every hour?) and delete any expired sessions. 我觉得你需要一个单独的线程来每隔一段时间(每小时?)浏览数据库并删除任何过期的会话。

Is there a good rule of thumb for which one to use? 有一个很好的经验法则可以使用哪一个?

No. 没有。

Cached_db seems like it would always be a better choice ... Cached_db似乎永远是一个更好的选择......

That's fine. 没关系。

In some cases, there a many Django (and Apache) processes querying a common database. 在某些情况下,有许多Django(和Apache)进程查询公共数据库。 mod_wsgi allows a lot of scalability this way. mod_wsgi以这种方式允许很多可伸缩性。 The cache doesn't help much because the sessions are distributed randomly among the Apache (and Django) processes. 缓存没有多大帮助,因为会话是在Apache(和Django)进程之间随机分布的。

Is it possible to have both, the session expire at the browser close AND give an age? 是否可以同时使用,会话在浏览器关闭时到期并给出年龄?

Don't see why not. 不明白为什么不。

What is considered "inactivity"? 什么被认为是“不活跃”?

I assume you're kidding. 我假设你在开玩笑。 "activity" is -- well -- activity. “活动”是 - 好 - 活动。 You know. 你懂。 Stuff happening in Django. 发生在Django的东西。 A GET or POST request that Django can see. Django可以看到的GET或POST请求。 What else could it be? 还有什么呢?

Where exactly would one put code to "purge the db"? 究竟哪里会把代码放到“清除数据库”?

Put it in crontab or something similar. 把它放在crontab或类似的东西。

I feel like you would need a seperate thread to just go through the db every once in awhile (Every hour?) 我觉得你需要一个单独的线程来每隔一段时间(每小时?)通过数据库

Forget threads (please). 忘记线程(请)。 It's a separate process. 这是一个单独的过程。 Once a day is fine. 一天一天好。 How many sessions do you think you'll have? 你认为你会有多少次?

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

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