Fix GC deleting values without expiry (#54)

* Test MYSQL Fix

* Test

* Add postgres

* Fix mysql

* Fix sqlite
This commit is contained in:
hi019
2021-03-18 17:09:06 -04:00
committed by GitHub
parent 9829073dd7
commit a66230ef17
6 changed files with 108 additions and 15 deletions

View File

@@ -81,11 +81,11 @@ func New(config ...Config) *Storage {
sqlInsert: fmt.Sprintf("INSERT OR REPLACE INTO %s (k, v, e) VALUES (?,?,?)", cfg.Table),
sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE k=?", cfg.Table),
sqlReset: fmt.Sprintf("DELETE FROM %s;", cfg.Table),
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= ?", cfg.Table),
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= ? AND e != 0", cfg.Table),
}
// Start garbage collector
go store.gc()
go store.gcTicker()
return store
}
@@ -152,8 +152,8 @@ func (s *Storage) Close() error {
return s.db.Close()
}
// GC deletes all expired entries
func (s *Storage) gc() {
// gcTicker starts the gc ticker
func (s *Storage) gcTicker() {
ticker := time.NewTicker(s.gcInterval)
defer ticker.Stop()
for {
@@ -161,7 +161,12 @@ func (s *Storage) gc() {
case <-s.done:
return
case t := <-ticker.C:
_, _ = s.db.Exec(s.sqlGC, t.Unix())
s.gc(t)
}
}
}
// gc deletes all expired entries
func (s *Storage) gc(t time.Time) {
_, _ = s.db.Exec(s.sqlGC, t.Unix())
}