diff --git a/cache/user.go b/cache/user.go index 99f23dd..4607eee 100644 --- a/cache/user.go +++ b/cache/user.go @@ -26,7 +26,7 @@ func init() { log = logger.Logger() userCache = make(map[string]*UserWithExpired) - ticker := time.NewTicker(time.Hour) + ticker := time.NewTicker(time.Minute) go func() { for { diff --git a/handler/signup/signup.go b/handler/signup/signup.go index 804a2a5..eb68f59 100644 --- a/handler/signup/signup.go +++ b/handler/signup/signup.go @@ -50,7 +50,7 @@ func init() { for _, data := range VerifyUser { data.mu.Lock() - if currentTime.Sub(data.CreateTime) > time.Minute*1 { + if currentTime.Sub(data.CreateTime) > time.Minute*10 { delete(VerifyUser, data.Code) delete(VerifyEmail, data.User.Email) cacheClean++ diff --git a/session/session.go b/session/session.go index 99fcaef..d0fc312 100644 --- a/session/session.go +++ b/session/session.go @@ -1,6 +1,8 @@ package session import ( + "fmt" + "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/types" "net/http" "strconv" @@ -11,9 +13,10 @@ import ( ) type Session struct { - ID string - Values map[string]interface{} - mu sync.Mutex + ID string + Values map[string]interface{} + CreateTime time.Time + mu sync.Mutex } type SessionInfo struct { @@ -28,6 +31,7 @@ type SessionInfo struct { } type UserStatus string +type SessionNotFoundError struct{} const ( Authorized UserStatus = "authorized" @@ -37,8 +41,34 @@ const ( var GlobalSessionStore = make(map[string]*Session) var UserSessionInfoList = make(map[string]map[string]*SessionInfo) +var log *logger.AggregatedLogger -type SessionNotFoundError struct{} +func init() { + log = logger.Logger() + + ticker := time.NewTicker(time.Minute) + go func() { + for { + <-ticker.C + currentTime := time.Now() + cacheClean := 0 + cleanID := utils.GenerateRandomString(10) + log.Info(fmt.Sprintf("Cache cleanup [Session] [%s] initiated at %02d:%02d:%02d", cleanID, currentTime.Hour(), currentTime.Minute(), currentTime.Second())) + + for _, data := range GlobalSessionStore { + data.mu.Lock() + if currentTime.Sub(data.CreateTime) > time.Hour*24*7 { + RemoveSessionInfo(data.Values["user"].(types.User).Email, data.ID) + delete(GlobalSessionStore, data.ID) + cacheClean++ + } + data.mu.Unlock() + } + + log.Info(fmt.Sprintf("Cache cleanup [Session] [%s] completed: %d entries removed. Finished at %s", cleanID, cacheClean, time.Since(currentTime))) + } + }() +} func (e *SessionNotFoundError) Error() string { return "session not found"