stop the notification context profider from hitting the database for each menu hook

This commit is contained in:
Aaron Kable 2020-03-16 02:09:24 +00:00
parent 32e0621b0a
commit 1ce0dbde0e

View File

@ -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}