Skip to content

Commit

Permalink
Revert "feat: add cache front/back method to get the first/last element"
Browse files Browse the repository at this point in the history
This reverts commit e8cdb07.
  • Loading branch information
sanad haj yahya committed Mar 9, 2023
1 parent e8cdb07 commit bb15979
Show file tree
Hide file tree
Showing 15 changed files with 9 additions and 170 deletions.
14 changes: 0 additions & 14 deletions arc/arc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions arc/arc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
30 changes: 1 addition & 29 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 0 additions & 14 deletions fifo/fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
7 changes: 2 additions & 5 deletions fifo/fifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 0 additions & 2 deletions idle/idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
26 changes: 0 additions & 26 deletions internal/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ type Collection interface {
Add(*Entry)
Remove(*Entry)
Discard() *Entry
Front() *Entry
Back() *Entry
Len() int
Init()
}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 0 additions & 14 deletions lfu/lfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
4 changes: 0 additions & 4 deletions lfu/lfu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 0 additions & 14 deletions lifo/lifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 2 additions & 6 deletions lifo/lifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 0 additions & 14 deletions lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 2 additions & 6 deletions lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 0 additions & 14 deletions mru/mru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
7 changes: 2 additions & 5 deletions mru/mru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit bb15979

Please sign in to comment.