mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-04-09 15:15:17 +08:00
优化ttlcache
This commit is contained in:
@@ -5,21 +5,24 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ItemMap = map[int64]zero.Zero
|
||||
type ItemMap = map[uint64]zero.Zero
|
||||
|
||||
type List struct {
|
||||
expireMap map[int64]ItemMap // expires timestamp => map[id]ItemMap
|
||||
itemsMap map[int64]int64 // itemId => timestamp
|
||||
itemsMap map[uint64]int64 // itemId => timestamp
|
||||
|
||||
locker sync.Mutex
|
||||
|
||||
gcCallback func(itemId int64)
|
||||
gcCallback func(itemId uint64)
|
||||
gcBatchCallback func(itemIds ItemMap)
|
||||
|
||||
lastTimestamp int64
|
||||
}
|
||||
|
||||
func NewList() *List {
|
||||
var list = &List{
|
||||
expireMap: map[int64]ItemMap{},
|
||||
itemsMap: map[int64]int64{},
|
||||
itemsMap: map[uint64]int64{},
|
||||
}
|
||||
|
||||
SharedManager.Add(list)
|
||||
@@ -27,12 +30,25 @@ func NewList() *List {
|
||||
return list
|
||||
}
|
||||
|
||||
func NewSingletonList() *List {
|
||||
var list = &List{
|
||||
expireMap: map[int64]ItemMap{},
|
||||
itemsMap: map[uint64]int64{},
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
// Add 添加条目
|
||||
// 如果条目已经存在,则覆盖
|
||||
func (this *List) Add(itemId int64, expiresAt int64) {
|
||||
func (this *List) Add(itemId uint64, expiresAt int64) {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
|
||||
if this.lastTimestamp == 0 || this.lastTimestamp > expiresAt {
|
||||
this.lastTimestamp = expiresAt
|
||||
}
|
||||
|
||||
// 是否已经存在
|
||||
oldExpiresAt, ok := this.itemsMap[itemId]
|
||||
if ok {
|
||||
@@ -55,34 +71,61 @@ func (this *List) Add(itemId int64, expiresAt int64) {
|
||||
this.itemsMap[itemId] = expiresAt
|
||||
}
|
||||
|
||||
func (this *List) Remove(itemId int64) {
|
||||
func (this *List) Remove(itemId uint64) {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
this.removeItem(itemId)
|
||||
}
|
||||
|
||||
func (this *List) GC(timestamp int64, callback func(itemId int64)) {
|
||||
func (this *List) GC(timestamp int64) ItemMap {
|
||||
if this.lastTimestamp > timestamp+1 {
|
||||
return nil
|
||||
}
|
||||
this.locker.Lock()
|
||||
var itemMap = this.gcItems(timestamp)
|
||||
if len(itemMap) == 0 {
|
||||
this.locker.Unlock()
|
||||
return
|
||||
return itemMap
|
||||
}
|
||||
this.locker.Unlock()
|
||||
|
||||
if callback != nil {
|
||||
if this.gcCallback != nil {
|
||||
for itemId := range itemMap {
|
||||
callback(itemId)
|
||||
this.gcCallback(itemId)
|
||||
}
|
||||
}
|
||||
if this.gcBatchCallback != nil {
|
||||
this.gcBatchCallback(itemMap)
|
||||
}
|
||||
|
||||
return itemMap
|
||||
}
|
||||
|
||||
func (this *List) OnGC(callback func(itemId int64)) *List {
|
||||
func (this *List) Clean() {
|
||||
this.locker.Lock()
|
||||
this.itemsMap = map[uint64]int64{}
|
||||
this.expireMap = map[int64]ItemMap{}
|
||||
this.locker.Unlock()
|
||||
}
|
||||
|
||||
func (this *List) Count() int {
|
||||
this.locker.Lock()
|
||||
var count = len(this.itemsMap)
|
||||
this.locker.Unlock()
|
||||
return count
|
||||
}
|
||||
|
||||
func (this *List) OnGC(callback func(itemId uint64)) *List {
|
||||
this.gcCallback = callback
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *List) removeItem(itemId int64) {
|
||||
func (this *List) OnGCBatch(callback func(itemMap ItemMap)) *List {
|
||||
this.gcBatchCallback = callback
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *List) removeItem(itemId uint64) {
|
||||
expiresAt, ok := this.itemsMap[itemId]
|
||||
if !ok {
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package expires
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
@@ -50,13 +51,34 @@ func TestList_Remove(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestList_GC(t *testing.T) {
|
||||
var unixTime = time.Now().Unix()
|
||||
t.Log("unixTime:", unixTime)
|
||||
|
||||
var list = NewList()
|
||||
list.Add(1, unixTime+1)
|
||||
list.Add(2, unixTime+1)
|
||||
list.Add(3, unixTime+2)
|
||||
list.OnGC(func(itemId uint64) {
|
||||
t.Log("gc:", itemId)
|
||||
})
|
||||
t.Log("last unixTime:", list.lastTimestamp)
|
||||
list.GC(time.Now().Unix() + 2)
|
||||
logs.PrintAsJSON(list.expireMap, t)
|
||||
logs.PrintAsJSON(list.itemsMap, t)
|
||||
|
||||
t.Log(list.Count())
|
||||
}
|
||||
|
||||
func TestList_GC_Batch(t *testing.T) {
|
||||
list := NewList()
|
||||
list.Add(1, time.Now().Unix()+1)
|
||||
list.Add(2, time.Now().Unix()+1)
|
||||
list.Add(3, time.Now().Unix()+2)
|
||||
list.GC(time.Now().Unix()+2, func(itemId int64) {
|
||||
t.Log("gc:", itemId)
|
||||
list.Add(4, time.Now().Unix()+2)
|
||||
list.OnGCBatch(func(itemMap ItemMap) {
|
||||
t.Log("gc:", itemMap)
|
||||
})
|
||||
list.GC(time.Now().Unix() + 2)
|
||||
logs.PrintAsJSON(list.expireMap, t)
|
||||
logs.PrintAsJSON(list.itemsMap, t)
|
||||
}
|
||||
@@ -72,7 +94,7 @@ func TestList_Start_GC(t *testing.T) {
|
||||
list.Add(7, time.Now().Unix()+6)
|
||||
list.Add(8, time.Now().Unix()+6)
|
||||
|
||||
list.OnGC(func(itemId int64) {
|
||||
list.OnGC(func(itemId uint64) {
|
||||
t.Log("gc:", itemId, timeutil.Format("H:i:s"))
|
||||
time.Sleep(2 * time.Second)
|
||||
})
|
||||
@@ -87,17 +109,18 @@ func TestList_Start_GC(t *testing.T) {
|
||||
func TestList_ManyItems(t *testing.T) {
|
||||
list := NewList()
|
||||
for i := 0; i < 1_000; i++ {
|
||||
list.Add(int64(i), time.Now().Unix())
|
||||
list.Add(uint64(i), time.Now().Unix())
|
||||
}
|
||||
for i := 0; i < 1_000; i++ {
|
||||
list.Add(int64(i), time.Now().Unix()+1)
|
||||
list.Add(uint64(i), time.Now().Unix()+1)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
count := 0
|
||||
list.GC(time.Now().Unix()+1, func(itemId int64) {
|
||||
list.OnGC(func(itemId uint64) {
|
||||
count++
|
||||
})
|
||||
list.GC(time.Now().Unix() + 1)
|
||||
t.Log("gc", count, "items")
|
||||
t.Log(time.Now().Sub(now))
|
||||
}
|
||||
@@ -171,15 +194,23 @@ func BenchmarkList_GC(b *testing.B) {
|
||||
|
||||
var lists = []*List{}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
lists = append(lists, NewList())
|
||||
for m := 0; m < 1_000; m++ {
|
||||
var list = NewList()
|
||||
for j := 0; j < 10_000; j++ {
|
||||
list.Add(uint64(j), utils.UnixTime()+100)
|
||||
}
|
||||
lists = append(lists, list)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
var timestamp = time.Now().Unix()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, list := range lists {
|
||||
list.GC(timestamp, nil)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
for _, list := range lists {
|
||||
list.GC(timestamp)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user