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

Refactor DeleteExpired for safer behavior #60

Open
wants to merge 5 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,31 +241,24 @@ func (c *Cache[K, V]) GetOrSet(key K, val V, opts ...ItemOption) (actual V, load

// DeleteExpired all expired items from the cache.
func (c *Cache[K, V]) DeleteExpired() {
c.mu.Lock()
l := c.expManager.len()
c.mu.Unlock()

evict := func() bool {
key := c.expManager.pop()
// if is expired, delete it and return nil instead
item, ok := c.cache.Get(key)
if ok {
if item.Expired() {
c.cache.Delete(key)
return false
}
c.expManager.update(key, item.Expiration)
c.mu.Lock()
defer c.mu.Unlock()
if c.expManager.len() == 0 {
return false
}
key, expiration := c.expManager.pop()
if nowFunc().After(expiration) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better to use expired than this because a Expired method already has checking expiration logic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was meant to avoid an extra cache.Get(). Also, if the cache already had deleted the item, Expired() was not callable. However, your approach is more maintainable, since if you edit the way Expired() works you need to edit this code as well. So what should I do about this change ?

c.cache.Delete(key)
return true
}
return true
if _, ok := c.cache.Get(key); ok {
c.expManager.update(key, expiration)
}
return false
}

for i := 0; i < l; i++ {
c.mu.Lock()
shouldBreak := evict()
c.mu.Unlock()
if shouldBreak {
break
}
for evict() {
}
}

Expand Down
37 changes: 37 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cache_test

import (
"math/rand"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -121,3 +123,38 @@ func TestCallJanitor(t *testing.T) {
t.Errorf("want items is empty but got %d", len(keys))
}
}

func TestConcurrentDelete(t *testing.T) {
c := cache.New[string, int]()
var (
wg sync.WaitGroup
stop atomic.Bool
timeout = 10 * time.Second
)

if testing.Short() {
timeout = 100 * time.Millisecond
}
time.AfterFunc(timeout, func() {
stop.Store(true)
})

wg.Add(1)
go func() {
defer wg.Done()
for k := 1; !stop.Load(); k++ {
c.Set(strconv.Itoa(k), k, cache.WithExpiration(0))
c.Delete(strconv.Itoa(k))
}
}()

wg.Add(1)
go func() {
defer wg.Done()
for !stop.Load() {
c.DeleteExpired()
}
}()

wg.Wait()
}
5 changes: 3 additions & 2 deletions expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ func (m *expirationManager[K]) len() int {
return m.queue.Len()
}

func (m *expirationManager[K]) pop() K {
func (m *expirationManager[K]) pop() (K, time.Time) {
v := heap.Pop(&m.queue)
key := v.(*expirationKey[K]).key
exp := v.(*expirationKey[K]).expiration
delete(m.mapping, key)
return key
return key, exp
}

func (m *expirationManager[K]) remove(key K) {
Expand Down