1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00
MiraiGo/utils/ttl.go
2020-08-03 00:50:28 +08:00

61 lines
942 B
Go

package utils
import (
"sync"
"time"
)
type TTList struct {
list []*item
lock *sync.Mutex
}
type item struct {
i interface{}
lastAccess int64
}
func NewTTList(ttl int64) *TTList {
l := &TTList{
lock: new(sync.Mutex),
}
go func() {
for now := range time.Tick(time.Second * 5) {
l.lock.Lock()
pos := 0
for _, i := range l.list {
if now.Unix()-i.lastAccess > ttl {
l.list = append(l.list[:pos], l.list[pos+1:]...)
if pos > 0 {
pos++
}
}
pos++
}
l.lock.Unlock()
}
}()
return l
}
func (l *TTList) Add(i interface{}) {
l.lock.Lock()
defer l.lock.Unlock()
l.list = append(l.list, &item{
i: i,
lastAccess: time.Now().Unix(),
})
}
func (l *TTList) Any(filter func(i interface{}) bool) bool {
l.lock.Lock()
defer l.lock.Unlock()
for _, it := range l.list {
if filter(it.i) {
it.lastAccess = time.Now().Unix()
return true
}
}
return false
}