1 Star 0 Fork 0

ljfirst/algo-go-sdk

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LRUCache.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
ljfirst 提交于 2023-07-04 23:35 +08:00 . feat: PriorityQueue
package list
import (
C "gitee.com/ljfirst/algo-go-sdk/common/constant"
)
/**
* @author ljfirst
* @version V1.0
* @date 2023/6/28 17:19
* @author-Email ljfirst@mail.ustc.edu.cn
* @blogURL https://blog.csdn.net/ljfirst
* @description 最近最久未使用算法
* LRU: The Least Recently Used
* */
type LRUCache struct {
LimitSize int
searchMap map[interface{}]*Node // 快速定位 key
list *DuplexList
}
func NewLRUCache(options ...C.Options) *LRUCache {
opt := C.NewOptions(options...)
lru := &LRUCache{
LimitSize: 3,
searchMap: make(map[interface{}]*Node),
list: NewDuplexList(),
}
lru.LimitSize = opt.LimitSize
return lru
}
func (m *LRUCache) Set(key, value interface{}) {
// 是否存在:存在则更新的
if v, exist := m.searchMap[key]; exist {
m.list.Delete(v)
//m.list.InsertIndex(v, 0)
}
// 是否需要内存淘汰
if m.list.Size() == m.LimitSize {
}
// 插入
}
func (m *LRUCache) Get(key interface{}) interface{} { return nil }
func (m *LRUCache) Delete(key interface{}) {}
func (m *LRUCache) Length() int { return 0 }
func (m *LRUCache) Clear() {}
func (m *LRUCache) GetAttribute() *C.Attribute {
return &C.Attribute{
Tags: []string{C.Cache},
Desc: &C.Desc{
Name: "LRUCache",
NameCn: "最近最久未使用算法",
},
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ljfirst/algo-go-sdk.git
git@gitee.com:ljfirst/algo-go-sdk.git
ljfirst
algo-go-sdk
algo-go-sdk
v1.0.3

搜索帮助