From 1ce0dbde0ee7ed203f994664fdb5926ca8c8d293 Mon Sep 17 00:00:00 2001 From: Aaron Kable Date: Mon, 16 Mar 2020 02:09:24 +0000 Subject: [PATCH] stop the notification context profider from hitting the database for each menu hook --- allianceauth/notifications/context_processors.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/allianceauth/notifications/context_processors.py b/allianceauth/notifications/context_processors.py index 75726acb..d151fceb 100644 --- a/allianceauth/notifications/context_processors.py +++ b/allianceauth/notifications/context_processors.py @@ -1,5 +1,10 @@ from .models import Notification - +from django.core.cache import cache def user_notification_count(request): - return {'notifications': len(Notification.objects.filter(user__id=request.user.id).filter(viewed=False))} + notification_count = cache.get("u-note:{}".format(request.user.id), -1) + if notification_count<0: + notification_count = Notification.objects.filter(user__id=request.user.id).filter(viewed=False).count() + cache.set("u-note:{}".format(request.user.id),notification_count,5) + + return {'notifications': notification_count}