-
Notifications
You must be signed in to change notification settings - Fork 12
/
provider.go
163 lines (140 loc) · 3.95 KB
/
provider.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
package postgres
import (
"errors"
"github.com/phachon/fasthttpsession"
"reflect"
"time"
)
// session postgres provider
// session Table structure
//
// DROP TABLE IF EXISTS `session`;
// CREATE TABLE `session` (
// `session_id` varchar(64) NOT NULL DEFAULT '',
// `contents` TEXT NOT NULL,
// `last_active` int(10) NOT NULL DEFAULT '0',
// PRIMARY KEY (`session_id`),
// )
// create index last_active on session (last_active);
//
const ProviderName = "postgres"
var (
provider = NewProvider()
encrypt = fasthttpsession.NewEncrypt()
)
type Provider struct {
config *Config
values *fasthttpsession.CCMap
sessionDao *sessionDao
maxLifeTime int64
}
// new postgres provider
func NewProvider() *Provider {
return &Provider{
config: &Config{},
values: fasthttpsession.NewDefaultCCMap(),
sessionDao: &sessionDao{},
}
}
// init provider config
func (pp *Provider) Init(lifeTime int64, postgresConfig fasthttpsession.ProviderConfig) error {
if postgresConfig.Name() != ProviderName {
return errors.New("session postgres provider init error, config must postgres config")
}
vc := reflect.ValueOf(postgresConfig)
rc := vc.Interface().(*Config)
pp.config = rc
pp.maxLifeTime = lifeTime
// check config
if pp.config.Host == "" {
return errors.New("session postgres provider init error, config Host not empty")
}
if pp.config.Port == 0 {
return errors.New("session postgres provider init error, config Port not empty")
}
// init config serialize func
if pp.config.SerializeFunc == nil {
pp.config.SerializeFunc = encrypt.Base64Encode
}
if pp.config.UnSerializeFunc == nil {
pp.config.UnSerializeFunc = encrypt.Base64Decode
}
// init sessionDao
sessionDao, err := newSessionDao(pp.config.Database, pp.config.TableName)
if err != nil {
return err
}
sessionDao.postgresConn.SetMaxOpenConns(pp.config.SetMaxIdleConn)
sessionDao.postgresConn.SetMaxIdleConns(pp.config.SetMaxIdleConn)
pp.sessionDao = sessionDao
return sessionDao.postgresConn.Ping()
}
// not need gc
func (pp *Provider) NeedGC() bool {
return true
}
// session postgres provider not need garbage collection
func (pp *Provider) GC() {
pp.sessionDao.deleteSessionByMaxLifeTime(pp.maxLifeTime)
}
// read session store by session id
func (pp *Provider) ReadStore(sessionId string) (fasthttpsession.SessionStore, error) {
sessionValue, err := pp.sessionDao.getSessionBySessionId(sessionId)
if err != nil {
return nil, err
}
if len(sessionValue) == 0 {
_, err := pp.sessionDao.insert(sessionId, "", time.Now().Unix())
if err != nil {
return nil, err
}
return NewPostgresStore(sessionId), nil
}
if len(sessionValue["contents"]) == 0 {
return NewPostgresStore(sessionId), nil
}
data, err := pp.config.UnSerializeFunc(sessionValue["contents"])
if err != nil {
return nil, err
}
return NewPostgresStoreData(sessionId, data), nil
}
// regenerate session
func (pp *Provider) Regenerate(oldSessionId string, sessionId string) (fasthttpsession.SessionStore, error) {
sessionValue, err := pp.sessionDao.getSessionBySessionId(oldSessionId)
if err != nil {
return nil, err
}
if len(sessionValue) == 0 {
// old sessionId not exists, insert new sessionId
_, err := pp.sessionDao.insert(sessionId, "", time.Now().Unix())
if err != nil {
return nil, err
}
return NewPostgresStore(sessionId), nil
}
// delete old session
_, err = pp.sessionDao.deleteBySessionId(oldSessionId)
if err != nil {
return nil, err
}
// insert new session
_, err = pp.sessionDao.insert(sessionId, string(sessionValue["contents"]), time.Now().Unix())
if err != nil {
return nil, err
}
return pp.ReadStore(sessionId)
}
// destroy session by sessionId
func (pp *Provider) Destroy(sessionId string) error {
_, err := pp.sessionDao.deleteBySessionId(sessionId)
return err
}
// session values count
func (pp *Provider) Count() int {
return pp.sessionDao.countSessions()
}
// register session provider
func init() {
fasthttpsession.Register(ProviderName, provider)
}