You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
784 B

package LRUCache
import "container/list"
type item struct {
key string
value string
pos *list.Element
}
type LRU struct {
ul list.List
data map[string]*item
Maxsize int
}
func (lru *LRU) Get(key string) string {
i := lru.data[key]
if i != nil {
lru.ul.Remove(i.pos)
lru.ul.PushBack(i)
return i.value
} else {
return ""
}
}
func (lru *LRU) Set(key string, value string) {
i := lru.data[key]
if i == nil {
i = &item{key: key, value: value}
lru.data[key] = i
} else {
lru.ul.Remove(i.pos)
}
lru.ul.PushBack(i)
if lru.ul.Len() > lru.Maxsize {
delete(lru.data, lru.ul.Front().Value.(*item).key)
lru.ul.Remove(lru.ul.Front())
}
i.pos = lru.ul.Back()
}
func (lru *LRU) Init() {
lru.data = make(map[string]*item)
lru.ul = list.List{}
}