Skip to content

Commit 995392c

Browse files
authored
Merge pull request #100 from pior/cleanups
Add a missing test and minor code cleanups
2 parents 6d2af3f + 2a13ed5 commit 995392c

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

cache.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ func (c *Cache[T]) gc() int {
373373
if item == nil {
374374
return dropped
375375
}
376-
// fmt.Println(item.key)
377376
prev := item.prev
378377
if !c.tracking || atomic.LoadInt32(&item.refCount) == 0 {
379378
c.bucket(item.key).delete(item.key)

configuration.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ccache
33
type Configuration[T any] struct {
44
maxSize int64
55
buckets int
6-
itemsToPrune int
76
percentToPrune int
87
deleteBuffer int
98
promoteBuffer int
@@ -18,7 +17,6 @@ type Configuration[T any] struct {
1817
func Configure[T any]() *Configuration[T] {
1918
return &Configuration[T]{
2019
buckets: 16,
21-
itemsToPrune: 0,
2220
percentToPrune: 10,
2321
deleteBuffer: 1024,
2422
getsPerPromote: 3,
@@ -99,7 +97,7 @@ func (c *Configuration[T]) Track() *Configuration[T] {
9997
return c
10098
}
10199

102-
// OnDelete allows setting a callback function to react to ideam deletion.
100+
// OnDelete allows setting a callback function to react to item deletion.
103101
// This typically allows to do a cleanup of resources, such as calling a Close() on
104102
// cached object that require some kind of tear-down.
105103
func (c *Configuration[T]) OnDelete(callback func(item *Item[T])) *Configuration[T] {

list_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,34 @@ func Test_List_Remove(t *testing.T) {
5656
assertList(t, l)
5757
}
5858

59+
func Test_List_MoveToFront(t *testing.T) {
60+
l := NewList[int]()
61+
62+
// Single item - already at front
63+
n1 := newItem("a", 1, 0, false)
64+
l.Insert(n1)
65+
l.MoveToFront(n1)
66+
assertList(t, l, 1)
67+
68+
// Head item - already at front
69+
n2 := newItem("b", 2, 0, false)
70+
l.Insert(n2)
71+
assertList(t, l, 2, 1)
72+
l.MoveToFront(n2)
73+
assertList(t, l, 2, 1)
74+
75+
// Tail item
76+
l.MoveToFront(n1)
77+
assertList(t, l, 1, 2)
78+
79+
// Middle item
80+
n3 := newItem("c", 3, 0, false)
81+
l.Insert(n3)
82+
assertList(t, l, 3, 1, 2)
83+
l.MoveToFront(n1)
84+
assertList(t, l, 1, 3, 2)
85+
}
86+
5987
func assertList(t *testing.T, list *List[int], expected ...int) {
6088
t.Helper()
6189

0 commit comments

Comments
 (0)