/
session.go
62 lines (49 loc) · 1.29 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
package goKLC
import "time"
type SessionInterface interface {
Set(key string, value interface{})
Get(key string, defaultValue interface{}) interface{}
Delete()
}
type Session struct {
Id string
Key string
Value interface{}
cookie string
maxDuration time.Duration
Duration time.Duration
}
func NewSession(r *Request) *Session {
cookie := NewCookie()
cookieName := _config.Get("SessionName", "goKLCSession").(string)
cookie.Get(r, cookieName)
session := &Session{
Id: _app.GetSessionKey(),
cookie: cookie.Value,
maxDuration: time.Second * time.Duration(cookie.Duration),
}
return session
}
func (s *Session) Set() {
collection := _sessionCollector.GetCollection(s.cookie)
collection.Set(s)
if s.Duration > 0 {
if s.Duration < s.maxDuration && s.Duration != 0 {
time.AfterFunc(s.Duration, s.Delete)
} else {
time.AfterFunc(s.maxDuration, s.Delete)
}
}
}
func (s *Session) Get(key string, defaultValue interface{}) interface{} {
collection := _sessionCollector.GetCollection(s.cookie)
session := collection.Get(key)
if session == nil || session.Value == nil {
return defaultValue
}
return session.Value
}
func (s *Session) Delete() {
collection := _sessionCollector.GetCollection(s.cookie)
collection.Delete(s.Key)
}