-
Notifications
You must be signed in to change notification settings - Fork 42
/
store.go
228 lines (192 loc) · 4.98 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
package session
import (
"context"
"sync"
"time"
"github.com/bytedance/gopkg/collection/skipmap"
)
var (
_ ManagerStore = &memoryStore{}
_ Store = &store{}
now = time.Now
)
// 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
}
// 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
}
// Create a new session storage (memory)
func NewMemoryStore() ManagerStore {
mstore := &memoryStore{
ticker: time.NewTicker(time.Second),
data: skipmap.NewString(),
}
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 {
ticker *time.Ticker
data *skipmap.StringMap
}
func (s *memoryStore) gc() {
for range s.ticker.C {
s.data.Range(func(key string, value interface{}) bool {
if item, ok := value.(*dataItem); ok && item.expiredAt.Before(now()) {
s.data.Delete(key)
}
return true
})
}
}
func (s *memoryStore) save(sid string, values map[string]interface{}, expired int64) {
if dt, ok := s.data.Load(sid); ok {
dt.(*dataItem).values = values
return
}
s.data.Store(sid, newDataItem(sid, values, expired))
}
func (s *memoryStore) Check(ctx context.Context, sid string) (bool, error) {
dt, ok := s.data.Load(sid)
if !ok {
return false, nil
}
if item, ok := dt.(*dataItem); ok && item.expiredAt.After(now()) {
return true, nil
}
return false, nil
}
func (s *memoryStore) Create(ctx context.Context, sid string, expired int64) (Store, error) {
return newStore(ctx, s, sid, expired, nil), nil
}
func (s *memoryStore) Update(ctx context.Context, sid string, expired int64) (Store, error) {
dt, ok := s.data.Load(sid)
if !ok {
return newStore(ctx, s, sid, expired, nil), nil
}
item := dt.(*dataItem)
item.expiredAt = now().Add(time.Duration(expired) * time.Second)
s.data.Store(sid, item)
return newStore(ctx, s, sid, expired, item.values), nil
}
func (s *memoryStore) delete(sid string) {
s.data.Delete(sid)
}
func (s *memoryStore) Delete(_ context.Context, sid string) error {
s.delete(sid)
return nil
}
func (s *memoryStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (Store, error) {
dt, ok := s.data.Load(oldsid)
if !ok {
return newStore(ctx, s, sid, expired, nil), nil
}
item := dt.(*dataItem)
newItem := newDataItem(sid, item.values, expired)
s.data.Store(sid, newItem)
s.delete(oldsid)
return newStore(ctx, s, sid, expired, newItem.values), nil
}
func (s *memoryStore) Close() error {
s.ticker.Stop()
return nil
}
func newStore(ctx context.Context, mstore *memoryStore, sid string, expired int64, values map[string]interface{}) *store {
if values == nil {
values = make(map[string]interface{})
}
return &store{
mstore: mstore,
ctx: ctx,
sid: sid,
expired: expired,
values: values,
}
}
type store struct {
sync.RWMutex
mstore *memoryStore
ctx context.Context
sid string
expired int64
values map[string]interface{}
}
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
}