You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
919 B
46 lines
919 B
package api
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type SessionStore struct {
|
|
sessions map[string]*Session
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func NewSessionStore() *SessionStore {
|
|
return &SessionStore{
|
|
sessions: make(map[string]*Session),
|
|
}
|
|
}
|
|
|
|
func (store *SessionStore) Set(sessionID string, session *Session) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.sessions[sessionID] = session
|
|
}
|
|
|
|
func (store *SessionStore) Get(sessionID string) (*Session, bool) {
|
|
store.mu.RLock()
|
|
defer store.mu.RUnlock()
|
|
session, ok := store.sessions[sessionID]
|
|
return session, ok
|
|
}
|
|
|
|
func (store *SessionStore) Delete(sessionID string) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
delete(store.sessions, sessionID)
|
|
}
|
|
|
|
func (store *SessionStore) CleanupSessions() {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
for id, session := range store.sessions {
|
|
if time.Since(session.LastAccessed) > 24*time.Hour {
|
|
delete(store.sessions, id)
|
|
}
|
|
}
|
|
}
|
|
|