mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-07 02:20:25 +08:00
优化代码
This commit is contained in:
@@ -24,11 +24,19 @@ func NewPiece[T any](maxItems int) *Piece[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *Piece[T]) Add(key uint64, item *Item[T]) (ok bool) {
|
func (this *Piece[T]) Add(key uint64, item *Item[T]) (ok bool) {
|
||||||
this.locker.Lock()
|
this.locker.RLock()
|
||||||
if this.maxItems > 0 && len(this.m) >= this.maxItems {
|
if this.maxItems > 0 && len(this.m) >= this.maxItems {
|
||||||
this.locker.Unlock()
|
this.locker.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
this.locker.RUnlock()
|
||||||
|
|
||||||
|
this.locker.Lock()
|
||||||
|
oldItem, exists := this.m[key]
|
||||||
|
if exists && oldItem.expiredAt == item.expiredAt {
|
||||||
|
this.locker.Unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
this.m[key] = item
|
this.m[key] = item
|
||||||
this.locker.Unlock()
|
this.locker.Unlock()
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,16 @@ func TestPiece_Add(t *testing.T) {
|
|||||||
t.Log(piece.Read(1))
|
t.Log(piece.Read(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPiece_Add_Same(t *testing.T) {
|
||||||
|
piece := NewPiece[int](10)
|
||||||
|
piece.Add(1, &Item[int]{expiredAt: time.Now().Unix() + 3600})
|
||||||
|
piece.Add(1, &Item[int]{expiredAt: time.Now().Unix() + 3600})
|
||||||
|
for key, item := range piece.m {
|
||||||
|
t.Log(key, item.Value)
|
||||||
|
}
|
||||||
|
t.Log(piece.Read(1))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPiece_MaxItems(t *testing.T) {
|
func TestPiece_MaxItems(t *testing.T) {
|
||||||
piece := NewPiece[int](10)
|
piece := NewPiece[int](10)
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package expires
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||||
"github.com/iwind/TeaGo/assert"
|
"github.com/iwind/TeaGo/assert"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
@@ -84,6 +85,10 @@ func TestList_GC_Batch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestList_Start_GC(t *testing.T) {
|
func TestList_Start_GC(t *testing.T) {
|
||||||
|
if !testutils.IsSingleTesting() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
list := NewList()
|
list := NewList()
|
||||||
list.Add(1, time.Now().Unix()+1)
|
list.Add(1, time.Now().Unix()+1)
|
||||||
list.Add(2, time.Now().Unix()+1)
|
list.Add(2, time.Now().Unix()+1)
|
||||||
@@ -125,6 +130,25 @@ func TestList_ManyItems(t *testing.T) {
|
|||||||
t.Log(time.Since(now))
|
t.Log(time.Since(now))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestList_Memory(t *testing.T) {
|
||||||
|
if !testutils.IsSingleTesting() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var list = NewList()
|
||||||
|
|
||||||
|
testutils.StartMemoryStats(t, func() {
|
||||||
|
t.Log(list.Count(), "items")
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
for i := 0; i < 10_000_000; i++ {
|
||||||
|
list.Add(uint64(i), time.Now().Unix()+1800)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Hour)
|
||||||
|
}
|
||||||
|
|
||||||
func TestList_Map_Performance(t *testing.T) {
|
func TestList_Map_Performance(t *testing.T) {
|
||||||
t.Log("max uint32", math.MaxUint32)
|
t.Log("max uint32", math.MaxUint32)
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func (this *Manager) init() {
|
|||||||
for range this.ticker.C {
|
for range this.ticker.C {
|
||||||
var currentTime = time.Now().Unix()
|
var currentTime = time.Now().Unix()
|
||||||
if lastTimestamp == 0 {
|
if lastTimestamp == 0 {
|
||||||
lastTimestamp = currentTime - 3600
|
lastTimestamp = currentTime - 86400 // prevent timezone changes
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentTime >= lastTimestamp {
|
if currentTime >= lastTimestamp {
|
||||||
|
|||||||
Reference in New Issue
Block a user