day 3: of trying to rizzzzzzz my gophy mol
This commit is contained in:
parent
815dc130d6
commit
40632a8349
|
|
@ -0,0 +1,35 @@
|
||||||
|
package gopherriz
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Session struct {
|
||||||
|
UserId string
|
||||||
|
ExpiresAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type SessionManager struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
sessions map[string]Session
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SessionManager) Get(key string) (Session, bool) {
|
||||||
|
s.mu.RLock()
|
||||||
|
val, ok := s.sessions[key]
|
||||||
|
s.mu.RUnlock()
|
||||||
|
|
||||||
|
if !ok || time.Now().After(val.ExpiresAt) {
|
||||||
|
return Session{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return val, ok
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SessionManager) Set(session Session, key string) {
|
||||||
|
s.mu.Lock()
|
||||||
|
s.sessions[key] = session
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue