mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-02 05:50:25 +08:00
优化代码
This commit is contained in:
@@ -3,7 +3,6 @@ package ttlcache
|
|||||||
import (
|
import (
|
||||||
"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"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var SharedCache = NewCache()
|
var SharedCache = NewCache()
|
||||||
@@ -11,8 +10,10 @@ var SharedCache = NewCache()
|
|||||||
// Cache TTL缓存
|
// Cache TTL缓存
|
||||||
// 最大的缓存时间为30 * 86400
|
// 最大的缓存时间为30 * 86400
|
||||||
// Piece数据结构:
|
// Piece数据结构:
|
||||||
|
//
|
||||||
// Piece1 | Piece2 | Piece3 | ...
|
// Piece1 | Piece2 | Piece3 | ...
|
||||||
// [ Item1, Item2, ... ] | ...
|
// [ Item1, Item2, ... ] | ...
|
||||||
|
//
|
||||||
// KeyMap列表数据结构
|
// KeyMap列表数据结构
|
||||||
// { timestamp1 => [key1, key2, ...] }, ...
|
// { timestamp1 => [key1, key2, ...] }, ...
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
@@ -70,7 +71,7 @@ func NewCache(opt ...OptionInterface) *Cache {
|
|||||||
return 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 {
|
if this.isDestroyed {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -84,8 +85,8 @@ func (this *Cache) Write(key string, value interface{}, expiredAt int64) (ok boo
|
|||||||
if expiredAt > maxExpiredAt {
|
if expiredAt > maxExpiredAt {
|
||||||
expiredAt = maxExpiredAt
|
expiredAt = maxExpiredAt
|
||||||
}
|
}
|
||||||
uint64Key := HashKey([]byte(key))
|
var uint64Key = HashKey([]byte(key))
|
||||||
pieceIndex := uint64Key % this.countPieces
|
var pieceIndex = uint64Key % this.countPieces
|
||||||
return this.pieces[pieceIndex].Add(uint64Key, &Item{
|
return this.pieces[pieceIndex].Add(uint64Key, &Item{
|
||||||
Value: value,
|
Value: value,
|
||||||
expiredAt: expiredAt,
|
expiredAt: expiredAt,
|
||||||
@@ -97,22 +98,22 @@ func (this *Cache) IncreaseInt64(key string, delta int64, expiredAt int64, exten
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
currentTimestamp := time.Now().Unix()
|
var currentTimestamp = fasttime.Now().Unix()
|
||||||
if expiredAt <= currentTimestamp {
|
if expiredAt <= currentTimestamp {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
maxExpiredAt := currentTimestamp + 30*86400
|
var maxExpiredAt = currentTimestamp + 30*86400
|
||||||
if expiredAt > maxExpiredAt {
|
if expiredAt > maxExpiredAt {
|
||||||
expiredAt = maxExpiredAt
|
expiredAt = maxExpiredAt
|
||||||
}
|
}
|
||||||
uint64Key := HashKey([]byte(key))
|
var uint64Key = HashKey([]byte(key))
|
||||||
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, expiredAt, extend)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Cache) Read(key string) (item *Item) {
|
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)
|
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) {
|
func (this *Cache) Delete(key string) {
|
||||||
uint64Key := HashKey([]byte(key))
|
var uint64Key = HashKey([]byte(key))
|
||||||
this.pieces[uint64Key%this.countPieces].Delete(uint64Key)
|
this.pieces[uint64Key%this.countPieces].Delete(uint64Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +139,7 @@ func (this *Cache) Count() (count int) {
|
|||||||
|
|
||||||
func (this *Cache) GC() {
|
func (this *Cache) GC() {
|
||||||
this.pieces[this.gcPieceIndex].GC()
|
this.pieces[this.gcPieceIndex].GC()
|
||||||
newIndex := this.gcPieceIndex + 1
|
var newIndex = this.gcPieceIndex + 1
|
||||||
if newIndex >= int(this.countPieces) {
|
if newIndex >= int(this.countPieces) {
|
||||||
newIndex = 0
|
newIndex = 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ttlcache
|
package ttlcache
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
Value interface{}
|
Value any
|
||||||
expiredAt int64
|
expiredAt int64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Piece struct {
|
type Piece struct {
|
||||||
@@ -27,7 +26,7 @@ func NewPiece(maxItems int) *Piece {
|
|||||||
|
|
||||||
func (this *Piece) Add(key uint64, item *Item) (ok bool) {
|
func (this *Piece) Add(key uint64, item *Item) (ok bool) {
|
||||||
this.locker.Lock()
|
this.locker.Lock()
|
||||||
if len(this.m) >= this.maxItems {
|
if this.maxItems > 0 && len(this.m) >= this.maxItems {
|
||||||
this.locker.Unlock()
|
this.locker.Unlock()
|
||||||
return
|
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) {
|
func (this *Piece) IncreaseInt64(key uint64, delta int64, expiredAt int64, extend bool) (result int64) {
|
||||||
this.locker.Lock()
|
this.locker.Lock()
|
||||||
item, ok := this.m[key]
|
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
|
result = types.Int64(item.Value) + delta
|
||||||
item.Value = result
|
item.Value = result
|
||||||
if extend {
|
if extend {
|
||||||
@@ -91,7 +90,7 @@ func (this *Piece) Count() (count int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *Piece) GC() {
|
func (this *Piece) GC() {
|
||||||
var currentTime = time.Now().Unix()
|
var currentTime = fasttime.Now().Unix()
|
||||||
if this.lastGCTime == 0 {
|
if this.lastGCTime == 0 {
|
||||||
this.lastGCTime = currentTime - 3600
|
this.lastGCTime = currentTime - 3600
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user