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可以看作桶,每次有元素被访问的时候就会跳到下一个桶