Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added GetOrAdd to expirable LRU #155

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions expirable/expirable_lru.go
Expand Up @@ -120,6 +120,10 @@ func (c *LRU[K, V]) Purge() {
func (c *LRU[K, V]) Add(key K, value V) (evicted bool) {
c.mu.Lock()
defer c.mu.Unlock()
return c.addWithLock(key, value)
}

func (c *LRU[K, V]) addWithLock(key K, value V) (evicted bool) {
now := time.Now()

// Check for existing item
Expand Down Expand Up @@ -149,6 +153,11 @@ func (c *LRU[K, V]) Add(key K, value V) (evicted bool) {
func (c *LRU[K, V]) Get(key K) (value V, ok bool) {
c.mu.Lock()
defer c.mu.Unlock()
return c.getWithLock(key)
}

// Get looks up a key's value from the cache.
func (c *LRU[K, V]) getWithLock(key K) (value V, ok bool) {
var ent *internal.Entry[K, V]
if ent, ok = c.items[key]; ok {
// Expired item check
Expand All @@ -161,6 +170,22 @@ func (c *LRU[K, V]) Get(key K) (value V, ok bool) {
return
}

func (c *LRU[K, V]) GetOrAdd(key K, new func() V) (value V, added bool, evicted bool) {
c.mu.Lock()
defer c.mu.Unlock()

if existingValue, exists := c.getWithLock(key); exists {
return existingValue, added, evicted
} else {
if new != nil {
value = new()
}
added = true
evicted = c.addWithLock(key, value)
return value, added, evicted
}
}

// Contains checks if a key is in the cache, without updating the recent-ness
// or deleting it for being stale.
func (c *LRU[K, V]) Contains(key K) (ok bool) {
Expand Down
31 changes: 31 additions & 0 deletions expirable/expirable_lru_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"math/big"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -335,6 +336,36 @@ func TestLRUConcurrency(t *testing.T) {
}
}

func TestLRUGetOrAddConcurrency(t *testing.T) {
lc := NewLRU[string, string](0, nil, 0)
wg := sync.WaitGroup{}
wg.Add(1000)
var evictedCount int32
var addedCount int32
for i := 0; i < 1000; i++ {
go func(i int) {
_, added, evicted := lc.GetOrAdd(fmt.Sprintf("key-%d", i/10), func() string { return fmt.Sprintf("val-%d", i/10) })
if evicted {
atomic.AddInt32(&evictedCount, 1)
}
if added {
atomic.AddInt32(&addedCount, 1)
}
wg.Done()
}(i)
}
wg.Wait()
if lc.Len() != 100 {
t.Fatalf("length differs from expected")
}
if addedCount != 100 {
t.Fatalf("GetOrAdd: unexpected added count %d", addedCount)
}
if evictedCount > 0 {
t.Fatalf("GetOrAdd: unexpected evicted count %d", evictedCount)
}
}

func TestLRUInvalidateAndEvict(t *testing.T) {
var evicted int
lc := NewLRU(-1, func(_, _ string) { evicted++ }, 0)
Expand Down