From 4cf47163c175736160e5b2a1f293c1e3820ce9a5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 5 Jul 2021 17:11:57 +0300 Subject: [PATCH] lib/workingsetcache: fix cache capacity calculations after 4f0003f182f6cd6e40277310a27ac4fdc921c522 --- lib/workingsetcache/cache.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/workingsetcache/cache.go b/lib/workingsetcache/cache.go index 790751d4b..6ad0c81d6 100644 --- a/lib/workingsetcache/cache.go +++ b/lib/workingsetcache/cache.go @@ -62,8 +62,9 @@ func Load(filePath string, maxBytes int, expireDuration time.Duration) *Cache { // The cache couldn't be loaded with maxBytes size. // This may mean that the cache is split into curr and prev caches. // Try loading it again with maxBytes / 2 size. + curr := fastcache.New(maxBytes / 2) prev := fastcache.LoadFromFileOrNew(filePath, maxBytes/2) - c := newCacheInternal(fastcache.New(1024), prev, maxBytes, split) + c := newCacheInternal(curr, prev, maxBytes, split) c.runWatchers(expireDuration) return c } @@ -71,7 +72,8 @@ func Load(filePath string, maxBytes int, expireDuration time.Duration) *Cache { // The cache has been successfully loaded in full. // Set its' mode to `whole`. // There is no need in runWatchers call. - return newCacheInternal(curr, fastcache.New(1024), maxBytes, whole) + prev := fastcache.New(1024) + return newCacheInternal(curr, prev, maxBytes, whole) } // New creates new cache with the given maxBytes capcity and the given expireDuration @@ -79,9 +81,9 @@ func Load(filePath string, maxBytes int, expireDuration time.Duration) *Cache { // // Stop must be called on the returned cache when it is no longer needed. func New(maxBytes int, expireDuration time.Duration) *Cache { - // Split maxBytes between curr and prev caches. - curr := fastcache.New(maxBytes) - c := newCacheInternal(curr, fastcache.New(1024), maxBytes, split) + curr := fastcache.New(maxBytes / 2) + prev := fastcache.New(1024) + c := newCacheInternal(curr, prev, maxBytes, split) c.runWatchers(expireDuration) return c }