Skip to content

Commit

Permalink
fix evacuate count
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood committed Jul 6, 2020
1 parent ebbf0c4 commit 3740491
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"crypto/rand"
"encoding/binary"
"fmt"
"log"
mrand "math/rand"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3740491

Please sign in to comment.