Add cleanup code to check if session from database is expired or not
This commit is contained in:
parent
2566ec8eaa
commit
87cf3bda7e
|
@ -30,10 +30,18 @@ var sessionCache SessiomMapMu
|
||||||
func Init() error {
|
func Init() error {
|
||||||
sessionCache.SessionMap = make(map[string]*db.Session)
|
sessionCache.SessionMap = make(map[string]*db.Session)
|
||||||
var dbSessions []*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
|
return err
|
||||||
}
|
}
|
||||||
|
timeNow := time.Now()
|
||||||
for _, dbSession := range dbSessions {
|
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.SessionMap[dbSession.ID] = dbSession
|
||||||
}
|
}
|
||||||
sessionCache.Initialized = true
|
sessionCache.Initialized = true
|
||||||
|
|
Loading…
Reference in New Issue
Block a user