From 374049170aa0d9266844d56be622f1949bb480c3 Mon Sep 17 00:00:00 2001 From: Evan Zhou Date: Mon, 6 Jul 2020 09:42:50 +0800 Subject: [PATCH] fix evacuate count --- cache_test.go | 25 +++++++++++++++++++++++++ segment.go | 2 ++ 2 files changed, 27 insertions(+) diff --git a/cache_test.go b/cache_test.go index 782dd8a..0651efa 100644 --- a/cache_test.go +++ b/cache_test.go @@ -5,7 +5,9 @@ import ( "crypto/rand" "encoding/binary" "fmt" + "log" mrand "math/rand" + "strconv" "strings" "sync" "testing" @@ -588,6 +590,29 @@ func TestConcurrentSet(t *testing.T) { } } +func TestEvacuateCount(t *testing.T) { + cache := NewCache(1024 * 1024) + n := 100000 + for i := 0; i < n; i++ { + err := cache.Set([]byte(strconv.Itoa(i)), []byte("A"), 0) + if err != nil { + log.Fatal(err) + } + } + missingItems := 0 + for i := 0; i < n; i++ { + res, err := cache.Get([]byte(strconv.Itoa(i))) + if err == ErrNotFound || (err == nil && string(res) != "A") { + missingItems++ + } else if err != nil { + log.Fatal(err) + } + } + if cache.EntryCount()+cache.EvacuateCount() != int64(n) { + t.Fatal(cache.EvacuateCount(), cache.EvacuateCount()) + } +} + func BenchmarkCacheSet(b *testing.B) { cache := NewCache(256 * 1024 * 1024) var key [8]byte diff --git a/segment.go b/segment.go index df37c1d..7a74a47 100644 --- a/segment.go +++ b/segment.go @@ -236,6 +236,8 @@ func (seg *segment) evacuate(entryLen int64, slotId uint8, now uint32) (slotModi seg.vacuumLen += oldEntryLen if expired { atomic.AddInt64(&seg.totalExpired, 1) + } else { + atomic.AddInt64(&seg.totalEvacuate, 1) } } else { // evacuate an old entry that has been accessed recently for better cache hit rate.