Skip to content

Commit b0f170f

Browse files
committed
Remove unneeded calls to the LRU cache
1 parent 9616f8e commit b0f170f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

cache/cache.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@ func New(maxElementCount int) Cache {
6161
}
6262

6363
func (c *lruCache) Add(node Node) Node {
64-
if e, exists := c.dict[string(node.GetKey())]; exists {
64+
key := string(node.GetKey())
65+
if e, exists := c.dict[key]; exists {
6566
c.ll.MoveToFront(e)
6667
old := e.Value
6768
e.Value = node
6869
return old.(Node)
6970
}
7071

7172
elem := c.ll.PushFront(node)
72-
c.dict[string(node.GetKey())] = elem
73+
c.dict[key] = elem
7374

7475
if c.ll.Len() > c.maxElementCount {
7576
oldest := c.ll.Back()
@@ -96,8 +97,9 @@ func (c *lruCache) Len() int {
9697
}
9798

9899
func (c *lruCache) Remove(key []byte) Node {
99-
if elem, exists := c.dict[string(key)]; exists {
100-
return c.remove(elem)
100+
keyS := string(key)
101+
if elem, exists := c.dict[keyS]; exists {
102+
return c.removeWithKey(elem, keyS)
101103
}
102104
return nil
103105
}
@@ -107,3 +109,9 @@ func (c *lruCache) remove(e *list.Element) Node {
107109
delete(c.dict, ibytes.UnsafeBytesToStr(removed.GetKey()))
108110
return removed
109111
}
112+
113+
func (c *lruCache) removeWithKey(e *list.Element, key string) Node {
114+
removed := c.ll.Remove(e).(Node)
115+
delete(c.dict, key)
116+
return removed
117+
}

0 commit comments

Comments
 (0)