@@ -86,21 +86,20 @@ func (c *Cache[K, V]) Get(key K) (zero V, _ bool) {
86
86
return
87
87
}
88
88
entry := e .Value .(* entry [K , V ])
89
- entry .referenceCount = 1
89
+ entry .referenceCount ++
90
90
return entry .val , true
91
91
}
92
92
93
93
func (c * Cache [K , V ]) evict () {
94
- if c .hand .Value == nil {
95
- return
96
- }
97
- for c .hand .Value .(* entry [K , V ]).referenceCount > 0 {
94
+ for c .hand .Value != nil && c .hand .Value .(* entry [K , V ]).referenceCount > 0 {
98
95
c .hand .Value .(* entry [K , V ]).referenceCount --
99
96
c .hand = c .hand .Next ()
100
97
}
101
- entry := c .hand .Value .(* entry [K , V ])
102
- delete (c .items , entry .key )
103
- c .hand .Value = nil
98
+ if c .hand .Value != nil {
99
+ entry := c .hand .Value .(* entry [K , V ])
100
+ delete (c .items , entry .key )
101
+ c .hand .Value = nil
102
+ }
104
103
}
105
104
106
105
// Keys returns the keys of the cache. the order as same as current ring order.
@@ -116,12 +115,10 @@ func (c *Cache[K, V]) Keys() []K {
116
115
// iterating
117
116
for p := c .head .Next (); p != r ; p = p .Next () {
118
117
if p .Value == nil {
119
- break
118
+ continue
120
119
}
121
120
e := p .Value .(* entry [K , V ])
122
- if e .referenceCount > 0 {
123
- keys = append (keys , e .key )
124
- }
121
+ keys = append (keys , e .key )
125
122
}
126
123
return keys
127
124
}
@@ -130,7 +127,7 @@ func (c *Cache[K, V]) Keys() []K {
130
127
func (c * Cache [K , V ]) Delete (key K ) {
131
128
if e , ok := c .items [key ]; ok {
132
129
delete (c .items , key )
133
- e .Value .( * entry [ K , V ]). referenceCount = 0
130
+ e .Value = nil
134
131
}
135
132
}
136
133
0 commit comments