优化ttlcache

This commit is contained in:
刘祥超
2022-04-09 18:28:22 +08:00
parent 02469f2886
commit ded2f98cce
10 changed files with 259 additions and 90 deletions

View File

@@ -31,31 +31,32 @@ func NewManager() *Manager {
func (this *Manager) init() {
var lastTimestamp = int64(0)
for range this.ticker.C {
timestamp := time.Now().Unix()
var currentTime = time.Now().Unix()
if lastTimestamp == 0 {
lastTimestamp = timestamp - 3600
lastTimestamp = currentTime - 3600
}
if timestamp >= lastTimestamp {
for i := lastTimestamp; i <= timestamp; i++ {
if currentTime >= lastTimestamp {
for i := lastTimestamp; i <= currentTime; i++ {
this.locker.Lock()
for list := range this.listMap {
list.GC(i, list.gcCallback)
list.GC(i)
}
this.locker.Unlock()
}
} else {
for i := timestamp; i <= lastTimestamp; i++ {
// 如果过去的时间比现在大,则从这一秒重新开始
for i := currentTime; i <= currentTime; i++ {
this.locker.Lock()
for list := range this.listMap {
list.GC(i, list.gcCallback)
list.GC(i)
}
this.locker.Unlock()
}
}
// 这样做是为了防止系统时钟突变
lastTimestamp = timestamp
lastTimestamp = currentTime
}
}