Skip to content

Commit

Permalink
Narrowed full locking in Get
Browse files Browse the repository at this point in the history
  • Loading branch information
toschnabel committed Nov 19, 2024
1 parent 3c8cb45 commit b8d0b93
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
27 changes: 16 additions & 11 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"math/rand"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -107,9 +108,9 @@ type Cache struct {

// Cache statistics
sets int64
gets int64
gets atomic.Int64
hits int64
misses int64
misses atomic.Int64
evictions int64

items map[interface{}]*list.Element
Expand Down Expand Up @@ -227,13 +228,17 @@ func (cache *Cache) Set(key, value interface{}) bool {
// not the value was found. The OnExpiration callback is invoked if the value
// had expired on access
func (cache *Cache) Get(key interface{}) (interface{}, bool) {
cache.mutex.Lock()
defer cache.mutex.Unlock()

cache.gets++

if element, ok := cache.items[key]; ok {
cache.gets.Add(1)
cache.mutex.RLock()
element, ok := cache.items[key]
cache.mutex.RUnlock()
if ok {
entry := element.Value.(*cacheEntry)

cache.mutex.Lock()
defer cache.mutex.Unlock()

if cache.maxAge == 0 || time.Since(entry.timestamp) <= cache.maxAge {
cache.evictionList.MoveToFront(element)
cache.hits++
Expand All @@ -242,14 +247,14 @@ func (cache *Cache) Get(key interface{}) (interface{}, bool) {

// Entry expired
cache.deleteElement(element)
cache.misses++
cache.misses.Add(1)
if cache.onExpiration != nil {
cache.onExpiration(entry.key, entry.value)
}
return nil, false
}

cache.misses++
cache.misses.Add(1)
return nil, false
}

Expand Down Expand Up @@ -447,9 +452,9 @@ func (cache *Cache) Stats() Stats {
Capacity: int64(cache.capacity),
Count: int64(cache.evictionList.Len()),
Sets: cache.sets,
Gets: cache.gets,
Gets: cache.gets.Load(),
Hits: cache.hits,
Misses: cache.misses,
Misses: cache.misses.Load(),
Evictions: cache.evictions,
}
}
Expand Down
25 changes: 24 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agecache

import (
"sort"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -559,7 +560,7 @@ func TestStats(t *testing.T) {
})
}

func BenchmarkCache(b *testing.B) {
func BenchmarkCache_EvenTraffic(b *testing.B) {
cache := New(Config{Capacity: 100, MaxAge: time.Second})

b.RunParallel(func(pb *testing.PB) {
Expand All @@ -569,3 +570,25 @@ func BenchmarkCache(b *testing.B) {
}
})
}

func BenchmarkCache_OnlyHits(b *testing.B) {
cache := New(Config{Capacity: 100, MaxAge: time.Second})

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
cache.Get("a")
}
})
}

func BenchmarkCache_NeverHits(b *testing.B) {
cache := New(Config{Capacity: 100, MaxAge: time.Second})
k := atomic.Int64{}

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k.Add(1)
cache.Get(k)
}
})
}

0 comments on commit b8d0b93

Please sign in to comment.