2023-07-07 06:07:10 +00:00
|
|
|
package timecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var backgroundSweepInterval = time.Minute
|
|
|
|
|
|
|
|
func background(ctx context.Context, lk sync.Locker, m map[string]time.Time) {
|
|
|
|
ticker := time.NewTicker(backgroundSweepInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case now := <-ticker.C:
|
|
|
|
sweep(lk, m, now)
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
2024-08-12 03:56:30 +00:00
|
|
|
ticker.Stop()
|
2023-07-07 06:07:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sweep(lk sync.Locker, m map[string]time.Time, now time.Time) {
|
|
|
|
lk.Lock()
|
|
|
|
|
|
|
|
for k, expiry := range m {
|
|
|
|
if expiry.Before(now) {
|
|
|
|
delete(m, k)
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 03:56:30 +00:00
|
|
|
|
|
|
|
lk.Unlock()
|
2023-07-07 06:07:10 +00:00
|
|
|
}
|