1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

fix(algorithm): fix bug of lrucache in issue #251 (#254)

This commit is contained in:
BoWen Qiu
2024-10-16 09:55:38 +08:00
committed by GitHub
parent 0bc11001a4
commit a254ebdc8e

View File

@@ -44,7 +44,7 @@ func (l *LRUCache[K, V]) Get(key K) (V, bool) {
node, ok := l.cache[key] node, ok := l.cache[key]
if ok { if ok {
l.moveToHead(node) l.moveToTail(node)
return node.value, true return node.value, true
} }
@@ -66,7 +66,7 @@ func (l *LRUCache[K, V]) Put(key K, value V) {
} }
} else { } else {
node.value = value node.value = value
l.moveToHead(node) l.moveToTail(node)
} }
l.length = len(l.cache) l.length = len(l.cache)
} }
@@ -79,7 +79,7 @@ func (l *LRUCache[K, V]) Delete(key K) bool {
delete(l.cache, key) delete(l.cache, key)
return true return true
} }
l.length = len(l.cache)
return false return false
} }
@@ -112,7 +112,7 @@ func (l *LRUCache[K, V]) deleteNode(node *lruNode[K, V]) K {
return node.key return node.key
} }
func (l *LRUCache[K, V]) moveToHead(node *lruNode[K, V]) { func (l *LRUCache[K, V]) moveToTail(node *lruNode[K, V]) {
if l.tail == node { if l.tail == node {
return return
} }