@@ -3,22 +3,21 @@ package mysql
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "encoding/json"
6
7
"fmt"
7
8
"io"
8
9
"os"
9
10
"sync"
10
11
"time"
11
12
12
13
"github.com/go-session/session"
13
- "github.com/json-iterator/go"
14
- "gopkg.in/gorp.v2"
15
14
)
16
15
17
16
var (
18
17
_ session.ManagerStore = & managerStore {}
19
18
_ session.Store = & store {}
20
- jsonMarshal = jsoniter .Marshal
21
- jsonUnmarshal = jsoniter .Unmarshal
19
+ jsonMarshal = json .Marshal
20
+ jsonUnmarshal = json .Unmarshal
22
21
)
23
22
24
23
// NewConfig create mysql configuration instance
@@ -60,7 +59,7 @@ func NewStore(config *Config, tableName string, gcInterval int) session.ManagerS
60
59
// gcInterval Time interval for executing GC (in seconds, default 600)
61
60
func NewStoreWithDB (db * sql.DB , tableName string , gcInterval int ) session.ManagerStore {
62
61
store := & managerStore {
63
- db : & gorp. DbMap { Db : db , Dialect : gorp. MySQLDialect { Encoding : "UTF8" , Engine : "MyISAM" }} ,
62
+ db : db ,
64
63
tableName : "go_session" ,
65
64
stdout : os .Stderr ,
66
65
}
@@ -75,18 +74,7 @@ func NewStoreWithDB(db *sql.DB, tableName string, gcInterval int) session.Manage
75
74
}
76
75
store .ticker = time .NewTicker (time .Second * time .Duration (interval ))
77
76
78
- store .pool = sync.Pool {
79
- New : func () interface {} {
80
- return newStore (store .db , store .tableName )
81
- },
82
- }
83
-
84
- store .db .AddTableWithName (SessionItem {}, store .tableName )
85
-
86
- err := store .db .CreateTablesIfNotExists ()
87
- if err != nil {
88
- panic (err )
89
- }
77
+ store .db .Exec (fmt .Sprintf ("CREATE TABLE IF NOT EXISTS `%s` (`id` VARCHAR(255) NOT NULL PRIMARY KEY, `value` VARCHAR(2048), `expired_at` bigint) engine=InnoDB charset=UTF8;" , store .tableName ))
90
78
store .db .Exec (fmt .Sprintf ("CREATE INDEX `idx_expired_at` ON %s (`expired_at`);" , store .tableName ))
91
79
92
80
go store .gc ()
@@ -95,8 +83,7 @@ func NewStoreWithDB(db *sql.DB, tableName string, gcInterval int) session.Manage
95
83
96
84
type managerStore struct {
97
85
ticker * time.Ticker
98
- pool sync.Pool
99
- db * gorp.DbMap
86
+ db * sql.DB
100
87
tableName string
101
88
stdout io.Writer
102
89
}
@@ -111,13 +98,15 @@ func (s *managerStore) errorf(format string, args ...interface{}) {
111
98
func (s * managerStore ) gc () {
112
99
for range s .ticker .C {
113
100
now := time .Now ().Unix ()
114
- query := fmt .Sprintf ("SELECT COUNT(*) FROM %s WHERE expired_at<=?" , s .tableName )
115
- n , err := s .db .SelectInt (query , now )
101
+
102
+ var count int
103
+ row := s .db .QueryRow (fmt .Sprintf ("SELECT COUNT(*) FROM `%s` WHERE `expired_at`<=?" , s .tableName ), now )
104
+ err := row .Scan (& count )
116
105
if err != nil {
117
106
s .errorf ("[ERROR]:%s" , err .Error ())
118
107
return
119
- } else if n > 0 {
120
- _ , err = s .db .Exec (fmt .Sprintf ("DELETE FROM %s WHERE expired_at<=?" , s .tableName ), now )
108
+ } else if count > 0 {
109
+ _ , err = s .db .Exec (fmt .Sprintf ("DELETE FROM `%s` WHERE ` expired_at` <=?" , s .tableName ), now )
121
110
if err != nil {
122
111
s .errorf ("[ERROR]:%s" , err .Error ())
123
112
}
@@ -128,7 +117,8 @@ func (s *managerStore) gc() {
128
117
func (s * managerStore ) getValue (sid string ) (string , error ) {
129
118
var item SessionItem
130
119
131
- err := s .db .SelectOne (& item , fmt .Sprintf ("SELECT * FROM %s WHERE id=?" , s .tableName ), sid )
120
+ row := s .db .QueryRow (fmt .Sprintf ("SELECT `id`,`value`,`expired_at` FROM `%s` WHERE `id`=?" , s .tableName ), sid )
121
+ err := row .Scan (& item .ID , & item .Value , & item .ExpiredAt )
132
122
if err != nil {
133
123
if err == sql .ErrNoRows {
134
124
return "" , nil
@@ -162,23 +152,18 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
162
152
}
163
153
164
154
func (s * managerStore ) Create (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
165
- store := s .pool .Get ().(* store )
166
- store .reset (ctx , sid , expired , nil )
167
- return store , nil
155
+ return newStore (ctx , s , sid , expired , nil ), nil
168
156
}
169
157
170
158
func (s * managerStore ) Update (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
171
- store := s .pool .Get ().(* store )
172
-
173
159
value , err := s .getValue (sid )
174
160
if err != nil {
175
161
return nil , err
176
162
} else if value == "" {
177
- store .reset (ctx , sid , expired , nil )
178
- return store , nil
163
+ return newStore (ctx , s , sid , expired , nil ), nil
179
164
}
180
165
181
- _ , err = s .db .Exec (fmt .Sprintf ("UPDATE %s SET expired_at=? WHERE id =?" , s .tableName ),
166
+ _ , err = s .db .Exec (fmt .Sprintf ("UPDATE `%s` SET ` expired_at` =? WHERE `id` =?" , s .tableName ),
182
167
time .Now ().Add (time .Duration (expired )* time .Second ).Unix (),
183
168
sid )
184
169
if err != nil {
@@ -190,31 +175,24 @@ func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (s
190
175
return nil , err
191
176
}
192
177
193
- store .reset (ctx , sid , expired , values )
194
- return store , nil
178
+ return newStore (ctx , s , sid , expired , values ), nil
195
179
}
196
180
197
181
func (s * managerStore ) Delete (_ context.Context , sid string ) error {
198
- _ , err := s .db .Exec (fmt .Sprintf ("DELETE FROM %s WHERE id =?" , s .tableName ), sid )
182
+ _ , err := s .db .Exec (fmt .Sprintf ("DELETE FROM `%s` WHERE `id` =?" , s .tableName ), sid )
199
183
return err
200
184
}
201
185
202
186
func (s * managerStore ) Refresh (ctx context.Context , oldsid , sid string , expired int64 ) (session.Store , error ) {
203
- store := s .pool .Get ().(* store )
204
-
205
187
value , err := s .getValue (oldsid )
206
188
if err != nil {
207
189
return nil , err
208
190
} else if value == "" {
209
- store .reset (ctx , sid , expired , nil )
210
- return store , nil
191
+ return newStore (ctx , s , sid , expired , nil ), nil
211
192
}
212
193
213
- err = s .db .Insert (& SessionItem {
214
- ID : sid ,
215
- Value : value ,
216
- ExpiredAt : time .Now ().Add (time .Duration (expired ) * time .Second ).Unix (),
217
- })
194
+ query := fmt .Sprintf ("INSERT INTO `%s` (`id`,`value`,`expired_at`) VALUES (?,?,?);" , s .tableName )
195
+ _ , err = s .db .Exec (query , sid , value , time .Now ().Add (time .Duration (expired )* time .Second ).Unix ())
218
196
if err != nil {
219
197
return nil , err
220
198
}
@@ -229,43 +207,40 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
229
207
return nil , err
230
208
}
231
209
232
- store .reset (ctx , sid , expired , values )
233
- return store , nil
210
+ return newStore (ctx , s , sid , expired , values ), nil
234
211
}
235
212
236
213
func (s * managerStore ) Close () error {
237
214
s .ticker .Stop ()
238
- s .db .Db . Close ()
215
+ s .db .Close ()
239
216
return nil
240
217
}
241
218
242
- func newStore (db * gorp.DbMap , tableName string ) * store {
219
+ func newStore (ctx context.Context , s * managerStore , sid string , expired int64 , values map [string ]interface {}) * store {
220
+ if values == nil {
221
+ values = make (map [string ]interface {})
222
+ }
223
+
243
224
return & store {
244
- db : db ,
245
- tableName : tableName ,
225
+ db : s .db ,
226
+ tableName : s .tableName ,
227
+ ctx : ctx ,
228
+ sid : sid ,
229
+ expired : expired ,
230
+ values : values ,
246
231
}
247
232
}
248
233
249
234
type store struct {
250
235
sync.RWMutex
251
236
ctx context.Context
252
- db * gorp. DbMap
237
+ db * sql. DB
253
238
tableName string
254
239
sid string
255
240
expired int64
256
241
values map [string ]interface {}
257
242
}
258
243
259
- func (s * store ) reset (ctx context.Context , sid string , expired int64 , values map [string ]interface {}) {
260
- if values == nil {
261
- values = make (map [string ]interface {})
262
- }
263
- s .ctx = ctx
264
- s .sid = sid
265
- s .expired = expired
266
- s .values = values
267
- }
268
-
269
244
func (s * store ) Context () context.Context {
270
245
return s .ctx
271
246
}
@@ -320,18 +295,18 @@ func (s *store) Save() error {
320
295
}
321
296
s .RUnlock ()
322
297
323
- n , err := s .db .SelectInt (fmt .Sprintf ("SELECT COUNT(*) FROM %s WHERE id=?" , s .tableName ), s .sid )
298
+ var count int
299
+ row := s .db .QueryRow (fmt .Sprintf ("SELECT COUNT(*) FROM %s WHERE id=?" , s .tableName ), s .sid )
300
+ err := row .Scan (& count )
324
301
if err != nil {
325
302
return err
326
- } else if n == 0 {
327
- return s .db .Insert (& SessionItem {
328
- ID : s .sid ,
329
- Value : value ,
330
- ExpiredAt : time .Now ().Add (time .Duration (s .expired ) * time .Second ).Unix (),
331
- })
303
+ } else if count == 0 {
304
+ query := fmt .Sprintf ("INSERT INTO `%s` (`id`,`value`,`expired_at`) VALUES (?,?,?);" , s .tableName )
305
+ _ , err = s .db .Exec (query , s .sid , value , time .Now ().Add (time .Duration (s .expired )* time .Second ).Unix ())
306
+ return err
332
307
}
333
308
334
- _ , err = s .db .Exec (fmt .Sprintf ("UPDATE %s SET value=?,expired_at=? WHERE id =?" , s .tableName ),
309
+ _ , err = s .db .Exec (fmt .Sprintf ("UPDATE `%s` SET ` value` =?,` expired_at` =? WHERE `id` =?" , s .tableName ),
335
310
value ,
336
311
time .Now ().Add (time .Duration (s .expired )* time .Second ).Unix (),
337
312
s .sid )
@@ -341,7 +316,7 @@ func (s *store) Save() error {
341
316
342
317
// SessionItem Data items stored in mysql
343
318
type SessionItem struct {
344
- ID string `db:"id,primarykey,size:255"`
345
- Value string `db:"value,size:2048"`
346
- ExpiredAt int64 `db:"expired_at"`
319
+ ID string
320
+ Value string
321
+ ExpiredAt int64
347
322
}
0 commit comments