dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit d25d358595d5f157ff188f746eb4e65db8f1fa59
parent b354c9db5a7989f82401f454ef53746993cc1d5c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sat, 28 Jan 2023 13:27:12 -0800

Fix notifications cache

Diffstat:
Mpkg/global/global.go | 14+++++++++-----
Mpkg/web/handlers/handlers.go | 2+-
2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/pkg/global/global.go b/pkg/global/global.go @@ -6,14 +6,18 @@ import ( "time" ) -var notifCountCache = cache.NewWithKey[database.UserID, int64](30*time.Second, time.Minute) +var notifCountCache = cache.NewWithKey[string, int64](30*time.Second, time.Minute) -func DeleteUserNotificationCount(userID database.UserID) { - notifCountCache.Delete(userID) +func cacheKey(userID database.UserID, sessionToken string) string { + return userID.String() + "_" + sessionToken +} + +func DeleteUserNotificationCount(userID database.UserID, sessionToken string) { + notifCountCache.Delete(cacheKey(userID, sessionToken)) } func GetUserNotificationCount(userID database.UserID, sessionToken string) int64 { - count, found := notifCountCache.Get(userID) + count, found := notifCountCache.Get(cacheKey(userID, sessionToken)) if found { return count } @@ -22,6 +26,6 @@ func GetUserNotificationCount(userID database.UserID, sessionToken string) int64 if sessionToken != "" { count += database.GetUserSessionNotificationsCount(sessionToken) } - notifCountCache.SetD(userID, count) + notifCountCache.SetD(cacheKey(userID, sessionToken), count) return count } diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -2637,7 +2637,7 @@ func SettingsInboxHandler(c echo.Context) error { data.ActiveTab = "inbox" // Do not fetch inboxes & notifications if logged in under duress if !authUser.IsUnderDuress { - global.DeleteUserNotificationCount(authUser.ID) + global.DeleteUserNotificationCount(authUser.ID, authCookie.Value) data.ChatMessages, _ = database.GetUserChatInboxMessages(authUser.ID) data.Notifications, _ = database.GetUserNotifications(authUser.ID) data.SessionNotifications, _ = database.GetUserSessionNotifications(authCookie.Value)