lru

package
v0.0.0-...-c42a728 Latest Latest
Warning

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

Go to latest
Published: May 17, 2016 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package lru implements an LRU cache.

LRU 最近最少使用算法,LRU算法主要用于缓存淘汰。 原理:

添加元素时,放到链表头 缓存命中,将元素移动到链表头 缓存满了之后,将链表尾的元素删除

LRU实现:

可以用一个双向链表保存数据 使用hash实现O(1)的访问

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 specificies 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.

func (*Cache) Add

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

Add adds a value to 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

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)

Remove removes the provided key from the cache. 在缓存中移除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 Key 是任何可以比较的值

Jump to

Keyboard shortcuts

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