diff --git a/internal/store.go b/internal/store.go index 02065cf..38cd012 100644 --- a/internal/store.go +++ b/internal/store.go @@ -19,7 +19,6 @@ import ( ) const ( - MAX_READ_BUFF_SIZE = 64 MIN_WRITE_BUFF_SIZE = 4 MAX_WRITE_BUFF_SIZE = 1024 ) @@ -225,10 +224,13 @@ func (s *Store[K, V]) getFromShard(key K, hash uint64, shard *Shard[K, V]) (V, b expire := entry.expire.Load() if expire != 0 && expire <= s.timerwheel.clock.NowNano() { ok = false + s.policy.miss.Add(1) } else { s.policy.hit.Add(1) value = entry.value } + } else { + s.policy.miss.Add(1) } shard.mu.RUnlock(tk) @@ -542,7 +544,6 @@ func (s *Store[K, V]) removeEntry(entry *Entry[K, V], reason RemoveReason) { } func (s *Store[K, V]) drainRead(buffer []ReadBufItem[K, V]) { - s.policy.total.Add(MAX_READ_BUFF_SIZE) s.mlock.Lock() for _, e := range buffer { s.policy.Access(e) diff --git a/internal/store_test.go b/internal/store_test.go index 4cf072f..ae1fdfb 100644 --- a/internal/store_test.go +++ b/internal/store_test.go @@ -114,3 +114,21 @@ func TestDoorKeeperDynamicSize(t *testing.T) { } require.True(t, shard.dookeeper.Capacity > 100000) } + +func TestPolicyCounter(t *testing.T) { + store := NewStore[int, int](1000, false, nil, nil, nil, 0, 0, nil) + for i := 0; i < 1000; i++ { + store.Set(i, i, 1, 0) + } + // hit + for i := 0; i < 1600; i++ { + store.Get(100) + } + // miss + for i := 0; i < 1600; i++ { + store.Get(10000) + } + + require.Equal(t, int64(1600), store.policy.hit.Value()) + require.Equal(t, int64(1600), store.policy.miss.Value()) +} diff --git a/internal/tlfu.go b/internal/tlfu.go index acc1e8c..93e1768 100644 --- a/internal/tlfu.go +++ b/internal/tlfu.go @@ -10,7 +10,7 @@ type TinyLfu[K comparable, V any] struct { hasher *Hasher[K] size uint counter uint - total *Counter + miss *Counter hit *Counter hr float32 threshold atomic.Int32 @@ -25,7 +25,7 @@ func NewTinyLfu[K comparable, V any](size uint, hasher *Hasher[K]) *TinyLfu[K, V sketch: NewCountMinSketch(), step: 1, hasher: hasher, - total: NewCounter(), + miss: NewCounter(), hit: NewCounter(), } // default threshold to -1 so all entries are admitted until cache is full @@ -34,9 +34,9 @@ func NewTinyLfu[K comparable, V any](size uint, hasher *Hasher[K]) *TinyLfu[K, V } func (t *TinyLfu[K, V]) climb() { - total := t.total.Value() + miss := t.miss.Value() hit := t.hit.Value() - current := float32(hit) / float32(total) + current := float32(hit) / float32(hit+miss) delta := current - t.hr var diff int8 if delta > 0.0 { @@ -77,7 +77,7 @@ func (t *TinyLfu[K, V]) climb() { t.threshold.Add(-int32(diff)) t.hr = current t.hit.Reset() - t.total.Reset() + t.miss.Reset() } func (t *TinyLfu[K, V]) Set(entry *Entry[K, V]) *Entry[K, V] {