Files
EdgeNode/internal/utils/expires/list.go

111 lines
2.1 KiB
Go
Raw Normal View History

2021-01-03 20:18:47 +08:00
package expires
import (
"github.com/TeaOSLab/EdgeNode/internal/zero"
2021-01-03 20:18:47 +08:00
"sync"
)
type ItemMap = map[int64]zero.Zero
2021-01-03 20:18:47 +08:00
type List struct {
expireMap map[int64]ItemMap // expires timestamp => map[id]ItemMap
2021-01-03 20:18:47 +08:00
itemsMap map[int64]int64 // itemId => timestamp
locker sync.Mutex
gcCallback func(itemId int64)
2021-01-03 20:18:47 +08:00
}
func NewList() *List {
var list = &List{
2021-01-03 20:18:47 +08:00
expireMap: map[int64]ItemMap{},
itemsMap: map[int64]int64{},
}
SharedManager.Add(list)
return list
2021-01-03 20:18:47 +08:00
}
2021-12-12 11:48:01 +08:00
// Add 添加条目
// 如果条目已经存在,则覆盖
2021-07-18 15:51:49 +08:00
func (this *List) Add(itemId int64, expiresAt int64) {
2021-01-03 20:18:47 +08:00
this.locker.Lock()
defer this.locker.Unlock()
// 是否已经存在
2021-12-12 11:48:01 +08:00
oldExpiresAt, ok := this.itemsMap[itemId]
2021-01-03 20:18:47 +08:00
if ok {
2021-12-12 11:48:01 +08:00
if oldExpiresAt == expiresAt {
return
}
delete(this.expireMap, oldExpiresAt)
2021-01-03 20:18:47 +08:00
}
2021-07-18 15:51:49 +08:00
expireItemMap, ok := this.expireMap[expiresAt]
2021-01-03 20:18:47 +08:00
if ok {
expireItemMap[itemId] = zero.New()
2021-01-03 20:18:47 +08:00
} else {
expireItemMap = ItemMap{
itemId: zero.New(),
2021-01-03 20:18:47 +08:00
}
2021-07-18 15:51:49 +08:00
this.expireMap[expiresAt] = expireItemMap
2021-01-03 20:18:47 +08:00
}
2021-07-18 15:51:49 +08:00
this.itemsMap[itemId] = expiresAt
2021-01-03 20:18:47 +08:00
}
func (this *List) Remove(itemId int64) {
this.locker.Lock()
defer this.locker.Unlock()
this.removeItem(itemId)
}
func (this *List) GC(timestamp int64, callback func(itemId int64)) {
this.locker.Lock()
2022-01-10 15:38:53 +08:00
var itemMap = this.gcItems(timestamp)
if len(itemMap) == 0 {
this.locker.Unlock()
return
}
2021-01-03 20:18:47 +08:00
this.locker.Unlock()
if callback != nil {
for itemId := range itemMap {
callback(itemId)
}
2021-01-03 20:18:47 +08:00
}
}
2021-12-12 11:48:01 +08:00
func (this *List) OnGC(callback func(itemId int64)) *List {
this.gcCallback = callback
2021-12-12 11:48:01 +08:00
return this
2021-01-03 20:18:47 +08:00
}
func (this *List) removeItem(itemId int64) {
expiresAt, ok := this.itemsMap[itemId]
if !ok {
return
}
delete(this.itemsMap, itemId)
expireItemMap, ok := this.expireMap[expiresAt]
if ok {
delete(expireItemMap, itemId)
if len(expireItemMap) == 0 {
delete(this.expireMap, expiresAt)
}
}
}
func (this *List) gcItems(timestamp int64) ItemMap {
expireItemsMap, ok := this.expireMap[timestamp]
if ok {
for itemId := range expireItemsMap {
delete(this.itemsMap, itemId)
}
delete(this.expireMap, timestamp)
}
return expireItemsMap
}