LRU=hashmap+double-linked list
map[key]=*listnode
save:
node,ok := map[key]
if !ok {
list.PushBack(newnode)
map[key]=*newnode
} else {
node = map[key]
node.value = newvalue
list.MoveToBack(node)
}
if len(map) > SIZE {
list.Remove(list.Front())
}
get:
node,ok := map[key]
if !ok {
return nil
}
list.MoveToBack(node)
return node
网友评论