-
Notifications
You must be signed in to change notification settings - Fork 4
/
mongo.go
265 lines (227 loc) · 5.49 KB
/
mongo.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
package mongo
import (
"context"
"sync"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
session "github.com/go-session/session/v3"
jsoniter "github.com/json-iterator/go"
)
var (
_ session.ManagerStore = &managerStore{}
_ session.Store = &store{}
jsonMarshal = jsoniter.Marshal
jsonUnmarshal = jsoniter.Unmarshal
)
// NewStore Create an instance of a mongo store
func NewStore(url, dbName, cName string) session.ManagerStore {
session, err := mgo.Dial(url)
if err != nil {
panic(err)
}
return newManagerStore(session, dbName, cName)
}
// NewStoreWithSession Create an instance of a mongo store
func NewStoreWithSession(session *mgo.Session, dbName, cName string) session.ManagerStore {
return newManagerStore(session, dbName, cName)
}
func newManagerStore(session *mgo.Session, dbName, cName string) *managerStore {
err := session.DB(dbName).C(cName).EnsureIndex(mgo.Index{
Key: []string{"expired_at"},
ExpireAfter: time.Second,
})
if err != nil {
panic(err)
}
return &managerStore{
session: session,
dbName: dbName,
cName: cName,
}
}
type managerStore struct {
session *mgo.Session
dbName string
cName string
}
func (s *managerStore) getValue(sid string) (string, error) {
session := s.session.Clone()
defer session.Close()
var item sessionItem
err := session.DB(s.dbName).C(s.cName).FindId(sid).One(&item)
if err != nil {
if err == mgo.ErrNotFound {
return "", nil
}
return "", err
} else if item.ExpiredAt.Before(time.Now()) {
return "", nil
}
return item.Value, nil
}
func (s *managerStore) parseValue(value string) (map[string]interface{}, error) {
var values map[string]interface{}
if len(value) > 0 {
err := jsonUnmarshal([]byte(value), &values)
if err != nil {
return nil, err
}
}
return values, nil
}
func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
val, err := s.getValue(sid)
if err != nil {
return false, err
}
return val != "", nil
}
func (s *managerStore) Create(ctx context.Context, sid string, expired int64) (session.Store, error) {
return newStore(ctx, s, sid, expired, nil), nil
}
func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (session.Store, error) {
value, err := s.getValue(sid)
if err != nil {
return nil, err
} else if value == "" {
return newStore(ctx, s, sid, expired, nil), nil
}
session := s.session.Clone()
defer session.Close()
err = session.DB(s.dbName).C(s.cName).UpdateId(sid, bson.M{
"$set": bson.M{
"expired_at": time.Now().Add(time.Duration(expired) * time.Second),
},
})
if err != nil {
return nil, err
}
values, err := s.parseValue(value)
if err != nil {
return nil, err
}
return newStore(ctx, s, sid, expired, values), nil
}
func (s *managerStore) Delete(_ context.Context, sid string) error {
session := s.session.Clone()
defer session.Close()
return session.DB(s.dbName).C(s.cName).RemoveId(sid)
}
func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (session.Store, error) {
value, err := s.getValue(oldsid)
if err != nil {
return nil, err
} else if value == "" {
return newStore(ctx, s, sid, expired, nil), nil
}
session := s.session.Clone()
defer session.Close()
c := session.DB(s.dbName).C(s.cName)
_, err = c.UpsertId(sid, &sessionItem{
ID: sid,
Value: value,
ExpiredAt: time.Now().Add(time.Duration(expired) * time.Second),
})
if err != nil {
return nil, err
}
err = c.RemoveId(oldsid)
if err != nil {
return nil, err
}
values, err := s.parseValue(value)
if err != nil {
return nil, err
}
return newStore(ctx, s, sid, expired, values), nil
}
func (s *managerStore) Close() error {
s.session.Close()
return nil
}
func newStore(ctx context.Context, s *managerStore, sid string, expired int64, values map[string]interface{}) *store {
if values == nil {
values = make(map[string]interface{})
}
return &store{
session: s.session,
dbName: s.dbName,
cName: s.cName,
ctx: ctx,
sid: sid,
expired: expired,
values: values,
}
}
type store struct {
sync.RWMutex
ctx context.Context
session *mgo.Session
dbName string
cName string
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 {
var value string
s.RLock()
if len(s.values) > 0 {
buf, err := jsonMarshal(s.values)
if err != nil {
s.RUnlock()
return err
}
value = string(buf)
}
s.RUnlock()
session := s.session.Clone()
defer session.Close()
_, err := session.DB(s.dbName).C(s.cName).UpsertId(s.sid, &sessionItem{
ID: s.sid,
Value: value,
ExpiredAt: time.Now().Add(time.Duration(s.expired) * time.Second),
})
return err
}
// Data items stored in mongo
type sessionItem struct {
ID string `bson:"_id"`
Value string `bson:"value"`
ExpiredAt time.Time `bson:"expired_at"`
}