From 5ab8ff4fde2342b9cd0c5c4425f7863bfd17ac58 Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Wed, 8 Nov 2023 16:49:38 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=A0=B9=E6=8D=AEwebsocket=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=B4=BB=E8=B7=83=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/audits/models.py | 5 +++-- apps/audits/serializers.py | 2 +- apps/notifications/ws.py | 8 +++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/apps/audits/models.py b/apps/audits/models.py index 11f3d93f9..97496102b 100644 --- a/apps/audits/models.py +++ b/apps/audits/models.py @@ -4,7 +4,7 @@ from datetime import timedelta from importlib import import_module from django.conf import settings -from django.core.cache import caches +from django.core.cache import caches, cache from django.db import models from django.db.models import Q from django.utils import timezone @@ -278,7 +278,8 @@ class UserSession(models.Model): @property def is_active(self): - return caches.sismember(WS_SESSION_KEY, self.key) + redis_client = cache.client.get_client() + return redis_client.sismember(WS_SESSION_KEY, self.key) @property def date_expired(self): diff --git a/apps/audits/serializers.py b/apps/audits/serializers.py index 472f6ae28..7943e02a0 100644 --- a/apps/audits/serializers.py +++ b/apps/audits/serializers.py @@ -177,7 +177,7 @@ class UserSessionSerializer(serializers.ModelSerializer): fields_mini = ['id'] fields_small = fields_mini + [ 'type', 'ip', 'city', 'user_agent', 'user', 'is_current_user_session', - 'backend', 'backend_display', 'date_created', 'date_expired' + 'backend', 'backend_display', 'is_active', 'date_created', 'date_expired' ] fields = fields_small extra_kwargs = { diff --git a/apps/notifications/ws.py b/apps/notifications/ws.py index bf9e04bdd..019b0b409 100644 --- a/apps/notifications/ws.py +++ b/apps/notifications/ws.py @@ -1,7 +1,7 @@ import json from channels.generic.websocket import JsonWebsocketConsumer -from django.core.cache import caches +from django.core.cache import cache from common.db.utils import safe_db_connection from common.utils import get_logger @@ -21,7 +21,8 @@ class SiteMsgWebsocket(JsonWebsocketConsumer): if user.is_authenticated: self.accept() session = self.scope['session'] - caches.sadd(WS_SESSION_KEY, session.session_key) + redis_client = cache.client.get_client() + redis_client.sadd(WS_SESSION_KEY, session.session_key) self.sub = self.watch_recv_new_site_msg() else: self.close() @@ -66,4 +67,5 @@ class SiteMsgWebsocket(JsonWebsocketConsumer): return self.sub.unsubscribe() session = self.scope['session'] - caches.srem(WS_SESSION_KEY, session.session_key) + redis_client = cache.client.get_client() + redis_client.srem(WS_SESSION_KEY, session.session_key)