优化ttlcache

This commit is contained in:
GoEdgeLab
2022-04-09 18:44:51 +08:00
parent bad1e42ff6
commit c0696770f7
4 changed files with 15 additions and 2 deletions

View File

@@ -54,7 +54,7 @@ func NewCache(opt ...OptionInterface) *Cache {
}
}
cache := &Cache{
var cache = &Cache{
countPieces: uint64(countPieces),
maxItems: maxItems,
}

View File

@@ -165,6 +165,13 @@ func TestCache_GC2(t *testing.T) {
}
}
func TestCacheDestroy(t *testing.T) {
var cache = NewCache()
t.Log("count:", SharedManager.Count())
cache.Destroy()
t.Log("count:", SharedManager.Count())
}
func BenchmarkNewCache(b *testing.B) {
runtime.GOMAXPROCS(1)

View File

@@ -52,3 +52,9 @@ func (this *Manager) Remove(cache *Cache) {
delete(this.cacheMap, cache)
this.locker.Unlock()
}
func (this *Manager) Count() int {
this.locker.Lock()
defer this.locker.Unlock()
return len(this.cacheMap)
}