Files
EdgeNode/internal/utils/ttlcache/cache.go

178 lines
3.6 KiB
Go
Raw Normal View History

2020-11-21 21:43:03 +08:00
package ttlcache
import (
"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"
)
2023-10-05 08:28:16 +08:00
var SharedInt64Cache = NewBigCache[int64]()
2022-01-10 19:54:10 +08:00
// Cache TTL缓存
// 最大的缓存时间为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]
countPieces uint64
2020-11-21 21:43:03 +08:00
maxItems int
2023-10-12 16:03:52 +08:00
maxPiecesPerGC int
gcPieceIndex int
}
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
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
}
}
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
}
}
}
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,
}
for i := 0; i < countPieces; i++ {
2023-10-05 08:28:16 +08:00
cache.pieces = append(cache.pieces, NewPiece[T](maxItems/countPieces))
}
// Add to manager
SharedManager.Add(cache)
return cache
}
2024-05-08 11:10:56 +08:00
func (this *Cache[T]) Write(key string, value T, expiresAt int64) (ok bool) {
2020-11-22 12:11:39 +08:00
if this.isDestroyed {
return
}
var currentTimestamp = fasttime.Now().Unix()
2024-05-08 11:10:56 +08:00
if expiresAt <= currentTimestamp {
return
}
2024-05-08 11:10:56 +08:00
var maxExpiresAt = currentTimestamp + 30*86400
if expiresAt > maxExpiresAt {
expiresAt = maxExpiresAt
}
2024-04-18 18:25:33 +08:00
var uint64Key = HashKeyString(key)
2023-04-08 13:49:41 +08:00
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,
2024-05-08 11:10:56 +08:00
expiresAt: expiresAt,
})
}
2024-05-08 11:10:56 +08:00
func (this *Cache[T]) IncreaseInt64(key string, delta T, expiresAt 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()
2024-05-08 11:10:56 +08:00
if expiresAt <= currentTimestamp {
2023-10-05 08:28:16 +08:00
return any(0).(T)
2020-11-22 12:11:39 +08:00
}
2024-05-08 11:10:56 +08:00
var maxExpiresAt = currentTimestamp + 30*86400
if expiresAt > maxExpiresAt {
expiresAt = maxExpiresAt
2020-11-22 12:11:39 +08:00
}
2024-04-18 18:25:33 +08:00
var uint64Key = HashKeyString(key)
2023-04-08 13:49:41 +08:00
var pieceIndex = uint64Key % this.countPieces
2024-05-08 11:10:56 +08:00
return this.pieces[pieceIndex].IncreaseInt64(uint64Key, delta, expiresAt, 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]) {
2024-04-18 18:25:33 +08:00
var uint64Key = HashKeyString(key)
return this.pieces[uint64Key%this.countPieces].Read(uint64Key)
}
2023-10-05 08:28:16 +08:00
func (this *Cache[T]) Delete(key string) {
2024-04-18 18:25:33 +08:00
var uint64Key = HashKeyString(key)
this.pieces[uint64Key%this.countPieces].Delete(uint64Key)
}
2023-10-05 08:28:16 +08:00
func (this *Cache[T]) Count() (count int) {
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
}
2023-04-25 19:10:37 +08:00
this.gcPieceIndex = index
}
2020-11-22 12:11:39 +08:00
2023-10-05 08:28:16 +08:00
func (this *Cache[T]) Clean() {
for _, piece := range this.pieces {
piece.Clean()
}
}
2023-10-05 08:28:16 +08:00
func (this *Cache[T]) Destroy() {
SharedManager.Remove(this)
2020-11-22 12:11:39 +08:00
this.isDestroyed = true
for _, piece := range this.pieces {
piece.Destroy()
}
}