v0.1.2 Latest Latest

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

Go to latest
Published: May 11, 2015 License: MPL-2.0, MPL-2.0 Imports: 3 Imported by: 0



This provides the lru package which implements a fixed-size thread safe LRU cache. It is based on the cache in Groupcache.


Full docs are available on Godoc


Using the LRU is very simple:

l, _ := New(128)
for i := 0; i < 256; i++ {
    l.Add(i, nil)
if l.Len() != 128 {
    panic(fmt.Sprintf("bad len: %v", l.Len()))



This package provides a simple LRU cache. It is based on the LRU implementation in groupcache:



This section is empty.


This section is empty.


This section is empty.


type Cache

type Cache struct {
	// contains filtered or unexported fields

Cache is a thread-safe fixed size LRU cache.

func New

func New(size int) (*Cache, error)

New creates an LRU of the given size

func NewWithEvict

func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error)

func (*Cache) Add

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

Add adds a value to the cache. Returns true if an eviction occured.

func (*Cache) Get

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

Get looks up a key's value from the cache.

func (*Cache) Keys

func (c *Cache) Keys() []interface{}

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Purge

func (c *Cache) Purge()

Purge is used to completely clear the cache

func (*Cache) Remove

func (c *Cache) Remove(key interface{})

Remove removes the provided key from the cache.

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

Jump to

Keyboard shortcuts

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