From 87cf3bda7e4080c7ff5fabe39deda429a475001c Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sun, 1 Dec 2024 19:37:41 +0200 Subject: [PATCH] Add cleanup code to check if session from database is expired or not --- tokens/session_cache.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tokens/session_cache.go b/tokens/session_cache.go index 3d191de..d12aa76 100644 --- a/tokens/session_cache.go +++ b/tokens/session_cache.go @@ -30,10 +30,18 @@ var sessionCache SessiomMapMu func Init() error { sessionCache.SessionMap = make(map[string]*db.Session) var dbSessions []*db.Session - if err := db.Connect().Find(&dbSessions).Error; err != nil { + dbc := db.Connect() + if err := dbc.Find(&dbSessions).Error; err != nil { return err } + timeNow := time.Now() for _, dbSession := range dbSessions { + if dbSession.ExpireAt.Unix() < timeNow.Unix() { + if err := dbc.Unscoped().Delete(dbSession).Error; err != nil { + log.Printf("dbc.Unscoped().Delete(dbSession) error: %v\n", err) + } + continue + } sessionCache.SessionMap[dbSession.ID] = dbSession } sessionCache.Initialized = true