清除django_session表中的会话而不注销当前用户 [英] Clearing sessions in django_session table without logging out current users

查看:224
本文介绍了清除django_session表中的会话而不注销当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django中进行会话。

I am working on sessions in Django.

默认情况下,django将会话存储在 django_session

By default, django stores sessions in django_session, I found out there is no way to purge sessions.

尽管 clearsessions 可用于删除行。还建议将其作为cron作业运行。但这意味着注销所有已登录的用户,对吗?

Though clearsessions can be used to delete rows. It is also recommended to run this as a cron job. But doing this means logging out all logged-in users, right?

这是正确的方法吗?

推荐答案

Django 文档声明(强调我):

The Django documentation states (emphasis from me):


清除会话存储区



当用户在上创建新会话时您的网站上,会话数据可以在您的会话存储区中累积
。如果您使用数据库
后端,则 django_session 数据库表将会增加。如果您使用
作为文件后端,则您的临时目录将包含数量不断增加的
文件。

Clearing the session store

As users create new sessions on your website, session data can accumulate in your session store. If you’re using the database backend, the django_session database table will grow. If you’re using the file backend, your temporary directory will contain an increasing number of files.

要了解此问题,请考虑一下发生在数据库
后端。当用户登录时,Django在 django_session
数据库表中添加一行。会话数据
每次更改时,Django都会更新此行。如果用户手动注销,则Django删除该行。但是,如果
用户没有注销,则该行将永远不会被删除。文件后端也会发生类似的
过程。

To understand this problem, consider what happens with the database backend. When a user logs in, Django adds a row to the django_session database table. Django updates this row each time the session data changes. If the user logs out manually, Django deletes the row. But if the user does not log out, the row never gets deleted. A similar process happens with the file backend.

Django不会自动清除过期的会话。
因此,定期清除过期的会话是您的工作
Django为此提供了清理管理命令:
clearsessions 。建议定期在
基础上调用此命令,例如作为日常cron作业

Django does not provide automatic purging of expired sessions. Therefore, it’s your job to purge expired sessions on a regular basis. Django provides a clean-up management command for this purpose: clearsessions. It’s recommended to call this command on a regular basis, for example as a daily cron job.

请注意,缓存后端不会受到此问题的影响,因为
高速缓存会自动删除陈旧数据。 Cookie后端
也不是,因为会话数据是由用户的浏览器存储的。

Note that the cache backend isn’t vulnerable to this problem, because caches automatically delete stale data. Neither is the cookie backend, because the session data is stored by the users’ browsers.

在< a href = https://stackoverflow.com/a/7296233/1218980>遵守A的答案。


可以作为cron作业运行,也可以直接清除过期的会话。

Can be run as a cron job or directly to clean out expired sessions.

因此它不会注销每个用户。

So it won't log off every user.

正如Kevin Christopher Henry在评论和其他您的问题的可能重复 被e4c5标记

As mentioned by Kevin Christopher Henry in a comment and in the other possible duplicate of your question flagged by e4c5.

这篇关于清除django_session表中的会话而不注销当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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