package ttlruimport container/listtype Cache struct {MaxEntries intll *list.Listcache map[interface{}]*list.Element}func New(maxEntries int) *Cache {return Cache{MaxEntries: maxEntries,ll: ...
package ttlru
import "container/list"
type Cache struct {
MaxEntries int
ll *list.List
cache map[interface{}]*list.Element
}
func New(maxEntries int) *Cache {
return &Cache{
MaxEntries: maxEntries,
ll: list.New(),
cache: make(map[interface{}]*list.Element),
}
}
type entry struct {
key interface{}
value interface{}
}
func (c *Cache)Add(key,value interface{}) {
if c.cache == nil {
c.cache = make(map[interface{}]*list.Element)
c.ll = list.New()
}
//如果存在,则直接更新
if ee,ok := c.cache[key];ok {
c.cache[key] = ee
c.ll.MoveToFront(ee)
return
}
ele := c.ll.PushFront(&entry{
key: key,
value: value,
})
c.cache[key] = ele
if c.MaxEntries != 0 && len(c.cache) > c.MaxEntries{
c.removeElement(ele)
}
}
func (c *Cache)Get(key interface{}) (interface{},bool) {
if c.cache == nil{
return nil,false
}
if ele,hit := c.cache[key];hit{
return ele.Value.(*entry).value,true
}
return nil,false
}
func (c *Cache)Remove(key interface{}) {
if c.cache == nil{
return
}
if ele,hit := c.cache[key];hit{
c.removeElement(ele)
}
}
func (c *Cache) Clear() {
c.ll = nil
c.cache = nil
}
func (c *Cache) Len() int {
if c.cache == nil {
return 0
}
return c.ll.Len()
}
func (c *Cache)removeElement(e *list.Element) {
c.ll.Remove(e)
delete(c.cache,e.Value.(entry).key)
}
问题:线程非安全,没有加入过期时间
过期时间方案:set时设置过期时间,get时比较key是否过期,过期则删除
线程安全方案:map使用sync.Map(),链表使用读写锁加锁(通常来说,读多写少)
沃梦达教程
本文标题为:go语言lru实现
猜你喜欢
- R语言关于二项分布知识点总结 2022-11-30
- Golang http.Client设置超时 2023-09-05
- Ruby on Rails在Ping ++ 平台实现支付 2023-07-22
- Ruby的字符串与数组求最大值的相关问题讨论 2023-07-22
- R语言-如何切换科学计数法和更换小数点位数 2022-11-23
- Go Web开发进阶实战(gin框架) 2023-09-06
- Ruby 迭代器知识汇总 2023-07-23
- Swift超详细讲解指针 2023-07-08
- R语言绘图数据可视化pie chart饼图 2022-12-10
- 汇编语言程序设计之根据输入改变屏幕颜色的代码 2023-07-06
