forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionstate.go
More file actions
105 lines (89 loc) · 2.52 KB
/
sessionstate.go
File metadata and controls
105 lines (89 loc) · 2.52 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package session
import (
"fmt"
"strconv"
"sync"
"time"
)
var sessionStatePool sync.Pool
func init() {
sessionStatePool = sync.Pool{
New: func() interface{} {
return &SessionState{}
},
}
}
//session state
type SessionState struct {
sessionId string //session id
timeAccessed time.Time //last access time
values map[interface{}]interface{} //session store
lock *sync.RWMutex
store SessionStore
}
func NewSessionState(store SessionStore, sessionId string, values map[interface{}]interface{}) *SessionState {
state := sessionStatePool.Get().(*SessionState)
state.reset(store, sessionId, values, time.Now())
return state
}
// Set set key-value to current state
func (state *SessionState) reset(store SessionStore, sessionId string, values map[interface{}]interface{}, accessTime time.Time) {
state.values = values
state.sessionId = sessionId
state.timeAccessed = accessTime
state.store = store
state.lock = new(sync.RWMutex)
}
// Set set key-value to current state
func (state *SessionState) Set(key, value interface{}) error {
state.lock.Lock()
defer state.lock.Unlock()
state.values[key] = value
return state.store.SessionUpdate(state)
}
// Get get value by key in current state
func (state *SessionState) Get(key interface{}) interface{} {
state.lock.RLock()
defer state.lock.RUnlock()
if v, ok := state.values[key]; ok {
return v
}
return nil
}
// Get get value as string by key in current state
func (state *SessionState) GetString(key interface{}) string {
v := state.Get(key)
return fmt.Sprint(v)
}
// Get get value as int by key in current state
func (state *SessionState) GetInt(key interface{}) int {
v, _ := strconv.Atoi(state.GetString(key))
return v
}
// Get get value as int64 by key in current state
func (state *SessionState) GetInt64(key interface{}) int64 {
v, _ := strconv.ParseInt(state.GetString(key), 10, 64)
return v
}
// Remove remove value by key in current state
func (state *SessionState) Remove(key interface{}) error {
state.lock.Lock()
defer state.lock.Unlock()
delete(state.values, key)
return nil
}
// Clear clear all values in current store
func (state *SessionState) Clear() error {
state.lock.Lock()
defer state.lock.Unlock()
state.values = make(map[interface{}]interface{})
return nil
}
// SessionID get this id in current state
func (state *SessionState) SessionID() string {
return state.sessionId
}
// Count get all item's count in current state
func (state *SessionState) Count() int {
return len(state.values)
}