Skip to content

Commit

Permalink
Remove removal function away from LRU cache for simplicity (#2372)
Browse files Browse the repository at this point in the history
* Remove element removal function since this functionality is not used & brings complexity
  • Loading branch information
wxing1292 authored Jan 12, 2022
1 parent 434ec0e commit 00a49ab
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 76 deletions.
4 changes: 0 additions & 4 deletions common/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ type Options struct {

// Pin prevents in-use objects from getting evicted.
Pin bool

// RemovedFunc is an optional function called when an element
// is scheduled for deletion
RemovedFunc RemovedFunc
}

// SimpleOptions provides options that can be used to configure SimpleCache
Expand Down
5 changes: 0 additions & 5 deletions common/cache/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type (
maxSize int
ttl time.Duration
pin bool
rmFunc RemovedFunc
}

iteratorImpl struct {
Expand Down Expand Up @@ -141,7 +140,6 @@ func New(maxSize int, opts *Options) Cache {
ttl: opts.TTL,
maxSize: maxSize,
pin: opts.Pin,
rmFunc: opts.RemovedFunc,
}
}

Expand Down Expand Up @@ -308,9 +306,6 @@ func (c *lru) putInternal(key interface{}, value interface{}, allowUpdate bool)

func (c *lru) deleteInternal(element *list.Element) {
entry := c.byAccess.Remove(element).(*entryImpl)
if c.rmFunc != nil {
go c.rmFunc(entry.value)
}
delete(c.byKey, entry.key)
}

Expand Down
71 changes: 4 additions & 67 deletions common/cache/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,64 +156,21 @@ func TestLRUCacheConcurrentAccess(t *testing.T) {
wg.Wait()
}

func TestRemoveFunc(t *testing.T) {
ch := make(chan bool)
cache := New(5, &Options{
RemovedFunc: func(i interface{}) {
_, ok := i.(*testing.T)
assert.True(t, ok)
ch <- true
},
})

cache.Put("testing", t)
cache.Delete("testing")
assert.Nil(t, cache.Get("testing"))

timeout := time.NewTimer(time.Millisecond * 300)
select {
case b := <-ch:
assert.True(t, b)
case <-timeout.C:
t.Error("RemovedFunc did not send true on channel ch")
}
}

func TestRemovedFuncWithTTL(t *testing.T) {
ch := make(chan bool)
func TestTTL(t *testing.T) {
cache := New(5, &Options{
TTL: time.Millisecond * 50,
RemovedFunc: func(i interface{}) {
_, ok := i.(*testing.T)
assert.True(t, ok)
ch <- true
},
})

cache.Put("A", t)
assert.Equal(t, t, cache.Get("A"))
time.Sleep(time.Millisecond * 100)
assert.Nil(t, cache.Get("A"))

timeout := time.NewTimer(time.Millisecond * 300)
select {
case b := <-ch:
assert.True(t, b)
case <-timeout.C:
t.Error("RemovedFunc did not send true on channel ch")
}
}

func TestRemovedFuncWithTTL_Pin(t *testing.T) {
ch := make(chan bool)
func TestTTLWithPin(t *testing.T) {
cache := New(5, &Options{
TTL: time.Millisecond * 50,
Pin: true,
RemovedFunc: func(i interface{}) {
_, ok := i.(*testing.T)
assert.True(t, ok)
ch <- true
},
})

_, err := cache.PutIfNotExist("A", t)
Expand All @@ -226,26 +183,12 @@ func TestRemovedFuncWithTTL_Pin(t *testing.T) {
cache.Release("A")
cache.Release("A")
assert.Nil(t, cache.Get("A"))

timeout := time.NewTimer(time.Millisecond * 300)
select {
case b := <-ch:
assert.True(t, b)
case <-timeout.C:
t.Error("RemovedFunc did not send true on channel ch")
}
}

func TestRemovedFuncMaxSize_Pin_MidItem(t *testing.T) {
ch := make(chan bool)
func TestMaxSizeWithPin_MidItem(t *testing.T) {
cache := New(2, &Options{
TTL: time.Millisecond * 50,
Pin: true,
RemovedFunc: func(i interface{}) {
_, ok := i.(*testing.T)
assert.True(t, ok)
ch <- true
},
})

_, err := cache.PutIfNotExist("A", t)
Expand Down Expand Up @@ -277,16 +220,10 @@ func TestRemovedFuncMaxSize_Pin_MidItem(t *testing.T) {
assert.Nil(t, cache.Get("C"))
}

func TestRemovedFuncMaxSize_Pin_LastItem(t *testing.T) {
ch := make(chan bool)
func TestMaxSizeWithPin_LastItem(t *testing.T) {
cache := New(2, &Options{
TTL: time.Millisecond * 50,
Pin: true,
RemovedFunc: func(i interface{}) {
_, ok := i.(*testing.T)
assert.True(t, ok)
ch <- true
},
})

_, err := cache.PutIfNotExist("A", t)
Expand Down

0 comments on commit 00a49ab

Please sign in to comment.