From bb15979372e6e06c93b415c0d5c06fc1b02408a4 Mon Sep 17 00:00:00 2001 From: sanad haj yahya Date: Thu, 9 Mar 2023 02:04:22 +0200 Subject: [PATCH] Revert "feat: add cache front/back method to get the first/last element" This reverts commit e8cdb07c3f418509e8fffe25b257cc3e260405f1. --- arc/arc.go | 14 -------------- arc/arc_test.go | 3 --- cache.go | 30 +----------------------------- fifo/fifo.go | 14 -------------- fifo/fifo_test.go | 7 ++----- idle/idle.go | 2 -- internal/cache.go | 26 -------------------------- lfu/lfu.go | 14 -------------- lfu/lfu_test.go | 4 ---- lifo/lifo.go | 14 -------------- lifo/lifo_test.go | 8 ++------ lru/lru.go | 14 -------------- lru/lru_test.go | 8 ++------ mru/mru.go | 14 -------------- mru/mru_test.go | 7 ++----- 15 files changed, 9 insertions(+), 170 deletions(-) diff --git a/arc/arc.go b/arc/arc.go index f5a7fae..1e13589 100644 --- a/arc/arc.go +++ b/arc/arc.go @@ -32,20 +32,6 @@ type arc struct { b2 *internal.Cache } -func (a *arc) Front() interface{} { - if k := a.t1.Front(); k != nil { - return k - } - return a.t2.Front() -} - -func (a *arc) Back() interface{} { - if k := a.t1.Back(); k != nil { - return k - } - return a.t2.Back() -} - func (a *arc) Load(key interface{}) (value interface{}, ok bool) { if val, ok := a.t1.Peek(key); ok { exp, _ := a.t1.Expiry(key) diff --git a/arc/arc_test.go b/arc/arc_test.go index d631daa..705b558 100644 --- a/arc/arc_test.go +++ b/arc/arc_test.go @@ -53,11 +53,8 @@ func TestARCc(t *testing.T) { a.Store(1, 1) a.Load(1) - assert.Equal(t, 0, a.t1.Len()) assert.Equal(t, 1, a.t2.Len()) - assert.Equal(t, 1, a.Front()) - assert.Equal(t, 1, a.Back()) a.Delete(1) } diff --git a/cache.go b/cache.go index 18ac402..939643e 100644 --- a/cache.go +++ b/cache.go @@ -24,20 +24,6 @@ type Event = internal.Event // Cache stores data so that future requests for that data can be served faster. type Cache interface { - // Front returns the first key of cache or nil if the cache is empty. - // - // # Experimental - // - // Notice: This func is EXPERIMENTAL and may be changed or removed in a - // later release. - Front() interface{} - // Back returns the last key of cache or nil if the cache is empty. - // - // # Experimental - // - // Notice: This func is EXPERIMENTAL and may be changed or removed in a - // later release. - Back() interface{} // Load returns key value. Load(key interface{}) (interface{}, bool) // Peek returns key value without updating the underlying "recent-ness". @@ -105,7 +91,7 @@ type Cache interface { // GC is a long running function, it returns when ctx done, therefore the // caller must start it in its own goroutine. // -// # Experimental +// Experimental // // Notice: This func is EXPERIMENTAL and may be changed or removed in a // later release. @@ -156,20 +142,6 @@ type cache struct { unsafe Cache } -func (c *cache) Front() interface{} { - c.mu.Lock() - k := c.unsafe.Front() - c.mu.Unlock() - return k -} - -func (c *cache) Back() interface{} { - c.mu.Lock() - k := c.unsafe.Back() - c.mu.Unlock() - return k -} - func (c *cache) Load(key interface{}) (interface{}, bool) { c.mu.Lock() v, ok := c.unsafe.Load(key) diff --git a/fifo/fifo.go b/fifo/fifo.go index b643eab..de10411 100644 --- a/fifo/fifo.go +++ b/fifo/fifo.go @@ -42,20 +42,6 @@ func (c *collection) Discard() (e *internal.Entry) { return } -func (c *collection) Front() (e *internal.Entry) { - if le := c.ll.Front(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - -func (c *collection) Back() (e *internal.Entry) { - if le := c.ll.Back(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - func (c *collection) Len() int { return c.ll.Len() } diff --git a/fifo/fifo_test.go b/fifo/fifo_test.go index ed87a35..14645d4 100644 --- a/fifo/fifo_test.go +++ b/fifo/fifo_test.go @@ -28,14 +28,11 @@ func TestCollection(t *testing.T) { } } - front := c.Front() - back := c.Back() oldest := c.Discard() c.Remove(entries[2]) + back := c.ll.Back().Value.(*internal.Entry) - assert.Equal(t, 1, front.Key) - assert.Equal(t, 3, back.Key) assert.Equal(t, 1, oldest.Key) assert.Equal(t, 1, c.Len()) - assert.Equal(t, 2, c.Back().Key) + assert.Equal(t, 2, back.Key) } diff --git a/idle/idle.go b/idle/idle.go index c73e48f..9dad64f 100644 --- a/idle/idle.go +++ b/idle/idle.go @@ -18,8 +18,6 @@ func New(cap int) libcache.Cache { type idle struct{} -func (idle) Front() (v interface{}) { return } -func (idle) Back() (v interface{}) { return } func (idle) Load(interface{}) (v interface{}, ok bool) { return } func (idle) Peek(interface{}) (v interface{}, ok bool) { return } func (idle) Keys() (keys []interface{}) { return } diff --git a/internal/cache.go b/internal/cache.go index dd93aad..f5d36e2 100644 --- a/internal/cache.go +++ b/internal/cache.go @@ -53,8 +53,6 @@ type Collection interface { Add(*Entry) Remove(*Entry) Discard() *Entry - Front() *Entry - Back() *Entry Len() int Init() } @@ -99,30 +97,6 @@ type Cache struct { capacity int } -// Front returns the first key of cache or nil if the cache is empty. -func (c *Cache) Front() interface{} { - // Run GC inline before get the front entry. - c.GC() - - if e := c.coll.Front(); e != nil { - return e.Key - } - - return nil -} - -// Back returns the last key of cache or nil if the cache is empty. -func (c *Cache) Back() interface{} { - // Run GC inline before get the back entry. - c.GC() - - if e := c.coll.Back(); e != nil { - return e.Key - } - - return nil -} - // Load returns key value. func (c *Cache) Load(key interface{}) (interface{}, bool) { return c.get(key, false) diff --git a/lfu/lfu.go b/lfu/lfu.go index b1a0008..a8f4f68 100644 --- a/lfu/lfu.go +++ b/lfu/lfu.go @@ -57,20 +57,6 @@ func (f *collection) Discard() (e *internal.Entry) { return heap.Pop(f).(*element).value } -func (f *collection) Front() (e *internal.Entry) { - if f.Len() > 0 { - e = (*f)[f.Len()-1].value - } - return -} - -func (f *collection) Back() (e *internal.Entry) { - if f.Len() > 0 { - e = (*f)[0].value - } - return -} - func (f *collection) Move(e *internal.Entry) { ele := e.Element.(*element) ele.count++ diff --git a/lfu/lfu_test.go b/lfu/lfu_test.go index 9dd8fcd..5e78f9c 100644 --- a/lfu/lfu_test.go +++ b/lfu/lfu_test.go @@ -27,13 +27,9 @@ func TestCollection(t *testing.T) { } } - front := f.Front() - back := f.Back() oldest := f.Discard() f.Remove(entries[2]) - assert.Equal(t, front.Key, 2) - assert.Equal(t, back.Key, 1) assert.Equal(t, oldest.Key, 1) assert.Equal(t, f.Len(), 1) assert.Equal(t, (*f)[0].value.Key, 2) diff --git a/lifo/lifo.go b/lifo/lifo.go index 8a32fa5..1248dc6 100644 --- a/lifo/lifo.go +++ b/lifo/lifo.go @@ -42,20 +42,6 @@ func (c *collection) Discard() (e *internal.Entry) { return } -func (c *collection) Front() (e *internal.Entry) { - if le := c.ll.Front(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - -func (c *collection) Back() (e *internal.Entry) { - if le := c.ll.Back(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - func (c *collection) Len() int { return c.ll.Len() } diff --git a/lifo/lifo_test.go b/lifo/lifo_test.go index 91f6301..b43618c 100644 --- a/lifo/lifo_test.go +++ b/lifo/lifo_test.go @@ -28,15 +28,11 @@ func TestCollection(t *testing.T) { } } - front := c.Front() - back := c.Back() - oldest := c.Discard() c.Remove(entries[0]) + back := c.ll.Back().Value.(*internal.Entry) - assert.Equal(t, 1, front.Key) - assert.Equal(t, 3, back.Key) assert.Equal(t, 3, oldest.Key) assert.Equal(t, 1, c.Len()) - assert.Equal(t, 2, c.Back().Key) + assert.Equal(t, 2, back.Key) } diff --git a/lru/lru.go b/lru/lru.go index f86bd19..eee1a4f 100644 --- a/lru/lru.go +++ b/lru/lru.go @@ -45,20 +45,6 @@ func (c *collection) Discard() (e *internal.Entry) { return } -func (c *collection) Front() (e *internal.Entry) { - if le := c.ll.Front(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - -func (c *collection) Back() (e *internal.Entry) { - if le := c.ll.Back(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - func (c *collection) Len() int { return c.ll.Len() } diff --git a/lru/lru_test.go b/lru/lru_test.go index 8c3c65e..000debc 100644 --- a/lru/lru_test.go +++ b/lru/lru_test.go @@ -28,15 +28,11 @@ func TestCollection(t *testing.T) { } } - front := c.Front() - back := c.Back() - oldest := c.Discard() c.Remove(entries[2]) + back := c.ll.Back().Value.(*internal.Entry) - assert.Equal(t, 3, front.Key) - assert.Equal(t, 1, back.Key) assert.Equal(t, 1, oldest.Key) assert.Equal(t, 1, c.Len()) - assert.Equal(t, 2, c.Back().Key) + assert.Equal(t, 2, back.Key) } diff --git a/mru/mru.go b/mru/mru.go index e541af6..ad772fe 100644 --- a/mru/mru.go +++ b/mru/mru.go @@ -45,20 +45,6 @@ func (c *collection) Discard() (e *internal.Entry) { return } -func (c *collection) Front() (e *internal.Entry) { - if le := c.ll.Front(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - -func (c *collection) Back() (e *internal.Entry) { - if le := c.ll.Back(); le != nil { - e = le.Value.(*internal.Entry) - } - return -} - func (c *collection) Len() int { return c.ll.Len() } diff --git a/mru/mru_test.go b/mru/mru_test.go index f0816c8..104f663 100644 --- a/mru/mru_test.go +++ b/mru/mru_test.go @@ -28,14 +28,11 @@ func TestCollection(t *testing.T) { } } - front := c.Front() - back := c.Back() oldest := c.Discard() c.Remove(entries[1]) + back := c.ll.Back().Value.(*internal.Entry) - assert.Equal(t, 3, front.Key) - assert.Equal(t, 1, back.Key) assert.Equal(t, 3, oldest.Key) assert.Equal(t, 1, c.Len()) - assert.Equal(t, 1, c.Back().Key) + assert.Equal(t, 1, back.Key) }