diff --git a/internal/ttlcache/cache.go b/internal/ttlcache/cache.go index bc1e872..c71dc6f 100644 --- a/internal/ttlcache/cache.go +++ b/internal/ttlcache/cache.go @@ -3,7 +3,6 @@ package ttlcache import ( "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" - "time" ) var SharedCache = NewCache() @@ -11,8 +10,10 @@ var SharedCache = NewCache() // Cache TTL缓存 // 最大的缓存时间为30 * 86400 // Piece数据结构: -// Piece1 | Piece2 | Piece3 | ... -// [ Item1, Item2, ... ] | ... +// +// Piece1 | Piece2 | Piece3 | ... +// [ Item1, Item2, ... ] | ... +// // KeyMap列表数据结构 // { timestamp1 => [key1, key2, ...] }, ... type Cache struct { @@ -70,7 +71,7 @@ func NewCache(opt ...OptionInterface) *Cache { return cache } -func (this *Cache) Write(key string, value interface{}, expiredAt int64) (ok bool) { +func (this *Cache) Write(key string, value any, expiredAt int64) (ok bool) { if this.isDestroyed { return } @@ -84,8 +85,8 @@ func (this *Cache) Write(key string, value interface{}, expiredAt int64) (ok boo if expiredAt > maxExpiredAt { expiredAt = maxExpiredAt } - uint64Key := HashKey([]byte(key)) - pieceIndex := uint64Key % this.countPieces + var uint64Key = HashKey([]byte(key)) + var pieceIndex = uint64Key % this.countPieces return this.pieces[pieceIndex].Add(uint64Key, &Item{ Value: value, expiredAt: expiredAt, @@ -97,22 +98,22 @@ func (this *Cache) IncreaseInt64(key string, delta int64, expiredAt int64, exten return 0 } - currentTimestamp := time.Now().Unix() + var currentTimestamp = fasttime.Now().Unix() if expiredAt <= currentTimestamp { return 0 } - maxExpiredAt := currentTimestamp + 30*86400 + var maxExpiredAt = currentTimestamp + 30*86400 if expiredAt > maxExpiredAt { expiredAt = maxExpiredAt } - uint64Key := HashKey([]byte(key)) - pieceIndex := uint64Key % this.countPieces + var uint64Key = HashKey([]byte(key)) + var pieceIndex = uint64Key % this.countPieces return this.pieces[pieceIndex].IncreaseInt64(uint64Key, delta, expiredAt, extend) } func (this *Cache) Read(key string) (item *Item) { - uint64Key := HashKey([]byte(key)) + var uint64Key = HashKey([]byte(key)) return this.pieces[uint64Key%this.countPieces].Read(uint64Key) } @@ -121,7 +122,7 @@ func (this *Cache) readIntKey(key uint64) (value *Item) { } func (this *Cache) Delete(key string) { - uint64Key := HashKey([]byte(key)) + var uint64Key = HashKey([]byte(key)) this.pieces[uint64Key%this.countPieces].Delete(uint64Key) } @@ -138,7 +139,7 @@ func (this *Cache) Count() (count int) { func (this *Cache) GC() { this.pieces[this.gcPieceIndex].GC() - newIndex := this.gcPieceIndex + 1 + var newIndex = this.gcPieceIndex + 1 if newIndex >= int(this.countPieces) { newIndex = 0 } diff --git a/internal/ttlcache/item.go b/internal/ttlcache/item.go index 9720e29..5b0c501 100644 --- a/internal/ttlcache/item.go +++ b/internal/ttlcache/item.go @@ -1,6 +1,6 @@ package ttlcache type Item struct { - Value interface{} + Value any expiredAt int64 } diff --git a/internal/ttlcache/piece.go b/internal/ttlcache/piece.go index 8d7e2fd..3e9d4df 100644 --- a/internal/ttlcache/piece.go +++ b/internal/ttlcache/piece.go @@ -5,7 +5,6 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" "github.com/iwind/TeaGo/types" "sync" - "time" ) type Piece struct { @@ -27,7 +26,7 @@ func NewPiece(maxItems int) *Piece { func (this *Piece) Add(key uint64, item *Item) (ok bool) { this.locker.Lock() - if len(this.m) >= this.maxItems { + if this.maxItems > 0 && len(this.m) >= this.maxItems { this.locker.Unlock() return } @@ -42,7 +41,7 @@ func (this *Piece) Add(key uint64, item *Item) (ok bool) { func (this *Piece) IncreaseInt64(key uint64, delta int64, expiredAt int64, extend bool) (result int64) { this.locker.Lock() item, ok := this.m[key] - if ok && item.expiredAt > time.Now().Unix() { + if ok && item.expiredAt > fasttime.Now().Unix() { result = types.Int64(item.Value) + delta item.Value = result if extend { @@ -91,7 +90,7 @@ func (this *Piece) Count() (count int) { } func (this *Piece) GC() { - var currentTime = time.Now().Unix() + var currentTime = fasttime.Now().Unix() if this.lastGCTime == 0 { this.lastGCTime = currentTime - 3600 }