2020-11-21 21:43:03 +08:00
|
|
|
package ttlcache
|
2020-11-21 20:44:19 +08:00
|
|
|
|
|
|
|
|
import (
|
2022-04-09 18:28:22 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
2023-04-08 12:47:04 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
2020-11-21 20:44:19 +08:00
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
type Piece[T any] struct {
|
|
|
|
|
m map[uint64]*Item[T]
|
2022-04-09 18:28:22 +08:00
|
|
|
expiresList *expires.List
|
|
|
|
|
maxItems int
|
|
|
|
|
lastGCTime int64
|
2022-03-12 18:00:22 +08:00
|
|
|
|
|
|
|
|
locker sync.RWMutex
|
2020-11-21 20:44:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func NewPiece[T any](maxItems int) *Piece[T] {
|
|
|
|
|
return &Piece[T]{
|
|
|
|
|
m: map[uint64]*Item[T]{},
|
2022-04-09 18:28:22 +08:00
|
|
|
expiresList: expires.NewSingletonList(),
|
|
|
|
|
maxItems: maxItems,
|
|
|
|
|
}
|
2020-11-21 20:44:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) Add(key uint64, item *Item[T]) (ok bool) {
|
2023-10-11 14:07:13 +08:00
|
|
|
this.locker.RLock()
|
2023-04-08 13:49:41 +08:00
|
|
|
if this.maxItems > 0 && len(this.m) >= this.maxItems {
|
2023-10-11 14:07:13 +08:00
|
|
|
this.locker.RUnlock()
|
2022-04-09 18:28:22 +08:00
|
|
|
return
|
2020-11-21 21:43:03 +08:00
|
|
|
}
|
2023-10-11 14:07:13 +08:00
|
|
|
this.locker.RUnlock()
|
|
|
|
|
|
|
|
|
|
this.locker.Lock()
|
|
|
|
|
oldItem, exists := this.m[key]
|
|
|
|
|
if exists && oldItem.expiredAt == item.expiredAt {
|
|
|
|
|
this.locker.Unlock()
|
|
|
|
|
return true
|
|
|
|
|
}
|
2020-11-21 20:44:19 +08:00
|
|
|
this.m[key] = item
|
|
|
|
|
this.locker.Unlock()
|
2022-04-09 18:28:22 +08:00
|
|
|
|
|
|
|
|
this.expiresList.Add(key, item.expiredAt)
|
|
|
|
|
|
2021-10-04 09:12:17 +08:00
|
|
|
return true
|
2020-11-21 20:44:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) IncreaseInt64(key uint64, delta T, expiredAt int64, extend bool) (result T) {
|
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() {
|
2023-10-05 08:28:16 +08:00
|
|
|
int64Value, isInt64 := any(item.Value).(int64)
|
|
|
|
|
if isInt64 {
|
|
|
|
|
result = any(int64Value + any(delta).(int64)).(T)
|
|
|
|
|
}
|
2020-11-22 12:11:39 +08:00
|
|
|
item.Value = result
|
2022-05-12 21:48:33 +08:00
|
|
|
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
|
2023-10-05 08:28:16 +08:00
|
|
|
this.m[key] = &Item[T]{
|
2020-11-22 12:11:39 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) Delete(key uint64) {
|
2022-04-09 18:28:22 +08:00
|
|
|
this.expiresList.Remove(key)
|
|
|
|
|
|
2020-11-21 20:44:19 +08:00
|
|
|
this.locker.Lock()
|
|
|
|
|
delete(this.m, key)
|
|
|
|
|
this.locker.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) Read(key uint64) (item *Item[T]) {
|
2020-11-21 20:44:19 +08:00
|
|
|
this.locker.RLock()
|
|
|
|
|
item = this.m[key]
|
2023-04-08 12:47:04 +08:00
|
|
|
if item != nil && item.expiredAt < fasttime.Now().Unix() {
|
2020-11-21 20:44:19 +08:00
|
|
|
item = nil
|
|
|
|
|
}
|
|
|
|
|
this.locker.RUnlock()
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) Count() (count int) {
|
2020-11-21 20:44:19 +08:00
|
|
|
this.locker.RLock()
|
|
|
|
|
count = len(this.m)
|
|
|
|
|
this.locker.RUnlock()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) 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
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 16:03:52 +08:00
|
|
|
var minTime = this.lastGCTime
|
|
|
|
|
var maxTime = currentTime
|
|
|
|
|
if minTime > maxTime {
|
2022-04-09 18:28:22 +08:00
|
|
|
// 过去的时间比现在大,则从这一秒重新开始
|
2023-10-12 16:03:52 +08:00
|
|
|
minTime = maxTime
|
2022-04-09 18:28:22 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-12 16:03:52 +08:00
|
|
|
for i := minTime; i <= maxTime; i++ {
|
2022-04-09 18:28:22 +08:00
|
|
|
var itemMap = this.expiresList.GC(i)
|
|
|
|
|
if len(itemMap) > 0 {
|
|
|
|
|
this.gcItemMap(itemMap)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.lastGCTime = currentTime
|
2020-11-21 20:44:19 +08:00
|
|
|
}
|
2020-11-22 12:11:39 +08:00
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) Clean() {
|
2021-08-21 21:06:48 +08:00
|
|
|
this.locker.Lock()
|
2023-10-05 08:28:16 +08:00
|
|
|
this.m = map[uint64]*Item[T]{}
|
2021-08-21 21:06:48 +08:00
|
|
|
this.locker.Unlock()
|
2022-04-09 18:28:22 +08:00
|
|
|
|
|
|
|
|
this.expiresList.Clean()
|
2021-08-21 21:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) Destroy() {
|
2020-11-22 12:11:39 +08:00
|
|
|
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
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
func (this *Piece[T]) gcItemMap(itemMap expires.ItemMap) {
|
2022-04-09 18:28:22 +08:00
|
|
|
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
|
|
|
}
|