lru

package module
v0.0.0-...-f08842e Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2015 License: MPL-2.0 Imports: 4 Imported by: 0

README

golang-lru

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

Documentation

Full docs are available on Godoc

Example

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()))
}

Documentation

Overview

This package provides a simple LRU cache. It is based on the LRU implementation in groupcache: https://github.com/golang/groupcache/tree/master/lru

This package provides a simple LRU cache. It is based on the LRU implementation in groupcache: https://github.com/golang/groupcache/tree/master/lru

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 (*Cache) Add

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

Add adds a value to the cache.

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.

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.

type FillFunc

type FillFunc func(key interface{}) (value interface{}, expiration time.Time, err error)

func (FillFunc) Fill

func (ff FillFunc) Fill(key interface{}) (value interface{}, expiration time.Time, err error)

type Filler

type Filler interface {
	// Fill returns the value and expiration time of the given key.
	// When Fill fails to get the key, it can return a non-zero expiration to cache the error entry.
	Fill(key interface{}) (value interface{}, expiration time.Time, err error)
}

type FillingCache

type FillingCache struct {
	Metrics ReportMetrics
	// contains filtered or unexported fields
}

FillingCache is a LRU cache. It fills an entry the first time the entry is retrieved. It ensures the entry is filled once even when the entry is retrieved by multiple clients. An entry may expire. An expired entry goes through the filling process the same as a new entry. Note that if Fill fails, the error entry is cached.

func NewFillingCache

func NewFillingCache(size int, fr Filler) *FillingCache

NewFillingCache returns a FillingCache of the given size.

func (*FillingCache) Get

func (c *FillingCache) Get(key interface{}) (value interface{}, err error)

looks up a key's value from the cache, if it is not present get it

type ReportMetrics

type ReportMetrics interface {
	ItemFetched(time time.Duration, hit bool, err error)
}

Jump to

Keyboard shortcuts

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