This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/session/session.go

39 lines
740 B
Go
Raw Normal View History

2021-01-16 22:00:17 +00:00
package session
import (
"sync"
)
2021-01-18 04:56:56 +00:00
// Used to maintain a cache of user specific jwt secrets
// This will prevent DB lookups on every request
2021-01-16 22:00:17 +00:00
type SessionManager struct {
2021-01-18 04:56:56 +00:00
mutex sync.Mutex
values map[string]string
2021-01-16 22:00:17 +00:00
}
func NewMgr() *SessionManager {
return &SessionManager{}
}
func (sm *SessionManager) Set(key, value string) {
2021-01-18 04:56:56 +00:00
sm.mutex.Lock()
sm.values[key] = value
sm.mutex.Unlock()
2021-01-16 22:00:17 +00:00
}
func (sm *SessionManager) Get(key string) string {
2021-01-18 04:56:56 +00:00
sm.mutex.Lock()
defer sm.mutex.Unlock()
return sm.values[key]
2021-01-16 22:00:17 +00:00
}
func (sm *SessionManager) Delete(key string) {
2021-01-18 04:56:56 +00:00
sm.mutex.Lock()
defer sm.mutex.Unlock()
_, exists := sm.values[key]
2021-01-18 21:24:28 +00:00
if !exists {
return
}
delete(sm.values, key)
2021-01-16 22:00:17 +00:00
}