forked from go-session/session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
281 lines (237 loc) · 5.66 KB
/
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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package session
import (
"container/list"
"context"
"sync"
"time"
)
var (
_ ManagerStore = &memoryStore{}
_ Store = &store{}
now = time.Now
)
// ManagerStore Management of session storage, including creation, update, and delete operations
type ManagerStore interface {
// Check the session store exists
Check(ctx context.Context, sid string) (bool, error)
// Create a session store and specify the expiration time (in seconds)
Create(ctx context.Context, sid string, expired int64) (Store, error)
// Update a session store and specify the expiration time (in seconds)
Update(ctx context.Context, sid string, expired int64) (Store, error)
// Delete a session store
Delete(ctx context.Context, sid string) error
// Use sid to replace old sid and return session store
Refresh(ctx context.Context, oldsid, sid string, expired int64) (Store, error)
// Close storage, release resources
Close() error
}
// Store A session id storage operation
type Store interface {
// Get a session storage context
Context() context.Context
// Get the current session id
SessionID() string
// Set session value, call save function to take effect
Set(key string, value interface{})
// Get session value
Get(key string) (interface{}, bool)
// Delete session value, call save function to take effect
Delete(key string) interface{}
// Save session data
Save() error
// Clear all session data
Flush() error
}
// NewMemoryStore create an instance of a memory store
func NewMemoryStore() ManagerStore {
mstore := &memoryStore{
data: make(map[string]*dataItem),
list: list.New(),
ticker: time.NewTicker(time.Second),
}
mstore.pool = sync.Pool{
New: func() interface{} {
return newStore(mstore)
},
}
go mstore.gc()
return mstore
}
type dataItem struct {
sid string
expiredAt time.Time
values map[string]interface{}
}
func newDataItem(sid string, values map[string]interface{}, expired int64) *dataItem {
return &dataItem{
sid: sid,
expiredAt: now().Add(time.Duration(expired) * time.Second),
values: values,
}
}
type memoryStore struct {
sync.RWMutex
pool sync.Pool
data map[string]*dataItem
list *list.List
ticker *time.Ticker
}
func (s *memoryStore) gc() {
for range s.ticker.C {
s.RLock()
e := s.list.Front()
s.RUnlock()
for e != nil {
item := e.Value.(*dataItem)
if item.expiredAt.Before(now()) {
s.Lock()
s.list.Remove(e)
delete(s.data, item.sid)
e = e.Next()
s.Unlock()
} else {
break
}
}
}
}
func (s *memoryStore) save(sid string, values map[string]interface{}, expired int64) {
s.Lock()
defer s.Unlock()
if item, ok := s.data[sid]; ok {
item.values = values
return
}
item := newDataItem(sid, values, expired)
s.data[sid] = item
s.list.PushBack(item)
}
func (s *memoryStore) Check(_ context.Context, sid string) (bool, error) {
s.RLock()
item, ok := s.data[sid]
s.RUnlock()
if ok && item.expiredAt.After(now()) {
return true, nil
}
return false, nil
}
func (s *memoryStore) Create(ctx context.Context, sid string, expired int64) (Store, error) {
store := s.pool.Get().(*store)
store.reset(ctx, sid, expired, nil)
return store, nil
}
func (s *memoryStore) Update(ctx context.Context, sid string, expired int64) (Store, error) {
store := s.pool.Get().(*store)
s.Lock()
defer s.Unlock()
item, ok := s.data[sid]
if !ok {
store.reset(ctx, sid, expired, nil)
return store, nil
}
item.expiredAt = now().Add(time.Duration(expired) * time.Second)
for e := s.list.Front(); e != nil; e = e.Next() {
if e.Value.(*dataItem).sid == sid {
s.list.MoveToBack(e)
break
}
}
store.reset(ctx, sid, expired, item.values)
return store, nil
}
func (s *memoryStore) delete(sid string) {
delete(s.data, sid)
for e := s.list.Front(); e != nil; e = e.Next() {
if e.Value.(*dataItem).sid == sid {
s.list.Remove(e)
break
}
}
}
func (s *memoryStore) Delete(_ context.Context, sid string) error {
s.Lock()
defer s.Unlock()
s.delete(sid)
return nil
}
func (s *memoryStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (Store, error) {
store := s.pool.Get().(*store)
s.Lock()
defer s.Unlock()
item, ok := s.data[oldsid]
if !ok {
store.reset(ctx, sid, expired, nil)
return store, nil
}
newItem := newDataItem(sid, item.values, expired)
s.data[sid] = newItem
s.list.PushBack(newItem)
s.delete(oldsid)
store.reset(ctx, sid, expired, newItem.values)
return store, nil
}
func (s *memoryStore) Close() error {
s.ticker.Stop()
return nil
}
func newStore(mstore *memoryStore) *store {
return &store{mstore: mstore}
}
type store struct {
mstore *memoryStore
sync.RWMutex
ctx context.Context
sid string
expired int64
values map[string]interface{}
}
func (s *store) reset(ctx context.Context, sid string, expired int64, values map[string]interface{}) {
if values == nil {
values = make(map[string]interface{})
}
s.ctx = ctx
s.sid = sid
s.expired = expired
s.values = values
}
func (s *store) Context() context.Context {
return s.ctx
}
func (s *store) SessionID() string {
return s.sid
}
func (s *store) Set(key string, value interface{}) {
s.Lock()
s.values[key] = value
s.Unlock()
}
func (s *store) Get(key string) (interface{}, bool) {
s.RLock()
val, ok := s.values[key]
s.RUnlock()
return val, ok
}
func (s *store) Delete(key string) interface{} {
s.RLock()
v, ok := s.values[key]
s.RUnlock()
if ok {
s.Lock()
delete(s.values, key)
s.Unlock()
}
return v
}
func (s *store) Flush() error {
s.Lock()
s.values = make(map[string]interface{})
s.Unlock()
return s.Save()
}
func (s *store) Save() error {
s.RLock()
values := s.values
s.RUnlock()
s.mstore.save(s.sid, values, s.expired)
return nil
}