-
Notifications
You must be signed in to change notification settings - Fork 96
/
session_store.go
52 lines (40 loc) · 1.03 KB
/
session_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cas
import "sync"
// SessionStore store the session's ticket
// SessionID is retrived from cookies
type SessionStore interface {
// Get the ticket with the session id
Get(sessionID string) (string, bool)
// Set the session with a ticket
Set(sessionID, ticket string) error
// Delete the session
Delete(sessionID string) error
}
// NewMemorySessionStore create a default SessionStore that uses memory
func NewMemorySessionStore() SessionStore {
return &memorySessionStore{
sessions: make(map[string]string),
}
}
type memorySessionStore struct {
mu sync.RWMutex
sessions map[string]string
}
func (m *memorySessionStore) Get(sessionID string) (string, bool) {
m.mu.RLock()
ticket, ok := m.sessions[sessionID]
m.mu.RUnlock()
return ticket, ok
}
func (m *memorySessionStore) Set(sessionID, ticket string) error {
m.mu.Lock()
m.sessions[sessionID] = ticket
m.mu.Unlock()
return nil
}
func (m *memorySessionStore) Delete(sessionID string) error {
m.mu.Lock()
delete(m.sessions, sessionID)
m.mu.Unlock()
return nil
}