@@ -41,6 +41,11 @@ type Config struct {
41
41
MaxIdleConns int
42
42
}
43
43
44
+ // NewDefaultStore create mysql store instance
45
+ func NewDefaultStore (config * Config ) * Store {
46
+ return NewStore (config , "" , 0 )
47
+ }
48
+
44
49
// NewStore create mysql store instance,
45
50
// config mysql configuration,
46
51
// tableName table name (default oauth2_token),
@@ -114,27 +119,33 @@ func (s *Store) Close() {
114
119
s .db .Db .Close ()
115
120
}
116
121
117
- func (s * Store ) errorf (format string , args ... interface {}) {
118
- if s .stdout != nil {
119
- buf := fmt .Sprintf (format , args ... )
120
- s .stdout .Write ([]byte (buf ))
122
+ func (s * Store ) gc () {
123
+ for range s .ticker .C {
124
+ s .clean ()
121
125
}
122
126
}
123
127
124
- func (s * Store ) gc () {
125
- for range s . ticker . C {
126
- now := time . Now (). Unix ( )
127
- query := fmt . Sprintf ( "SELECT COUNT(*) FROM %s WHERE expired_at<=?" , s . tableName )
128
- n , err := s . db . SelectInt ( query , now )
128
+ func (s * Store ) clean () {
129
+ now := time . Now (). Unix ()
130
+ query := fmt . Sprintf ( "SELECT COUNT(*) FROM %s WHERE expired_at<=? OR (code='' AND access='' AND refresh='')" , s . tableName )
131
+ n , err := s . db . SelectInt ( query , now )
132
+ if err != nil || n == 0 {
129
133
if err != nil {
130
- s .errorf ("[ERROR]:%s" , err .Error ())
131
- return
132
- } else if n > 0 {
133
- _ , err = s .db .Exec (fmt .Sprintf ("DELETE FROM %s WHERE expired_at<=?" , s .tableName ), now )
134
- if err != nil {
135
- s .errorf ("[ERROR]:%s" , err .Error ())
136
- }
134
+ s .errorf (err .Error ())
137
135
}
136
+ return
137
+ }
138
+
139
+ _ , err = s .db .Exec (fmt .Sprintf ("DELETE FROM %s WHERE expired_at<=? OR (code='' AND access='' AND refresh='')" , s .tableName ), now )
140
+ if err != nil {
141
+ s .errorf (err .Error ())
142
+ }
143
+ }
144
+
145
+ func (s * Store ) errorf (format string , args ... interface {}) {
146
+ if s .stdout != nil {
147
+ buf := fmt .Sprintf ("[OAUTH2-MYSQL-ERROR]: " + format , args ... )
148
+ s .stdout .Write ([]byte (buf ))
138
149
}
139
150
}
140
151
0 commit comments