缓存算法

lru

最近最少使用,这个使用得最多,hash+双向list可以实现O(1)的时间复杂度

lfu

由于要记录缓存项的访问次数,所以需要在每个缓存项添加一个score值,用于记录访问频率,对应这个数据的就是redis的zset,redis是采用skiplist实现的。如果需要自己实现,可以使用min-heap+hash,时间复杂度为O(logn)

TwoQueueCache

TwoQueueCache tracks frequently used and recently used entries separately. This avoids a burst of accesses from taking out frequently used entries, at the cost of about 2x computational overhead and some extra bookkeeping.

ARCCache

ARCCache is an adaptive replacement cache. It tracks recent evictions as well as recent usage in both the frequent and recent caches. Its computational overhead is comparable to TwoQueueCache, but the memory overhead is linear with the size of the cache.

TwoQueueCache & ARCCache 参考https://godoc.org/github.com/hashicorp/golang-lru

lru的O(1)算法

https://github.com/bluele/gcache
不使用zset或者min-heap的思路,而是使用hash+list,这个list的设计很有技巧,它是按照出现次数来排序的,每一个list item里面又是一个set,记录了出现次数相同的item,当访问一个元素的时候,该item的访问次数增加,就会从当前list的set跳到下一个list。当list 里面的item set为空的时候,删除这个list节点。其实list可以看作桶,每次有元素被访问的时候就会跳到下一个桶

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值