从Redis(Django)删除会话 [英] Removing sessions from Redis (Django)

查看:187
本文介绍了从Redis(Django)删除会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django和Redis作为会话引擎(也是Celery,但这是别的)。它的工作完美,我可以看到速度的提高。

  SESSION_ENGINE ='django.contrib.sessions.backends.cached_db'

我有一个脚本,每分钟运行一次,通过一些方法检查活动用户,如果用户最近没有活动,则会话获取除去。这是针对客户的跟踪需求而完成的。



此脚本正常工作,直到我切换到Redis为会话引擎。会话确实从数据库中删除,但不是从Redis。我没有使用任何Django内置的方法,但我自己的功能:

  def clean_sessions():
stored_sessions = Session.objects.all()
active_users = active_users(minutes = 1)
active_users_ids = [active_users中的用户的user.id]
存储会话中的会话
session_uid = session.get_decoded()。get('_ auth_user_id')
如果不是session_uid:
session.delete()
continue
如果session_uid不在active_users_ids中:
user = User.objects.get(pk = session_uid)
## ##
之间的代码session.delete()

我的问题是,如何从缓存中删除会话,所以用户实际上会注销?

解决方案

这不是很简单,但我能够解决它。我从文件导入了这个文件我有我的clean_sessions():

  from importlib import import_module 
from django.conf import设置

然后,在函数内部,我加载了SessionStore对象:

  SessionStore = import_module(settings.SESSION_ENGINE).SessionStore 

从那里删除会话非常简单,留下这样的方法:

  def clean_sessions )
stored_sessions = Session.objects.all()
active_users = Request.objects.active_users(seconds = 15)
active_users_ids = [active_users中的用户的user.id]
对于session_session中的会话:
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
s = SessionStore(session_key = session.session_key)
session_uid = session.get_decoded()。get('_ auth_user_id'
如果不是session_uid:
s .delete()
continue
如果session_uid不在active_users_ids中:
##一些代码##
s.delete()
/ pre>

从任何会话引擎加载正确的SessionStore非常重要,否则将无法从两个地方(DB和缓存)中删除它。


I'm using Django and Redis as session engine (also Celery, but that is something else). It works perfectly and I can see an improvement in speed.

SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

I have a script that runs every minute to check the active users through some methods and if the user hasn't been active in the latest minute, then the session gets removed. This is done for customer's tracking needs.

This script was working perfectly until I switched to Redis as a session engine. The session gets indeed removed from the DB but not from Redis. I'm not using any Django built-in method for this but my own function:

def clean_sessions():
    stored_sessions = Session.objects.all()
    active_users = active_users(minutes=1)
    active_users_ids = [user.id for user in active_users]
    for session in stored_sessions:
        session_uid = session.get_decoded().get('_auth_user_id')
        if not session_uid:
            session.delete()
            continue
        if session_uid not in active_users_ids:
            user = User.objects.get(pk=session_uid)
            ## some code between ##
            session.delete()

My question is, how do I remove the session from the cache as well, so the user is actually logged out?

解决方案

It wasn't very straightforward but I was able to fix it. I imported this from the file I have my clean_sessions():

from importlib import import_module
from django.conf import settings

Then, inside the function, I loaded the SessionStore object:

SessionStore = import_module(settings.SESSION_ENGINE).SessionStore

From there, it was very easy to remove the sessions, leaving the method like this:

def clean_sessions():
    stored_sessions = Session.objects.all()
    active_users = Request.objects.active_users(seconds=15)
    active_users_ids = [user.id for user in active_users]
    for session in stored_sessions:
        SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
        s = SessionStore(session_key=session.session_key)
        session_uid = session.get_decoded().get('_auth_user_id')
        if not session_uid:
            s.delete()
            continue
        if session_uid not in active_users_ids:
            ## some code ##
            s.delete()

It's very important to load the right SessionStore from whatever session engine you're using, otherwise it will fail to remove it from both places (DB and cache).

这篇关于从Redis(Django)删除会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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