彻底替换掉memorygrid

This commit is contained in:
刘祥超
2020-11-22 12:11:39 +08:00
parent 964bacc36f
commit 02840a78cd
21 changed files with 101 additions and 1232 deletions

View File

@@ -2,6 +2,7 @@ package ttlcache
import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/types"
"sync"
"time"
)
@@ -26,6 +27,26 @@ func (this *Piece) Add(key uint64, item *Item) () {
this.locker.Unlock()
}
func (this *Piece) IncreaseInt64(key uint64, delta int64, expiredAt int64) (result int64) {
this.locker.Lock()
item, ok := this.m[key]
if ok {
result := types.Int64(item.Value) + delta
item.Value = result
item.expiredAt = expiredAt
} else {
if len(this.m) < this.maxItems {
result = delta
this.m[key] = &Item{
Value: delta,
expiredAt: expiredAt,
}
}
}
this.locker.Unlock()
return
}
func (this *Piece) Delete(key uint64) {
this.locker.Lock()
delete(this.m, key)
@@ -60,3 +81,9 @@ func (this *Piece) GC() {
}
this.locker.Unlock()
}
func (this *Piece) Destroy() {
this.locker.Lock()
this.m = nil
this.locker.Unlock()
}