lru

package
v0.0.0-...-e716e59 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {

	// optional and executed when an entry is purged.
	OnEvicted func(key string, value Value) // 某条记录被移除时的回调函数,可以为 nil
	// contains filtered or unexported fields
}

Cache is an LRU cache. It is not safe for concurrent access. 创建一个包含字典和双向链表的结构体类型 Cache,方便实现后续的增删查改操作

func New

func New(maxBytes int64, onEvicted func(string, Value)) *Cache

New is the Constructor of Cache

func (*Cache) Add

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

Add adds a value to the cache.

func (*Cache) Get

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

Get look ups a key's value 查找主要有 2 个步骤,第一步是从字典中找到对应的双向链表的节点,第二步,将该节点移动到队尾

func (*Cache) Len

func (c *Cache) Len() int

Len the number of cache entries 获取添加了多少条数据

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest()

RemoveOldest removes the oldest item 这里的删除,实际上是缓存淘汰。即移除最近最少访问的节点(队首)

type Value

type Value interface {
	Len() int
}

Value use Len to count how many bytes it takes 值是实现了 Value 接口的任意类型,该接口只包含了一个方法 Len() int,用于返回值所占用的内存大小

Jump to

Keyboard shortcuts

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