2020-11-21 21:43:03 +08:00
|
|
|
|
package ttlcache
|
2020-11-21 20:44:19 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2023-04-08 12:47:04 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
2024-03-28 17:17:34 +08:00
|
|
|
|
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
2023-10-12 16:03:52 +08:00
|
|
|
|
"runtime"
|
2020-11-21 20:44:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
var SharedInt64Cache = NewBigCache[int64]()
|
2022-01-10 19:54:10 +08:00
|
|
|
|
|
2021-08-21 21:06:48 +08:00
|
|
|
|
// Cache TTL缓存
|
2020-11-21 20:44:19 +08:00
|
|
|
|
// 最大的缓存时间为30 * 86400
|
|
|
|
|
|
// Piece数据结构:
|
2023-04-08 13:49:41 +08:00
|
|
|
|
//
|
|
|
|
|
|
// Piece1 | Piece2 | Piece3 | ...
|
|
|
|
|
|
// [ Item1, Item2, ... ] | ...
|
2023-10-05 08:28:16 +08:00
|
|
|
|
type Cache[T any] struct {
|
2020-11-22 12:11:39 +08:00
|
|
|
|
isDestroyed bool
|
2023-10-05 08:28:16 +08:00
|
|
|
|
pieces []*Piece[T]
|
2020-11-21 20:44:19 +08:00
|
|
|
|
countPieces uint64
|
2020-11-21 21:43:03 +08:00
|
|
|
|
maxItems int
|
2020-11-21 20:44:19 +08:00
|
|
|
|
|
2023-10-12 16:03:52 +08:00
|
|
|
|
maxPiecesPerGC int
|
|
|
|
|
|
gcPieceIndex int
|
2020-11-21 20:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func NewBigCache[T any]() *Cache[T] {
|
2024-03-28 17:17:34 +08:00
|
|
|
|
var delta = memutils.SystemMemoryGB() / 2
|
2023-04-25 17:24:05 +08:00
|
|
|
|
if delta <= 0 {
|
|
|
|
|
|
delta = 1
|
|
|
|
|
|
}
|
2023-10-05 08:28:16 +08:00
|
|
|
|
return NewCache[T](NewMaxItemsOption(delta * 1_000_000))
|
2023-04-25 17:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func NewCache[T any](opt ...OptionInterface) *Cache[T] {
|
2022-04-09 18:28:22 +08:00
|
|
|
|
var countPieces = 256
|
2022-04-21 09:40:20 +08:00
|
|
|
|
var maxItems = 1_000_000
|
2021-10-04 09:12:17 +08:00
|
|
|
|
|
2024-03-28 17:17:34 +08:00
|
|
|
|
var totalMemory = memutils.SystemMemoryGB()
|
2021-12-22 16:43:16 +08:00
|
|
|
|
if totalMemory < 2 {
|
|
|
|
|
|
// 我们限制内存过小的服务能够使用的数量
|
2022-04-21 09:40:20 +08:00
|
|
|
|
maxItems = 500_000
|
2021-12-22 16:43:16 +08:00
|
|
|
|
} else {
|
2023-04-25 17:24:05 +08:00
|
|
|
|
var delta = totalMemory / 4
|
2021-12-22 16:43:16 +08:00
|
|
|
|
if delta > 0 {
|
|
|
|
|
|
maxItems *= delta
|
|
|
|
|
|
}
|
2021-10-04 09:12:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-21 20:44:19 +08:00
|
|
|
|
for _, option := range opt {
|
|
|
|
|
|
if option == nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
switch o := option.(type) {
|
|
|
|
|
|
case *PiecesOption:
|
|
|
|
|
|
if o.Count > 0 {
|
|
|
|
|
|
countPieces = o.Count
|
|
|
|
|
|
}
|
2020-11-21 21:43:03 +08:00
|
|
|
|
case *MaxItemsOption:
|
|
|
|
|
|
if o.Count > 0 {
|
|
|
|
|
|
maxItems = o.Count
|
|
|
|
|
|
}
|
2020-11-21 20:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-12 16:03:52 +08:00
|
|
|
|
var maxPiecesPerGC = 4
|
|
|
|
|
|
var numCPU = runtime.NumCPU() / 2
|
|
|
|
|
|
if numCPU > maxPiecesPerGC {
|
|
|
|
|
|
maxPiecesPerGC = numCPU
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
var cache = &Cache[T]{
|
2023-10-12 16:03:52 +08:00
|
|
|
|
countPieces: uint64(countPieces),
|
|
|
|
|
|
maxItems: maxItems,
|
|
|
|
|
|
maxPiecesPerGC: maxPiecesPerGC,
|
2020-11-21 20:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < countPieces; i++ {
|
2023-10-05 08:28:16 +08:00
|
|
|
|
cache.pieces = append(cache.pieces, NewPiece[T](maxItems/countPieces))
|
2020-11-21 20:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-08 15:17:45 +08:00
|
|
|
|
// Add to manager
|
|
|
|
|
|
SharedManager.Add(cache)
|
2020-11-21 20:44:19 +08:00
|
|
|
|
|
|
|
|
|
|
return cache
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func (this *Cache[T]) Write(key string, value T, expiredAt int64) (ok bool) {
|
2020-11-22 12:11:39 +08:00
|
|
|
|
if this.isDestroyed {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-08 12:47:04 +08:00
|
|
|
|
var currentTimestamp = fasttime.Now().Unix()
|
2020-11-21 20:44:19 +08:00
|
|
|
|
if expiredAt <= currentTimestamp {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-16 17:06:26 +08:00
|
|
|
|
var maxExpiredAt = currentTimestamp + 30*86400
|
2020-11-21 20:44:19 +08:00
|
|
|
|
if expiredAt > maxExpiredAt {
|
|
|
|
|
|
expiredAt = maxExpiredAt
|
|
|
|
|
|
}
|
2023-04-08 13:49:41 +08:00
|
|
|
|
var uint64Key = HashKey([]byte(key))
|
|
|
|
|
|
var pieceIndex = uint64Key % this.countPieces
|
2023-10-05 08:28:16 +08:00
|
|
|
|
return this.pieces[pieceIndex].Add(uint64Key, &Item[T]{
|
2020-11-21 21:43:03 +08:00
|
|
|
|
Value: value,
|
2020-11-21 20:44:19 +08:00
|
|
|
|
expiredAt: expiredAt,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func (this *Cache[T]) IncreaseInt64(key string, delta T, expiredAt int64, extend bool) T {
|
2020-11-22 12:11:39 +08:00
|
|
|
|
if this.isDestroyed {
|
2023-10-05 08:28:16 +08:00
|
|
|
|
return any(0).(T)
|
2020-11-22 12:11:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-08 13:49:41 +08:00
|
|
|
|
var currentTimestamp = fasttime.Now().Unix()
|
2020-11-22 12:11:39 +08:00
|
|
|
|
if expiredAt <= currentTimestamp {
|
2023-10-05 08:28:16 +08:00
|
|
|
|
return any(0).(T)
|
2020-11-22 12:11:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-08 13:49:41 +08:00
|
|
|
|
var maxExpiredAt = currentTimestamp + 30*86400
|
2020-11-22 12:11:39 +08:00
|
|
|
|
if expiredAt > maxExpiredAt {
|
|
|
|
|
|
expiredAt = maxExpiredAt
|
|
|
|
|
|
}
|
2023-04-08 13:49:41 +08:00
|
|
|
|
var uint64Key = HashKey([]byte(key))
|
|
|
|
|
|
var pieceIndex = uint64Key % this.countPieces
|
2022-05-12 21:48:33 +08:00
|
|
|
|
return this.pieces[pieceIndex].IncreaseInt64(uint64Key, delta, expiredAt, extend)
|
2020-11-22 12:11:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func (this *Cache[T]) Read(key string) (item *Item[T]) {
|
2023-04-08 13:49:41 +08:00
|
|
|
|
var uint64Key = HashKey([]byte(key))
|
2020-11-21 20:44:19 +08:00
|
|
|
|
return this.pieces[uint64Key%this.countPieces].Read(uint64Key)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func (this *Cache[T]) Delete(key string) {
|
2023-04-08 13:49:41 +08:00
|
|
|
|
var uint64Key = HashKey([]byte(key))
|
2020-11-21 20:44:19 +08:00
|
|
|
|
this.pieces[uint64Key%this.countPieces].Delete(uint64Key)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func (this *Cache[T]) Count() (count int) {
|
2020-11-21 20:44:19 +08:00
|
|
|
|
for _, piece := range this.pieces {
|
|
|
|
|
|
count += piece.Count()
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func (this *Cache[T]) GC() {
|
2023-04-25 19:10:37 +08:00
|
|
|
|
var index = this.gcPieceIndex
|
2023-10-12 16:03:52 +08:00
|
|
|
|
|
|
|
|
|
|
for i := index; i < index+this.maxPiecesPerGC; i++ {
|
2023-04-25 19:10:37 +08:00
|
|
|
|
if i >= int(this.countPieces) {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
this.pieces[i].GC()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-12 16:03:52 +08:00
|
|
|
|
index += this.maxPiecesPerGC
|
2023-04-25 19:10:37 +08:00
|
|
|
|
if index >= int(this.countPieces) {
|
|
|
|
|
|
index = 0
|
2020-11-21 20:44:19 +08:00
|
|
|
|
}
|
2023-04-25 19:10:37 +08:00
|
|
|
|
this.gcPieceIndex = index
|
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 *Cache[T]) Clean() {
|
2021-08-21 21:06:48 +08:00
|
|
|
|
for _, piece := range this.pieces {
|
|
|
|
|
|
piece.Clean()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-05 08:28:16 +08:00
|
|
|
|
func (this *Cache[T]) Destroy() {
|
2021-12-08 15:17:45 +08:00
|
|
|
|
SharedManager.Remove(this)
|
|
|
|
|
|
|
2020-11-22 12:11:39 +08:00
|
|
|
|
this.isDestroyed = true
|
|
|
|
|
|
|
|
|
|
|
|
for _, piece := range this.pieces {
|
|
|
|
|
|
piece.Destroy()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|