lru

package
v0.0.0-...-e7086ac Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package lru implements an LRU cache. lru用于实现LRU cache

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// MaxEntries is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	// 最大入口数 也就是缓冲最多存几条数据 超过了就会触发淘汰 0表示没有限制
	MaxEntries int

	// OnEvicted optionally specifies a callback function to be
	// executed when an entry is purged from the cache.
	// 销毁前的回调
	OnEvicted func(key Key, value interface{})
	// contains filtered or unexported fields
}

Cache is an LRU cache. It is not safe for concurrent access. cache结构用于实现LRU cache 算法,并发访问不安全

func New

func New(maxEntries int) *Cache

New creates a new Cache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller. 初始化一个cache类型的实例

func (*Cache) Add

func (c *Cache) Add(key Key, value interface{})

Add adds a value to the cache. 往缓冲中增加一个值

func (*Cache) Clear

func (c *Cache) Clear()

Clear purges all stored items from the cache.

func (*Cache) Get

func (c *Cache) Get(key Key) (value interface{}, ok bool)

Get looks up a key's value from the cache. 根据key查找到value

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Remove

func (c *Cache) Remove(key Key)

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest()

RemoveOldest removes the oldest item from the cache. 删除最久的

type Key

type Key interface{}

A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators 任意类型

Jump to

Keyboard shortcuts

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