mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 08:37:10 +08:00
Fix GC deleting values without expiry (#54)
* Test MYSQL Fix * Test * Add postgres * Fix mysql * Fix sqlite
This commit is contained in:
@@ -100,11 +100,11 @@ func New(config ...Config) *Storage {
|
||||
sqlInsert: fmt.Sprintf("INSERT INTO %s (k, v, e) VALUES ($1, $2, $3) ON CONFLICT (k) DO UPDATE SET v = $4, e = $5", cfg.Table),
|
||||
sqlDelete: fmt.Sprintf("DELETE FROM %s WHERE k=$1", cfg.Table),
|
||||
sqlReset: fmt.Sprintf("DELETE FROM %s;", cfg.Table),
|
||||
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= $1", cfg.Table),
|
||||
sqlGC: fmt.Sprintf("DELETE FROM %s WHERE e <= $1 AND e != 0", cfg.Table),
|
||||
}
|
||||
|
||||
// Start garbage collector
|
||||
go store.gc()
|
||||
go store.gcTicker()
|
||||
|
||||
return store
|
||||
}
|
||||
@@ -175,8 +175,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 {
|
||||
@@ -184,7 +184,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())
|
||||
}
|
||||
|
Reference in New Issue
Block a user