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

feat: Add RemoveWithoutEvict cache method #176

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 19 additions & 7 deletions expirable/expirable_lru.go
Expand Up @@ -192,7 +192,19 @@ func (c *LRU[K, V]) Remove(key K) bool {
c.mu.Lock()
defer c.mu.Unlock()
if ent, ok := c.items[key]; ok {
c.removeElement(ent)
c.removeElement(ent, true)
return true
}
return false
}

// RemoveWithoutEvict removes the provided key from the cache without calling
// the eviction callback.
func (c *LRU[K, V]) RemoveWithoutEvict(key K) bool {
c.mu.Lock()
defer c.mu.Unlock()
if ent, ok := c.items[key]; ok {
c.removeElement(ent, false)
return true
}
return false
Expand All @@ -203,7 +215,7 @@ func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool) {
c.mu.Lock()
defer c.mu.Unlock()
if ent := c.evictList.Back(); ent != nil {
c.removeElement(ent)
c.removeElement(ent, true)
return ent.Key, ent.Value, true
}
return
Expand Down Expand Up @@ -292,16 +304,16 @@ func (c *LRU[K, V]) Resize(size int) (evicted int) {
// removeOldest removes the oldest item from the cache. Has to be called with lock!
func (c *LRU[K, V]) removeOldest() {
if ent := c.evictList.Back(); ent != nil {
c.removeElement(ent)
c.removeElement(ent, true)
}
}

// removeElement is used to remove a given list element from the cache. Has to be called with lock!
func (c *LRU[K, V]) removeElement(e *internal.Entry[K, V]) {
func (c *LRU[K, V]) removeElement(e *internal.Entry[K, V], cb bool) {
c.evictList.Remove(e)
delete(c.items, e.Key)
c.removeFromBucket(e)
if c.onEvict != nil {
if cb && c.onEvict != nil {
c.onEvict(e.Key, e.Value)
}
}
Expand All @@ -319,7 +331,7 @@ func (c *LRU[K, V]) deleteExpired() {
c.mu.Lock()
}
for _, ent := range c.buckets[bucketIdx].entries {
c.removeElement(ent)
c.removeElement(ent, true)
}
c.nextCleanupBucket = (c.nextCleanupBucket + 1) % numBuckets
c.mu.Unlock()
Expand All @@ -343,4 +355,4 @@ func (c *LRU[K, V]) removeFromBucket(e *internal.Entry[K, V]) {
// Cap returns the capacity of the cache
func (c *LRU[K, V]) Cap() int {
return c.size
}
}
9 changes: 9 additions & 0 deletions lru.go
Expand Up @@ -181,6 +181,15 @@ func (c *Cache[K, V]) Remove(key K) (present bool) {
return
}

// RemoveWithoutEvict removes the provided key from the cache without calling
// the eviction callback.
func (c *Cache[K, V]) RemoveWithoutEvict(key K) (present bool) {
c.lock.Lock()
present = c.lru.RemoveWithoutEvict(key)
c.lock.Unlock()
return
}

// Resize changes the cache size.
func (c *Cache[K, V]) Resize(size int) (evicted int) {
var ks []K
Expand Down
32 changes: 32 additions & 0 deletions lru_test.go
Expand Up @@ -444,3 +444,35 @@ func TestCache_EvictionSameKey(t *testing.T) {
}
})
}

func TestLRURemoveWithoutEvict(t *testing.T) {
var (
called bool
evictedKeys []int
)

cache, _ := NewWithEvict(2, func(key int, _ struct{}) {
called = true
evictedKeys = append(evictedKeys, key)
})

cache.Add(1, struct{}{})
cache.Add(2, struct{}{})

cache.Remove(1)
if !called {
t.Error("eviction wasn't called")
}

called = false

cache.RemoveWithoutEvict(2)
if called {
t.Error("eviction was called")
}

want := []int{1}
if !reflect.DeepEqual(evictedKeys, want) {
t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want)
}
}
18 changes: 13 additions & 5 deletions simplelru/lru.go
Expand Up @@ -97,7 +97,15 @@ func (c *LRU[K, V]) Peek(key K) (value V, ok bool) {
// key was contained.
func (c *LRU[K, V]) Remove(key K) (present bool) {
if ent, ok := c.items[key]; ok {
c.removeElement(ent)
c.removeElement(ent, true)
return true
}
return false
}

func (c *LRU[K, V]) RemoveWithoutEvict(key K) (present bool) {
if ent, ok := c.items[key]; ok {
c.removeElement(ent, false)
return true
}
return false
Expand All @@ -106,7 +114,7 @@ func (c *LRU[K, V]) Remove(key K) (present bool) {
// RemoveOldest removes the oldest item from the cache.
func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool) {
if ent := c.evictList.Back(); ent != nil {
c.removeElement(ent)
c.removeElement(ent, true)
return ent.Key, ent.Value, true
}
return
Expand Down Expand Up @@ -168,15 +176,15 @@ func (c *LRU[K, V]) Resize(size int) (evicted int) {
// removeOldest removes the oldest item from the cache.
func (c *LRU[K, V]) removeOldest() {
if ent := c.evictList.Back(); ent != nil {
c.removeElement(ent)
c.removeElement(ent, true)
}
}

// removeElement is used to remove a given list element from the cache
func (c *LRU[K, V]) removeElement(e *internal.Entry[K, V]) {
func (c *LRU[K, V]) removeElement(e *internal.Entry[K, V], cb bool) {
c.evictList.Remove(e)
delete(c.items, e.Key)
if c.onEvict != nil {
if cb && c.onEvict != nil {
c.onEvict(e.Key, e.Value)
}
}
3 changes: 3 additions & 0 deletions simplelru/lru_interface.go
Expand Up @@ -23,6 +23,9 @@ type LRUCache[K comparable, V any] interface {
// Removes a key from the cache.
Remove(key K) bool

// Removes a key from the cache without calling the eviction callback.
RemoveWithoutEvict(key K) bool

// Removes the oldest entry from cache.
RemoveOldest() (K, V, bool)

Expand Down