-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.go
81 lines (63 loc) · 1.67 KB
/
Session.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package session
import "sync"
// ession represents the session information
// in a single request & response context.
type Session struct {
id string
data sync.Map
modified bool
}
// New creates a new session with the given ID and data.
func New(sid string, baseData map[string]interface{}) *Session {
session := &Session{
id: sid,
}
for key, value := range baseData {
session.data.Store(key, value)
}
return session
}
// ID returns the session ID.
func (session *Session) ID() string {
return session.id
}
// Get returns the value for the key in this session.
func (session *Session) Get(key string) interface{} {
value, _ := session.data.Load(key)
return value
}
// GetString returns the string value for the key in this session.
func (session *Session) GetString(key string) string {
value := session.Get(key)
if value != nil {
str, ok := value.(string)
if ok {
return str
}
return ""
}
return ""
}
// Set sets the value for the key in this session.
func (session *Session) Set(key string, value interface{}) {
session.data.Store(key, value)
session.modified = true
}
// Delete deletes the the key/value entry in this session.
func (session *Session) Delete(key string) {
session.data.Delete(key)
session.modified = true
}
// Modified indicates whether the session has been modified since it's been retrieved.
func (session *Session) Modified() bool {
return session.modified
}
// Data returns a copy of the underlying session data.
func (session *Session) Data() map[string]interface{} {
newMap := map[string]interface{}{}
session.data.Range(func(key, value interface{}) bool {
newMap[key.(string)] = value
return true
})
return newMap
}