From e09008bcb7bf081e585a15dd8fd3e0730ee61085 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Sat, 13 Jan 2024 18:52:11 -0600 Subject: [PATCH] Optimize user log --- judge/user_log.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/judge/user_log.py b/judge/user_log.py index b8aec43..ab4ee53 100644 --- a/judge/user_log.py +++ b/judge/user_log.py @@ -1,5 +1,6 @@ from django.utils.timezone import now from django.conf import settings +from django.core.cache import cache from judge.models import Profile @@ -15,11 +16,14 @@ class LogUserAccessMiddleware(object): hasattr(request, "user") and request.user.is_authenticated and not getattr(request, "no_profile_update", False) + and not cache.get(f"user_log_update_{request.user.id}") ): updates = {"last_access": now()} # Decided on using REMOTE_ADDR as nginx will translate it to the external IP that hits it. if request.META.get(settings.META_REMOTE_ADDRESS_KEY): updates["ip"] = request.META.get(settings.META_REMOTE_ADDRESS_KEY) Profile.objects.filter(user_id=request.user.pk).update(**updates) + cache.set(f"user_log_update_{request.user.id}", True, 120) + print("UPDATE", updates) return response