老师我看到一个golang 实现lru cache

来源:4-1 Python常用内置算法与数据结构常考题

我是一只小蜗牛

2019-10-24

type Cache struct {
// MaxEntries is the maximum number of cache entries before
// an item is evicted. Zero means no limit.
MaxEntries int

// OnEvicted optionally specifies a callback function to be
// executed when an entry is purged from the cache.
OnEvicted func(key Key, value interface{})

ll    *list.List
cache map[interface{}]*list.Element

}
这里的代码为什么不能直接使用ll, 而不省去cache, 是因为get的时候好拿吗?虽然这里cache field是指针

写回答

1回答

PegasusWang

2020-03-10

这个可能和你另一个问题类似,lru 里边 map 是为了快速获取 key对应的 value 数据,而链表是为了保存顺序用的。

0
0

Python工程师面试宝典 一线大厂资深面试官亲授

Python工程师面试必看,资深面试官亲授,倍增面试成功率

1037 学习 · 102 问题

查看课程