Files
EdgeNode/internal/ttlcache/piece.go

138 lines
2.6 KiB
Go
Raw Normal View History

2020-11-21 21:43:03 +08:00
package ttlcache
import (
2022-04-09 18:28:22 +08:00
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
2020-11-22 12:11:39 +08:00
"github.com/iwind/TeaGo/types"
"sync"
)
type Piece struct {
2022-04-09 18:28:22 +08:00
m map[uint64]*Item
expiresList *expires.List
maxItems int
lastGCTime int64
2022-03-12 18:00:22 +08:00
locker sync.RWMutex
}
2020-11-21 21:43:03 +08:00
func NewPiece(maxItems int) *Piece {
2022-04-09 18:28:22 +08:00
return &Piece{
m: map[uint64]*Item{},
expiresList: expires.NewSingletonList(),
maxItems: maxItems,
}
}
func (this *Piece) Add(key uint64, item *Item) (ok bool) {
this.locker.Lock()
2023-04-08 13:49:41 +08:00
if this.maxItems > 0 && len(this.m) >= this.maxItems {
2022-04-09 18:28:22 +08:00
this.locker.Unlock()
return
2020-11-21 21:43:03 +08:00
}
this.m[key] = item
this.locker.Unlock()
2022-04-09 18:28:22 +08:00
this.expiresList.Add(key, item.expiredAt)
return true
}
func (this *Piece) IncreaseInt64(key uint64, delta int64, expiredAt int64, extend bool) (result int64) {
2020-11-22 12:11:39 +08:00
this.locker.Lock()
item, ok := this.m[key]
2023-04-08 13:49:41 +08:00
if ok && item.expiredAt > fasttime.Now().Unix() {
result = types.Int64(item.Value) + delta
2020-11-22 12:11:39 +08:00
item.Value = result
if extend {
item.expiredAt = expiredAt
}
2022-04-09 18:28:22 +08:00
this.expiresList.Add(key, expiredAt)
2020-11-22 12:11:39 +08:00
} else {
if len(this.m) < this.maxItems {
result = delta
this.m[key] = &Item{
Value: delta,
expiredAt: expiredAt,
}
2022-04-09 18:28:22 +08:00
this.expiresList.Add(key, expiredAt)
2020-11-22 12:11:39 +08:00
}
}
this.locker.Unlock()
2022-04-09 18:28:22 +08:00
2020-11-22 12:11:39 +08:00
return
}
func (this *Piece) Delete(key uint64) {
2022-04-09 18:28:22 +08:00
this.expiresList.Remove(key)
this.locker.Lock()
delete(this.m, key)
this.locker.Unlock()
}
func (this *Piece) Read(key uint64) (item *Item) {
this.locker.RLock()
item = this.m[key]
if item != nil && item.expiredAt < fasttime.Now().Unix() {
item = nil
}
this.locker.RUnlock()
return
}
func (this *Piece) Count() (count int) {
this.locker.RLock()
count = len(this.m)
this.locker.RUnlock()
return
}
func (this *Piece) GC() {
2023-04-08 13:49:41 +08:00
var currentTime = fasttime.Now().Unix()
2022-04-09 18:28:22 +08:00
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
}
2020-11-22 12:11:39 +08:00
func (this *Piece) Clean() {
this.locker.Lock()
this.m = map[uint64]*Item{}
this.locker.Unlock()
2022-04-09 18:28:22 +08:00
this.expiresList.Clean()
}
2020-11-22 12:11:39 +08:00
func (this *Piece) Destroy() {
this.locker.Lock()
this.m = nil
this.locker.Unlock()
2022-04-09 18:28:22 +08:00
this.expiresList.Clean()
2020-11-22 12:11:39 +08:00
}
2021-12-22 16:43:16 +08:00
2022-04-09 18:28:22 +08:00
func (this *Piece) gcItemMap(itemMap expires.ItemMap) {
this.locker.Lock()
for key := range itemMap {
delete(this.m, key)
2021-12-22 16:43:16 +08:00
}
2022-04-09 18:28:22 +08:00
this.locker.Unlock()
2021-12-22 16:43:16 +08:00
}