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

41 lines
790 B
Go
Raw Normal View History

2021-01-16 22:00:17 +00:00
package session
import (
2021-02-04 20:31:07 +00:00
"sync"
2021-01-16 22:00:17 +00:00
)
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-02-04 20:31:07 +00:00
// May not actually be needed. Refresh Token is the only
// token that will require proactive DB lookups.
2021-01-16 22:00:17 +00:00
type SessionManager struct {
2021-02-04 20:31:07 +00:00
mutex sync.Mutex
values map[string]string
2021-01-16 22:00:17 +00:00
}
func NewMgr() *SessionManager {
2021-02-04 20:31:07 +00:00
return &SessionManager{}
2021-01-16 22:00:17 +00:00
}
func (sm *SessionManager) Set(key, value string) {
2021-02-04 20:31:07 +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-02-04 20:31:07 +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-02-04 20:31:07 +00:00
sm.mutex.Lock()
defer sm.mutex.Unlock()
_, exists := sm.values[key]
if !exists {
return
}
delete(sm.values, key)
2021-01-16 22:00:17 +00:00
}