@@ -41,6 +41,11 @@ type Config struct {
4141 MaxIdleConns int
4242}
4343
44+ // NewDefaultStore create mysql store instance
45+ func NewDefaultStore (config * Config ) * Store {
46+ return NewStore (config , "" , 0 )
47+ }
48+
4449// NewStore create mysql store instance,
4550// config mysql configuration,
4651// tableName table name (default oauth2_token),
@@ -114,27 +119,33 @@ func (s *Store) Close() {
114119 s .db .Db .Close ()
115120}
116121
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 ()
121125 }
122126}
123127
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 {
129133 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 ())
137135 }
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 ))
138149 }
139150}
140151
0 commit comments