From 00a49ab5774d43ee085450e16a2e5d6b6d54a978 Mon Sep 17 00:00:00 2001 From: wxing1292 Date: Wed, 12 Jan 2022 14:49:08 -0800 Subject: [PATCH] Remove removal function away from LRU cache for simplicity (#2372) * Remove element removal function since this functionality is not used & brings complexity --- common/cache/cache.go | 4 --- common/cache/lru.go | 5 --- common/cache/lru_test.go | 71 +++------------------------------------- 3 files changed, 4 insertions(+), 76 deletions(-) diff --git a/common/cache/cache.go b/common/cache/cache.go index 20a8c4c719c..77c51e82afe 100644 --- a/common/cache/cache.go +++ b/common/cache/cache.go @@ -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 diff --git a/common/cache/lru.go b/common/cache/lru.go index ce3134fc969..c87cefe372f 100644 --- a/common/cache/lru.go +++ b/common/cache/lru.go @@ -45,7 +45,6 @@ type ( maxSize int ttl time.Duration pin bool - rmFunc RemovedFunc } iteratorImpl struct { @@ -141,7 +140,6 @@ func New(maxSize int, opts *Options) Cache { ttl: opts.TTL, maxSize: maxSize, pin: opts.Pin, - rmFunc: opts.RemovedFunc, } } @@ -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) } diff --git a/common/cache/lru_test.go b/common/cache/lru_test.go index 7d0c4cf3415..4433bba23c5 100644 --- a/common/cache/lru_test.go +++ b/common/cache/lru_test.go @@ -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) @@ -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) @@ -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)