Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
lru.go 747 Bytes
Copy Edit Raw Blame History
Atto authored 2022-09-16 15:54 . add lru.go.
package main
import (
"container/list"
"fmt"
)
type LRU struct {
list.List
Max int
}
func (this *LRU) Add(value int) {
head := this.Front()
for head != nil {
// 命中缓存
if head.Value == value {
this.PushFront(head.Value)
this.Remove(head)
return
}
head = head.Next()
}
// 未命中缓存
if this.Len() >= this.Max {
this.Remove(this.Back())
}
this.PushFront(value)
}
func NewLRU(max int) *LRU {
return &LRU{
Max: max,
}
}
func testLRU() {
lru := NewLRU(5)
lru.Add(1)
lru.Add(2)
lru.Add(3)
lru.Add(4)
lru.Add(5)
lru.Add(6)
lru.Add(7)
lru.Add(5)
head := lru.Front()
for head != nil {
fmt.Println(head.Value)
head = head.Next()
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化