根据系统内存自动调节ttlcache的最大条目

This commit is contained in:
刘祥超
2021-10-04 09:12:17 +08:00
parent 1ca967534a
commit 3888565c0f
6 changed files with 55 additions and 4 deletions

View File

@@ -24,7 +24,13 @@ type Cache struct {
func NewCache(opt ...OptionInterface) *Cache {
countPieces := 128
maxItems := 10_000_000
maxItems := 2_000_000
var delta = systemMemoryGB() / 4
if delta > 0 {
maxItems *= delta
}
for _, option := range opt {
if option == nil {
continue
@@ -61,7 +67,7 @@ func NewCache(opt ...OptionInterface) *Cache {
return cache
}
func (this *Cache) Write(key string, value interface{}, expiredAt int64) {
func (this *Cache) Write(key string, value interface{}, expiredAt int64) (ok bool) {
if this.isDestroyed {
return
}
@@ -77,7 +83,7 @@ func (this *Cache) Write(key string, value interface{}, expiredAt int64) {
}
uint64Key := HashKey([]byte(key))
pieceIndex := uint64Key % this.countPieces
this.pieces[pieceIndex].Add(uint64Key, &Item{
return this.pieces[pieceIndex].Add(uint64Key, &Item{
Value: value,
expiredAt: expiredAt,
})