lfu

package
v0.0.0-...-af6808f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: GPL-3.0 Imports: 1 Imported by: 0

README

LRU (Least Frequently Used)

实现思路

维护一个小根堆,并记录每个key的访问次数,这样小根堆的第一个元素就是要被淘汰的元素。

核心数据结构

使用小根堆 + 哈希表来实现

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap []*entry

func (*Heap) Len

func (h *Heap) Len() int

func (*Heap) Less

func (h *Heap) Less(i, j int) bool

func (*Heap) Pop

func (h *Heap) Pop() interface{}

func (*Heap) Push

func (h *Heap) Push(v interface{})

func (*Heap) Swap

func (h *Heap) Swap(i, j int)

type LFUCache

type LFUCache struct {
	OnEvicted func(key string, value Value) // 当一个 entry 被淘汰时触发的回调函数
	// contains filtered or unexported fields
}

lfu cache 对象

func New

func New(maxBytes int64, OnEvicted func(key string, value Value)) *LFUCache

func (*LFUCache) Add

func (c *LFUCache) Add(key string, value Value)

添加/更新 缓存的 key-value 对, 命中缓存,则更新,否则新增

func (*LFUCache) CurrentBytes

func (c *LFUCache) CurrentBytes() int64

func (*LFUCache) Get

func (c *LFUCache) Get(key string) (value Value, ok bool)

根据 key 来从缓存中获取元素

func (*LFUCache) Len

func (c *LFUCache) Len() int

func (*LFUCache) MaxBytes

func (c *LFUCache) MaxBytes() int64

type Value

type Value interface {
	Len() int
}

value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL