gpt4 book ai didi

python - 在不注销当前用户的情况下清除 django_session 表中的 session

转载 作者:太空狗 更新时间:2023-10-30 00:27:36 26 4
gpt4 key购买 nike

我正在使用 Django 开发 session 。

默认情况下,django 将 session 存储在 django_session 中,我发现没有办法清除 session 。

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

这是正确的做法吗?

最佳答案

Django documentation状态(我强调):

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.

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 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.

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.

Abid A's answer 中找到此链接.

clearsessions命令

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

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

正如 Kevin Christopher Henry 在 comment 中提到的那样在另一个possible duplicate你的问题flagged by e4c5 .

关于python - 在不注销当前用户的情况下清除 django_session 表中的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38753183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com