feat: improve defaultAdapter to add clean timer

This commit is contained in:
weloe
2023-05-09 15:28:27 +08:00
parent 4a14a250dc
commit f45277b4d5
6 changed files with 62 additions and 10 deletions

View File

@@ -12,6 +12,7 @@ import (
type DefaultAdapter struct {
dataMap *sync.Map
expireMap *sync.Map
once sync.Once
}
var _ Adapter = (*DefaultAdapter)(nil)
@@ -168,3 +169,27 @@ func (d *DefaultAdapter) getTimeout(key string) int64 {
}
return timeout
}
func (d *DefaultAdapter) StartCleanTimer(period int64) {
d.once.Do(func() {
go d.CleanTask(period)
})
}
func (d *DefaultAdapter) CleanTask(period int64) {
if period < 0 {
return
}
duration := period
// create timer
ticker := time.NewTicker(time.Duration(duration) * time.Second)
defer ticker.Stop()
for range ticker.C {
d.expireMap.Range(func(key, value any) bool {
_ = d.getExpireAndDelete(key.(string))
return true
})
}
}