@@ -86,21 +86,20 @@ func (c *Cache[K, V]) Get(key K) (zero V, _ bool) {
8686 return
8787 }
8888 entry := e .Value .(* entry [K , V ])
89- entry .referenceCount = 1
89+ entry .referenceCount ++
9090 return entry .val , true
9191}
9292
9393func (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 {
9895 c .hand .Value .(* entry [K , V ]).referenceCount --
9996 c .hand = c .hand .Next ()
10097 }
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+ }
104103}
105104
106105// 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 {
116115 // iterating
117116 for p := c .head .Next (); p != r ; p = p .Next () {
118117 if p .Value == nil {
119- break
118+ continue
120119 }
121120 e := p .Value .(* entry [K , V ])
122- if e .referenceCount > 0 {
123- keys = append (keys , e .key )
124- }
121+ keys = append (keys , e .key )
125122 }
126123 return keys
127124}
@@ -130,7 +127,7 @@ func (c *Cache[K, V]) Keys() []K {
130127func (c * Cache [K , V ]) Delete (key K ) {
131128 if e , ok := c .items [key ]; ok {
132129 delete (c .items , key )
133- e .Value .( * entry [ K , V ]). referenceCount = 0
130+ e .Value = nil
134131 }
135132}
136133
0 commit comments