mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	优化ttlcache相关代码
This commit is contained in:
		@@ -6,10 +6,10 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
 | 
				
			||||||
	memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
 | 
						memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
 | 
				
			||||||
	"github.com/iwind/TeaGo/types"
 | 
						"github.com/iwind/TeaGo/types"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,9 +4,9 @@ package caches
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/kvstore"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/kvstore"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
 | 
				
			||||||
	"github.com/cockroachdb/pebble"
 | 
						"github.com/cockroachdb/pebble"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,11 +7,11 @@ import (
 | 
				
			|||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/dbs"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/dbs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
 | 
				
			||||||
	fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
 | 
						fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/zero"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/zero"
 | 
				
			||||||
	"github.com/iwind/TeaGo/types"
 | 
						"github.com/iwind/TeaGo/types"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,8 +6,8 @@ import (
 | 
				
			|||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeutils"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/nodeutils"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/waf"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/waf"
 | 
				
			||||||
	"github.com/iwind/TeaGo/types"
 | 
						"github.com/iwind/TeaGo/types"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -85,45 +85,45 @@ func NewCache[T any](opt ...OptionInterface) *Cache[T] {
 | 
				
			|||||||
	return cache
 | 
						return cache
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Cache[T]) Write(key string, value T, expiredAt int64) (ok bool) {
 | 
					func (this *Cache[T]) Write(key string, value T, expiresAt int64) (ok bool) {
 | 
				
			||||||
	if this.isDestroyed {
 | 
						if this.isDestroyed {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var currentTimestamp = fasttime.Now().Unix()
 | 
						var currentTimestamp = fasttime.Now().Unix()
 | 
				
			||||||
	if expiredAt <= currentTimestamp {
 | 
						if expiresAt <= currentTimestamp {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var maxExpiredAt = currentTimestamp + 30*86400
 | 
						var maxExpiresAt = currentTimestamp + 30*86400
 | 
				
			||||||
	if expiredAt > maxExpiredAt {
 | 
						if expiresAt > maxExpiresAt {
 | 
				
			||||||
		expiredAt = maxExpiredAt
 | 
							expiresAt = maxExpiresAt
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var uint64Key = HashKeyString(key)
 | 
						var uint64Key = HashKeyString(key)
 | 
				
			||||||
	var pieceIndex = uint64Key % this.countPieces
 | 
						var pieceIndex = uint64Key % this.countPieces
 | 
				
			||||||
	return this.pieces[pieceIndex].Add(uint64Key, &Item[T]{
 | 
						return this.pieces[pieceIndex].Add(uint64Key, &Item[T]{
 | 
				
			||||||
		Value:     value,
 | 
							Value:     value,
 | 
				
			||||||
		expiredAt: expiredAt,
 | 
							expiresAt: expiresAt,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Cache[T]) IncreaseInt64(key string, delta T, expiredAt int64, extend bool) T {
 | 
					func (this *Cache[T]) IncreaseInt64(key string, delta T, expiresAt int64, extend bool) T {
 | 
				
			||||||
	if this.isDestroyed {
 | 
						if this.isDestroyed {
 | 
				
			||||||
		return any(0).(T)
 | 
							return any(0).(T)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var currentTimestamp = fasttime.Now().Unix()
 | 
						var currentTimestamp = fasttime.Now().Unix()
 | 
				
			||||||
	if expiredAt <= currentTimestamp {
 | 
						if expiresAt <= currentTimestamp {
 | 
				
			||||||
		return any(0).(T)
 | 
							return any(0).(T)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var maxExpiredAt = currentTimestamp + 30*86400
 | 
						var maxExpiresAt = currentTimestamp + 30*86400
 | 
				
			||||||
	if expiredAt > maxExpiredAt {
 | 
						if expiresAt > maxExpiresAt {
 | 
				
			||||||
		expiredAt = maxExpiredAt
 | 
							expiresAt = maxExpiresAt
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var uint64Key = HashKeyString(key)
 | 
						var uint64Key = HashKeyString(key)
 | 
				
			||||||
	var pieceIndex = uint64Key % this.countPieces
 | 
						var pieceIndex = uint64Key % this.countPieces
 | 
				
			||||||
	return this.pieces[pieceIndex].IncreaseInt64(uint64Key, delta, expiredAt, extend)
 | 
						return this.pieces[pieceIndex].IncreaseInt64(uint64Key, delta, expiresAt, extend)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Cache[T]) Read(key string) (item *Item[T]) {
 | 
					func (this *Cache[T]) Read(key string) (item *Item[T]) {
 | 
				
			||||||
@@ -11,6 +11,7 @@ import (
 | 
				
			|||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
	"runtime/debug"
 | 
						"runtime/debug"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
	"sync/atomic"
 | 
						"sync/atomic"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -26,7 +27,7 @@ func TestNewCache(t *testing.T) {
 | 
				
			|||||||
	for _, piece := range cache.pieces {
 | 
						for _, piece := range cache.pieces {
 | 
				
			||||||
		if len(piece.m) > 0 {
 | 
							if len(piece.m) > 0 {
 | 
				
			||||||
			for k, item := range piece.m {
 | 
								for k, item := range piece.m {
 | 
				
			||||||
				t.Log(k, "=>", item.Value, item.expiredAt)
 | 
									t.Log(k, "=>", item.Value, item.expiresAt)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -63,9 +64,25 @@ func TestCache_Memory(t *testing.T) {
 | 
				
			|||||||
	if memutils.SystemMemoryGB() > 4 {
 | 
						if memutils.SystemMemoryGB() > 4 {
 | 
				
			||||||
		count = 20_000_000
 | 
							count = 20_000_000
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for i := 0; i < count; i++ {
 | 
					
 | 
				
			||||||
		cache.Write("a"+strconv.Itoa(i), 1, time.Now().Unix()+int64(rands.Int(0, 300)))
 | 
						var concurrent = runtime.NumCPU()
 | 
				
			||||||
 | 
						var wg = &sync.WaitGroup{}
 | 
				
			||||||
 | 
						wg.Add(concurrent)
 | 
				
			||||||
 | 
						var id int64
 | 
				
			||||||
 | 
						for i := 0; i < concurrent; i++ {
 | 
				
			||||||
 | 
							go func() {
 | 
				
			||||||
 | 
								defer wg.Done()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								for {
 | 
				
			||||||
 | 
									var newId = atomic.AddInt64(&id, 1)
 | 
				
			||||||
 | 
									if newId > int64(count) {
 | 
				
			||||||
 | 
										return
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									cache.Write("a"+types.String(newId), 1, time.Now().Unix()+int64(rands.Int(1, 300)))
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						wg.Wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	func() {
 | 
						func() {
 | 
				
			||||||
		var before = time.Now()
 | 
							var before = time.Now()
 | 
				
			||||||
@@ -105,14 +122,14 @@ func TestCache_IncreaseInt64(t *testing.T) {
 | 
				
			|||||||
		var item = cache.Read("a")
 | 
							var item = cache.Read("a")
 | 
				
			||||||
		t.Log(item)
 | 
							t.Log(item)
 | 
				
			||||||
		a.IsTrue(item.Value == 1)
 | 
							a.IsTrue(item.Value == 1)
 | 
				
			||||||
		a.IsTrue(item.expiredAt == unixTime+3600)
 | 
							a.IsTrue(item.expiresAt == unixTime+3600)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		cache.IncreaseInt64("a", 1, unixTime+3600+1, true)
 | 
							cache.IncreaseInt64("a", 1, unixTime+3600+1, true)
 | 
				
			||||||
		var item = cache.Read("a")
 | 
							var item = cache.Read("a")
 | 
				
			||||||
		t.Log(item)
 | 
							t.Log(item)
 | 
				
			||||||
		a.IsTrue(item.Value == 2)
 | 
							a.IsTrue(item.Value == 2)
 | 
				
			||||||
		a.IsTrue(item.expiredAt == unixTime+3600+1)
 | 
							a.IsTrue(item.expiresAt == unixTime+3600+1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		cache.Write("b", 1, time.Now().Unix()+3600+2)
 | 
							cache.Write("b", 1, time.Now().Unix()+3600+2)
 | 
				
			||||||
@@ -185,7 +202,7 @@ func TestCache_GC(t *testing.T) {
 | 
				
			|||||||
	for _, p := range cache.pieces {
 | 
						for _, p := range cache.pieces {
 | 
				
			||||||
		t.Log("expire list:", p.expiresList.Count(), p.expiresList)
 | 
							t.Log("expire list:", p.expiresList.Count(), p.expiresList)
 | 
				
			||||||
		for k, v := range p.m {
 | 
							for k, v := range p.m {
 | 
				
			||||||
			t.Log(k, v.Value, v.expiredAt)
 | 
								t.Log(k, v.Value, v.expiresAt)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -223,6 +240,21 @@ func TestCacheDestroy(t *testing.T) {
 | 
				
			|||||||
	t.Log("count:", SharedManager.Count())
 | 
						t.Log("count:", SharedManager.Count())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestCache_Clean(t *testing.T) {
 | 
				
			||||||
 | 
						var cache = NewCache[int]()
 | 
				
			||||||
 | 
						cache.Clean()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestCache_Destroy(t *testing.T) {
 | 
				
			||||||
 | 
						var cache = NewCache[int]()
 | 
				
			||||||
 | 
						t.Log(SharedManager.Count())
 | 
				
			||||||
 | 
						for i := 0; i < 1_000; i++ {
 | 
				
			||||||
 | 
							cache.Write("a"+types.String(i), 1, fasttime.Now().Unix()+3600)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						cache.Destroy()
 | 
				
			||||||
 | 
						t.Log(SharedManager.Count())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func BenchmarkNewCache(b *testing.B) {
 | 
					func BenchmarkNewCache(b *testing.B) {
 | 
				
			||||||
	runtime.GOMAXPROCS(1)
 | 
						runtime.GOMAXPROCS(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -2,5 +2,5 @@ package ttlcache
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type Item[T any] struct {
 | 
					type Item[T any] struct {
 | 
				
			||||||
	Value     T
 | 
						Value     T
 | 
				
			||||||
	expiredAt int64
 | 
						expiresAt int64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -33,39 +33,39 @@ func (this *Piece[T]) Add(key uint64, item *Item[T]) (ok bool) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	this.locker.Lock()
 | 
						this.locker.Lock()
 | 
				
			||||||
	oldItem, exists := this.m[key]
 | 
						oldItem, exists := this.m[key]
 | 
				
			||||||
	if exists && oldItem.expiredAt == item.expiredAt {
 | 
						if exists && oldItem.expiresAt == item.expiresAt {
 | 
				
			||||||
		this.locker.Unlock()
 | 
							this.locker.Unlock()
 | 
				
			||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	this.m[key] = item
 | 
						this.m[key] = item
 | 
				
			||||||
	this.locker.Unlock()
 | 
						this.locker.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this.expiresList.Add(key, item.expiredAt)
 | 
						this.expiresList.Add(key, item.expiresAt)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Piece[T]) IncreaseInt64(key uint64, delta T, expiredAt int64, extend bool) (result T) {
 | 
					func (this *Piece[T]) IncreaseInt64(key uint64, delta T, expiresAt int64, extend bool) (result T) {
 | 
				
			||||||
	this.locker.Lock()
 | 
						this.locker.Lock()
 | 
				
			||||||
	item, ok := this.m[key]
 | 
						item, ok := this.m[key]
 | 
				
			||||||
	if ok && item.expiredAt > fasttime.Now().Unix() {
 | 
						if ok && item.expiresAt > fasttime.Now().Unix() {
 | 
				
			||||||
		int64Value, isInt64 := any(item.Value).(int64)
 | 
							int64Value, isInt64 := any(item.Value).(int64)
 | 
				
			||||||
		if isInt64 {
 | 
							if isInt64 {
 | 
				
			||||||
			result = any(int64Value + any(delta).(int64)).(T)
 | 
								result = any(int64Value + any(delta).(int64)).(T)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		item.Value = result
 | 
							item.Value = result
 | 
				
			||||||
		if extend {
 | 
							if extend {
 | 
				
			||||||
			item.expiredAt = expiredAt
 | 
								item.expiresAt = expiresAt
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		this.expiresList.Add(key, expiredAt)
 | 
							this.expiresList.Add(key, expiresAt)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		if len(this.m) < this.maxItems {
 | 
							if len(this.m) < this.maxItems {
 | 
				
			||||||
			result = delta
 | 
								result = delta
 | 
				
			||||||
			this.m[key] = &Item[T]{
 | 
								this.m[key] = &Item[T]{
 | 
				
			||||||
				Value:     delta,
 | 
									Value:     delta,
 | 
				
			||||||
				expiredAt: expiredAt,
 | 
									expiresAt: expiresAt,
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			this.expiresList.Add(key, expiredAt)
 | 
								this.expiresList.Add(key, expiresAt)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	this.locker.Unlock()
 | 
						this.locker.Unlock()
 | 
				
			||||||
@@ -84,7 +84,7 @@ func (this *Piece[T]) Delete(key uint64) {
 | 
				
			|||||||
func (this *Piece[T]) Read(key uint64) (item *Item[T]) {
 | 
					func (this *Piece[T]) Read(key uint64) (item *Item[T]) {
 | 
				
			||||||
	this.locker.RLock()
 | 
						this.locker.RLock()
 | 
				
			||||||
	item = this.m[key]
 | 
						item = this.m[key]
 | 
				
			||||||
	if item != nil && item.expiredAt < fasttime.Now().Unix() {
 | 
						if item != nil && item.expiresAt < fasttime.Now().Unix() {
 | 
				
			||||||
		item = nil
 | 
							item = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	this.locker.RUnlock()
 | 
						this.locker.RUnlock()
 | 
				
			||||||
@@ -133,6 +133,7 @@ func (this *Piece[T]) Clean() {
 | 
				
			|||||||
func (this *Piece[T]) Destroy() {
 | 
					func (this *Piece[T]) Destroy() {
 | 
				
			||||||
	this.locker.Lock()
 | 
						this.locker.Lock()
 | 
				
			||||||
	this.m = nil
 | 
						this.m = nil
 | 
				
			||||||
 | 
						this.expiresList.Clean()
 | 
				
			||||||
	this.locker.Unlock()
 | 
						this.locker.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this.expiresList.Clean()
 | 
						this.expiresList.Clean()
 | 
				
			||||||
@@ -8,7 +8,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func TestPiece_Add(t *testing.T) {
 | 
					func TestPiece_Add(t *testing.T) {
 | 
				
			||||||
	piece := NewPiece[int](10)
 | 
						piece := NewPiece[int](10)
 | 
				
			||||||
	piece.Add(1, &Item[int]{expiredAt: time.Now().Unix() + 3600})
 | 
						piece.Add(1, &Item[int]{expiresAt: time.Now().Unix() + 3600})
 | 
				
			||||||
	piece.Add(2, &Item[int]{})
 | 
						piece.Add(2, &Item[int]{})
 | 
				
			||||||
	piece.Add(3, &Item[int]{})
 | 
						piece.Add(3, &Item[int]{})
 | 
				
			||||||
	piece.Delete(3)
 | 
						piece.Delete(3)
 | 
				
			||||||
@@ -19,9 +19,9 @@ func TestPiece_Add(t *testing.T) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestPiece_Add_Same(t *testing.T) {
 | 
					func TestPiece_Add_Same(t *testing.T) {
 | 
				
			||||||
	piece := NewPiece[int](10)
 | 
						var piece = NewPiece[int](10)
 | 
				
			||||||
	piece.Add(1, &Item[int]{expiredAt: time.Now().Unix() + 3600})
 | 
						piece.Add(1, &Item[int]{expiresAt: time.Now().Unix() + 3600})
 | 
				
			||||||
	piece.Add(1, &Item[int]{expiredAt: time.Now().Unix() + 3600})
 | 
						piece.Add(1, &Item[int]{expiresAt: time.Now().Unix() + 3600})
 | 
				
			||||||
	for key, item := range piece.m {
 | 
						for key, item := range piece.m {
 | 
				
			||||||
		t.Log(key, item.Value)
 | 
							t.Log(key, item.Value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -31,16 +31,16 @@ func TestPiece_Add_Same(t *testing.T) {
 | 
				
			|||||||
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++ {
 | 
				
			||||||
		piece.Add(uint64(i), &Item[int]{expiredAt: time.Now().Unix() + 3600})
 | 
							piece.Add(uint64(i), &Item[int]{expiresAt: time.Now().Unix() + 3600})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	t.Log(len(piece.m))
 | 
						t.Log(len(piece.m))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestPiece_GC(t *testing.T) {
 | 
					func TestPiece_GC(t *testing.T) {
 | 
				
			||||||
	piece := NewPiece[int](10)
 | 
						piece := NewPiece[int](10)
 | 
				
			||||||
	piece.Add(1, &Item[int]{Value: 1, expiredAt: time.Now().Unix() + 1})
 | 
						piece.Add(1, &Item[int]{Value: 1, expiresAt: time.Now().Unix() + 1})
 | 
				
			||||||
	piece.Add(2, &Item[int]{Value: 2, expiredAt: time.Now().Unix() + 1})
 | 
						piece.Add(2, &Item[int]{Value: 2, expiresAt: time.Now().Unix() + 1})
 | 
				
			||||||
	piece.Add(3, &Item[int]{Value: 3, expiredAt: time.Now().Unix() + 1})
 | 
						piece.Add(3, &Item[int]{Value: 3, expiresAt: time.Now().Unix() + 1})
 | 
				
			||||||
	t.Log("before gc ===")
 | 
						t.Log("before gc ===")
 | 
				
			||||||
	for key, item := range piece.m {
 | 
						for key, item := range piece.m {
 | 
				
			||||||
		t.Log(key, item.Value)
 | 
							t.Log(key, item.Value)
 | 
				
			||||||
@@ -58,7 +58,7 @@ func TestPiece_GC(t *testing.T) {
 | 
				
			|||||||
func TestPiece_GC2(t *testing.T) {
 | 
					func TestPiece_GC2(t *testing.T) {
 | 
				
			||||||
	piece := NewPiece[int](10)
 | 
						piece := NewPiece[int](10)
 | 
				
			||||||
	for i := 0; i < 10_000; i++ {
 | 
						for i := 0; i < 10_000; i++ {
 | 
				
			||||||
		piece.Add(uint64(i), &Item[int]{Value: 1, expiredAt: time.Now().Unix() + int64(rands.Int(1, 10))})
 | 
							piece.Add(uint64(i), &Item[int]{Value: 1, expiresAt: time.Now().Unix() + int64(rands.Int(1, 10))})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	time.Sleep(1 * time.Second)
 | 
						time.Sleep(1 * time.Second)
 | 
				
			||||||
@@ -1,8 +1,8 @@
 | 
				
			|||||||
package ttlcache_test
 | 
					package ttlcache_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/zero"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/zero"
 | 
				
			||||||
	"github.com/cespare/xxhash/v2"
 | 
						"github.com/cespare/xxhash/v2"
 | 
				
			||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
@@ -7,9 +7,9 @@ import (
 | 
				
			|||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
 | 
						"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/compressions"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/compressions"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
 | 
				
			||||||
	wafutils "github.com/TeaOSLab/EdgeNode/internal/waf/utils"
 | 
						wafutils "github.com/TeaOSLab/EdgeNode/internal/waf/utils"
 | 
				
			||||||
	"github.com/iwind/TeaGo/logs"
 | 
						"github.com/iwind/TeaGo/logs"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,9 +3,9 @@ package utils
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
 | 
						teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/re"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/re"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/cachehits"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/cachehits"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
 | 
				
			||||||
	"github.com/cespare/xxhash/v2"
 | 
						"github.com/cespare/xxhash/v2"
 | 
				
			||||||
	"github.com/iwind/TeaGo/types"
 | 
						"github.com/iwind/TeaGo/types"
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user