Skip to content

Commit bb15979

Browse files
author
sanad haj yahya
committed
Revert "feat: add cache front/back method to get the first/last element"
This reverts commit e8cdb07.
1 parent e8cdb07 commit bb15979

File tree

15 files changed

+9
-170
lines changed

15 files changed

+9
-170
lines changed

arc/arc.go

-14
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ type arc struct {
3232
b2 *internal.Cache
3333
}
3434

35-
func (a *arc) Front() interface{} {
36-
if k := a.t1.Front(); k != nil {
37-
return k
38-
}
39-
return a.t2.Front()
40-
}
41-
42-
func (a *arc) Back() interface{} {
43-
if k := a.t1.Back(); k != nil {
44-
return k
45-
}
46-
return a.t2.Back()
47-
}
48-
4935
func (a *arc) Load(key interface{}) (value interface{}, ok bool) {
5036
if val, ok := a.t1.Peek(key); ok {
5137
exp, _ := a.t1.Expiry(key)

arc/arc_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ func TestARCc(t *testing.T) {
5353

5454
a.Store(1, 1)
5555
a.Load(1)
56-
5756
assert.Equal(t, 0, a.t1.Len())
5857
assert.Equal(t, 1, a.t2.Len())
59-
assert.Equal(t, 1, a.Front())
60-
assert.Equal(t, 1, a.Back())
6158

6259
a.Delete(1)
6360
}

cache.go

+1-29
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ type Event = internal.Event
2424

2525
// Cache stores data so that future requests for that data can be served faster.
2626
type Cache interface {
27-
// Front returns the first key of cache or nil if the cache is empty.
28-
//
29-
// # Experimental
30-
//
31-
// Notice: This func is EXPERIMENTAL and may be changed or removed in a
32-
// later release.
33-
Front() interface{}
34-
// Back returns the last key of cache or nil if the cache is empty.
35-
//
36-
// # Experimental
37-
//
38-
// Notice: This func is EXPERIMENTAL and may be changed or removed in a
39-
// later release.
40-
Back() interface{}
4127
// Load returns key value.
4228
Load(key interface{}) (interface{}, bool)
4329
// Peek returns key value without updating the underlying "recent-ness".
@@ -105,7 +91,7 @@ type Cache interface {
10591
// GC is a long running function, it returns when ctx done, therefore the
10692
// caller must start it in its own goroutine.
10793
//
108-
// # Experimental
94+
// Experimental
10995
//
11096
// Notice: This func is EXPERIMENTAL and may be changed or removed in a
11197
// later release.
@@ -156,20 +142,6 @@ type cache struct {
156142
unsafe Cache
157143
}
158144

159-
func (c *cache) Front() interface{} {
160-
c.mu.Lock()
161-
k := c.unsafe.Front()
162-
c.mu.Unlock()
163-
return k
164-
}
165-
166-
func (c *cache) Back() interface{} {
167-
c.mu.Lock()
168-
k := c.unsafe.Back()
169-
c.mu.Unlock()
170-
return k
171-
}
172-
173145
func (c *cache) Load(key interface{}) (interface{}, bool) {
174146
c.mu.Lock()
175147
v, ok := c.unsafe.Load(key)

fifo/fifo.go

-14
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,6 @@ func (c *collection) Discard() (e *internal.Entry) {
4242
return
4343
}
4444

45-
func (c *collection) Front() (e *internal.Entry) {
46-
if le := c.ll.Front(); le != nil {
47-
e = le.Value.(*internal.Entry)
48-
}
49-
return
50-
}
51-
52-
func (c *collection) Back() (e *internal.Entry) {
53-
if le := c.ll.Back(); le != nil {
54-
e = le.Value.(*internal.Entry)
55-
}
56-
return
57-
}
58-
5945
func (c *collection) Len() int {
6046
return c.ll.Len()
6147
}

fifo/fifo_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ func TestCollection(t *testing.T) {
2828
}
2929
}
3030

31-
front := c.Front()
32-
back := c.Back()
3331
oldest := c.Discard()
3432
c.Remove(entries[2])
33+
back := c.ll.Back().Value.(*internal.Entry)
3534

36-
assert.Equal(t, 1, front.Key)
37-
assert.Equal(t, 3, back.Key)
3835
assert.Equal(t, 1, oldest.Key)
3936
assert.Equal(t, 1, c.Len())
40-
assert.Equal(t, 2, c.Back().Key)
37+
assert.Equal(t, 2, back.Key)
4138
}

idle/idle.go

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ func New(cap int) libcache.Cache {
1818

1919
type idle struct{}
2020

21-
func (idle) Front() (v interface{}) { return }
22-
func (idle) Back() (v interface{}) { return }
2321
func (idle) Load(interface{}) (v interface{}, ok bool) { return }
2422
func (idle) Peek(interface{}) (v interface{}, ok bool) { return }
2523
func (idle) Keys() (keys []interface{}) { return }

internal/cache.go

-26
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ type Collection interface {
5353
Add(*Entry)
5454
Remove(*Entry)
5555
Discard() *Entry
56-
Front() *Entry
57-
Back() *Entry
5856
Len() int
5957
Init()
6058
}
@@ -99,30 +97,6 @@ type Cache struct {
9997
capacity int
10098
}
10199

102-
// Front returns the first key of cache or nil if the cache is empty.
103-
func (c *Cache) Front() interface{} {
104-
// Run GC inline before get the front entry.
105-
c.GC()
106-
107-
if e := c.coll.Front(); e != nil {
108-
return e.Key
109-
}
110-
111-
return nil
112-
}
113-
114-
// Back returns the last key of cache or nil if the cache is empty.
115-
func (c *Cache) Back() interface{} {
116-
// Run GC inline before get the back entry.
117-
c.GC()
118-
119-
if e := c.coll.Back(); e != nil {
120-
return e.Key
121-
}
122-
123-
return nil
124-
}
125-
126100
// Load returns key value.
127101
func (c *Cache) Load(key interface{}) (interface{}, bool) {
128102
return c.get(key, false)

lfu/lfu.go

-14
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,6 @@ func (f *collection) Discard() (e *internal.Entry) {
5757
return heap.Pop(f).(*element).value
5858
}
5959

60-
func (f *collection) Front() (e *internal.Entry) {
61-
if f.Len() > 0 {
62-
e = (*f)[f.Len()-1].value
63-
}
64-
return
65-
}
66-
67-
func (f *collection) Back() (e *internal.Entry) {
68-
if f.Len() > 0 {
69-
e = (*f)[0].value
70-
}
71-
return
72-
}
73-
7460
func (f *collection) Move(e *internal.Entry) {
7561
ele := e.Element.(*element)
7662
ele.count++

lfu/lfu_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@ func TestCollection(t *testing.T) {
2727
}
2828
}
2929

30-
front := f.Front()
31-
back := f.Back()
3230
oldest := f.Discard()
3331
f.Remove(entries[2])
3432

35-
assert.Equal(t, front.Key, 2)
36-
assert.Equal(t, back.Key, 1)
3733
assert.Equal(t, oldest.Key, 1)
3834
assert.Equal(t, f.Len(), 1)
3935
assert.Equal(t, (*f)[0].value.Key, 2)

lifo/lifo.go

-14
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,6 @@ func (c *collection) Discard() (e *internal.Entry) {
4242
return
4343
}
4444

45-
func (c *collection) Front() (e *internal.Entry) {
46-
if le := c.ll.Front(); le != nil {
47-
e = le.Value.(*internal.Entry)
48-
}
49-
return
50-
}
51-
52-
func (c *collection) Back() (e *internal.Entry) {
53-
if le := c.ll.Back(); le != nil {
54-
e = le.Value.(*internal.Entry)
55-
}
56-
return
57-
}
58-
5945
func (c *collection) Len() int {
6046
return c.ll.Len()
6147
}

lifo/lifo_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ func TestCollection(t *testing.T) {
2828
}
2929
}
3030

31-
front := c.Front()
32-
back := c.Back()
33-
3431
oldest := c.Discard()
3532
c.Remove(entries[0])
33+
back := c.ll.Back().Value.(*internal.Entry)
3634

37-
assert.Equal(t, 1, front.Key)
38-
assert.Equal(t, 3, back.Key)
3935
assert.Equal(t, 3, oldest.Key)
4036
assert.Equal(t, 1, c.Len())
41-
assert.Equal(t, 2, c.Back().Key)
37+
assert.Equal(t, 2, back.Key)
4238
}

lru/lru.go

-14
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,6 @@ func (c *collection) Discard() (e *internal.Entry) {
4545
return
4646
}
4747

48-
func (c *collection) Front() (e *internal.Entry) {
49-
if le := c.ll.Front(); le != nil {
50-
e = le.Value.(*internal.Entry)
51-
}
52-
return
53-
}
54-
55-
func (c *collection) Back() (e *internal.Entry) {
56-
if le := c.ll.Back(); le != nil {
57-
e = le.Value.(*internal.Entry)
58-
}
59-
return
60-
}
61-
6248
func (c *collection) Len() int {
6349
return c.ll.Len()
6450
}

lru/lru_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ func TestCollection(t *testing.T) {
2828
}
2929
}
3030

31-
front := c.Front()
32-
back := c.Back()
33-
3431
oldest := c.Discard()
3532
c.Remove(entries[2])
33+
back := c.ll.Back().Value.(*internal.Entry)
3634

37-
assert.Equal(t, 3, front.Key)
38-
assert.Equal(t, 1, back.Key)
3935
assert.Equal(t, 1, oldest.Key)
4036
assert.Equal(t, 1, c.Len())
41-
assert.Equal(t, 2, c.Back().Key)
37+
assert.Equal(t, 2, back.Key)
4238
}

mru/mru.go

-14
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,6 @@ func (c *collection) Discard() (e *internal.Entry) {
4545
return
4646
}
4747

48-
func (c *collection) Front() (e *internal.Entry) {
49-
if le := c.ll.Front(); le != nil {
50-
e = le.Value.(*internal.Entry)
51-
}
52-
return
53-
}
54-
55-
func (c *collection) Back() (e *internal.Entry) {
56-
if le := c.ll.Back(); le != nil {
57-
e = le.Value.(*internal.Entry)
58-
}
59-
return
60-
}
61-
6248
func (c *collection) Len() int {
6349
return c.ll.Len()
6450
}

mru/mru_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ func TestCollection(t *testing.T) {
2828
}
2929
}
3030

31-
front := c.Front()
32-
back := c.Back()
3331
oldest := c.Discard()
3432
c.Remove(entries[1])
33+
back := c.ll.Back().Value.(*internal.Entry)
3534

36-
assert.Equal(t, 3, front.Key)
37-
assert.Equal(t, 1, back.Key)
3835
assert.Equal(t, 3, oldest.Key)
3936
assert.Equal(t, 1, c.Len())
40-
assert.Equal(t, 1, c.Back().Key)
37+
assert.Equal(t, 1, back.Key)
4138
}

0 commit comments

Comments
 (0)