-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
232 lines (205 loc) · 4.95 KB
/
struct.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package session
import (
"net/http"
"sync"
"time"
)
//SessionObj struct should not be changed, readonly
type sessionObj struct {
value interface{}
expireTime time.Duration
setTime time.Time
}
func (obj *sessionObj) expired() bool {
if time.Since(obj.setTime) > obj.expireTime && obj.expireTime != 0 {
return false
}
return true
}
//Session struct
type Session struct {
sessionID string
expireTime time.Duration
address string
userAgent string
setTime time.Time
Map map[string]*sessionObj
lock sync.RWMutex
}
func (session *Session) init(sessionID string) {
session.Lock()
defer session.Unlock()
session.sessionID = sessionID
session.Map = make(map[string]*sessionObj)
}
//ExtraInfo set ExtraInfo for auth,
//set "" will not change, can use to get ExtraInfo
func (session *Session) ExtraInfo(address, userAgent string) (string, string) {
if address != "" {
session.Lock()
session.address = address
session.Unlock()
}
if userAgent != "" {
session.Lock()
session.userAgent = userAgent
session.Unlock()
}
session.RLock()
defer session.RUnlock()
return session.address, session.userAgent
}
//ExpireTime set the ExpireTime of session and retuen ExpireTime of session,
//ExpireTime smaller than 0 will not change ExpireTime of session so than can be use to get ExpireTime
func (session *Session) ExpireTime(expireTime time.Duration) time.Duration {
session.refresh()
session.RLock()
defer session.RUnlock()
if expireTime < 0 {
return session.expireTime
}
session.expireTime = expireTime
return session.expireTime
}
//Belong check wether req match the requirement of session
func (session *Session) Belong(req *http.Request) bool {
if req.RemoteAddr == session.address ||
req.UserAgent() == session.userAgent {
return true
}
return false
}
//ID return sessionID
func (session *Session) ID() string {
return session.sessionID
}
//Expired return wether session is expired
func (session *Session) Expired() bool {
session.RLock()
defer session.RUnlock()
if time.Since(session.setTime) > session.expireTime {
session.Map = nil
return true
}
return false
}
func (session *Session) refresh() {
session.Lock()
session.setTime = time.Now()
session.Unlock()
}
//Get return type is string
//if not have key or value type is not string return ""
func (session *Session) Get(key string) (res string, ok bool) {
session.refresh()
session.RLock()
if session.Map == nil {
return "", false
}
Obj, ok := session.Map[key]
if !ok {
return "", false
}
session.RUnlock()
if Obj.expired() {
session.Delete(key)
return "", false
}
if res, ok := Obj.value.(string); ok {
return res, true
}
return "", false
}
//GetInt return type is int
func (session *Session) GetInt(key string) (res int, ok bool) {
session.refresh()
session.RLock()
if session.Map == nil {
return 0, false
}
Obj, ok := session.Map[key]
if !ok {
return 0, false
}
session.RUnlock()
if Obj.expired() {
session.Delete(key)
return 0, false
}
res, ok = Obj.value.(int)
return res, ok
}
//GetTime return type is time.Time
func (session *Session) GetTime(key string) (res time.Time, ok bool) {
session.refresh()
session.lock.RLock()
if session.Map == nil {
return time.Time{}, false
}
Obj, ok := session.Map[key]
if !ok {
return time.Time{}, false
}
session.lock.RUnlock()
if Obj.expired() {
session.Delete(key)
return time.Time{}, false
}
res, ok = Obj.value.(time.Time)
return res, ok
}
//Have if session have key return true
func (session *Session) Have(key string) bool {
session.refresh()
session.lock.RLock()
_, ok := session.Map[key]
session.lock.RUnlock()
return ok
}
//Put string
//set expireTime=0 to enable infinit lifetime
func (session *Session) Put(key string, value string, expireTime time.Duration) {
session.refresh()
session.lock.Lock()
defer session.lock.Unlock()
session.Map[key] = &sessionObj{value: value, expireTime: expireTime, setTime: time.Now()}
}
//PutInt int
//set expireTime=0 to enable infinit lifetime
func (session *Session) PutInt(key string, value int, expireTime time.Duration) {
session.refresh()
session.lock.Lock()
defer session.lock.Unlock()
session.Map[key] = &sessionObj{value: value, expireTime: expireTime, setTime: time.Now()}
}
//PutTime time.Time
//set expireTime=0 to enable infinit lifetime
func (session *Session) PutTime(key string, value time.Time, expireTime time.Duration) {
session.refresh()
session.lock.Lock()
defer session.lock.Unlock()
session.Map[key] = &sessionObj{value: value, expireTime: expireTime, setTime: time.Now()}
}
//Delete key,
//delete will not cause panic
func (session *Session) Delete(key string) {
session.Lock()
delete(session.Map, key)
session.Unlock()
}
//RLock RLock
func (session *Session) RLock() {
session.lock.RLock()
}
//RUnlock RUnlock
func (session *Session) RUnlock() {
session.lock.RUnlock()
}
//Lock Lock
func (session *Session) Lock() {
session.lock.Lock()
}
//Unlock Unlock
func (session *Session) Unlock() {
session.lock.Unlock()
}