mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-29 01:06:35 +08:00
优化ttlcache
This commit is contained in:
@@ -2,36 +2,40 @@ package ttlcache
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Piece struct {
|
||||
m map[uint64]*Item
|
||||
maxItems int
|
||||
m map[uint64]*Item
|
||||
expiresList *expires.List
|
||||
maxItems int
|
||||
lastGCTime int64
|
||||
|
||||
locker sync.RWMutex
|
||||
}
|
||||
|
||||
func NewPiece(maxItems int) *Piece {
|
||||
return &Piece{m: map[uint64]*Item{}, maxItems: maxItems}
|
||||
return &Piece{
|
||||
m: map[uint64]*Item{},
|
||||
expiresList: expires.NewSingletonList(),
|
||||
maxItems: maxItems,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Piece) Add(key uint64, item *Item) (ok bool) {
|
||||
this.locker.Lock()
|
||||
if len(this.m) >= this.maxItems {
|
||||
// 尝试先删除过期的
|
||||
this.gcWithoutLocker()
|
||||
|
||||
// 仍然是满的就跳过
|
||||
if len(this.m) >= this.maxItems {
|
||||
this.locker.Unlock()
|
||||
return
|
||||
}
|
||||
this.locker.Unlock()
|
||||
return
|
||||
}
|
||||
this.m[key] = item
|
||||
this.locker.Unlock()
|
||||
|
||||
this.expiresList.Add(key, item.expiredAt)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -42,6 +46,7 @@ func (this *Piece) IncreaseInt64(key uint64, delta int64, expiredAt int64) (resu
|
||||
result = types.Int64(item.Value) + delta
|
||||
item.Value = result
|
||||
item.expiredAt = expiredAt
|
||||
this.expiresList.Add(key, expiredAt)
|
||||
} else {
|
||||
if len(this.m) < this.maxItems {
|
||||
result = delta
|
||||
@@ -49,13 +54,17 @@ func (this *Piece) IncreaseInt64(key uint64, delta int64, expiredAt int64) (resu
|
||||
Value: delta,
|
||||
expiredAt: expiredAt,
|
||||
}
|
||||
this.expiresList.Add(key, expiredAt)
|
||||
}
|
||||
}
|
||||
this.locker.Unlock()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (this *Piece) Delete(key uint64) {
|
||||
this.expiresList.Remove(key)
|
||||
|
||||
this.locker.Lock()
|
||||
delete(this.m, key)
|
||||
this.locker.Unlock()
|
||||
@@ -80,29 +89,48 @@ func (this *Piece) Count() (count int) {
|
||||
}
|
||||
|
||||
func (this *Piece) GC() {
|
||||
this.locker.Lock()
|
||||
this.gcWithoutLocker()
|
||||
this.locker.Unlock()
|
||||
var currentTime = time.Now().Unix()
|
||||
if this.lastGCTime == 0 {
|
||||
this.lastGCTime = currentTime - 3600
|
||||
}
|
||||
|
||||
var min = this.lastGCTime
|
||||
var max = currentTime
|
||||
if min > max {
|
||||
// 过去的时间比现在大,则从这一秒重新开始
|
||||
min = max
|
||||
}
|
||||
|
||||
for i := min; i <= max; i++ {
|
||||
var itemMap = this.expiresList.GC(i)
|
||||
if len(itemMap) > 0 {
|
||||
this.gcItemMap(itemMap)
|
||||
}
|
||||
}
|
||||
|
||||
this.lastGCTime = currentTime
|
||||
}
|
||||
|
||||
func (this *Piece) Clean() {
|
||||
this.locker.Lock()
|
||||
this.m = map[uint64]*Item{}
|
||||
this.locker.Unlock()
|
||||
|
||||
this.expiresList.Clean()
|
||||
}
|
||||
|
||||
func (this *Piece) Destroy() {
|
||||
this.locker.Lock()
|
||||
this.m = nil
|
||||
this.locker.Unlock()
|
||||
|
||||
this.expiresList.Clean()
|
||||
}
|
||||
|
||||
// 不加锁的gc
|
||||
func (this *Piece) gcWithoutLocker() {
|
||||
timestamp := time.Now().Unix()
|
||||
for k, item := range this.m {
|
||||
if item.expiredAt <= timestamp {
|
||||
delete(this.m, k)
|
||||
}
|
||||
func (this *Piece) gcItemMap(itemMap expires.ItemMap) {
|
||||
this.locker.Lock()
|
||||
for key := range itemMap {
|
||||
delete(this.m, key)
|
||||
}
|
||||
this.locker.Unlock()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user