lru

package module
v0.0.0-...-559c5d1 Latest Latest
Warning

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

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

README

golang-lru

GoDoc

This provides the lru package which implements a fixed-size thread safe LRU cache. It is based on the Hashicorp LRU cache, which is based on the cache in Groupcache. This package has two differences:

  • Cache limit is based on object-size, not object-count
  • There is an optional TTL for cached items.

If there is a nonzero TTL, items are removed from the cache lazily as they're accessed. This is useful to set a maximum time for an item to stay in the cache to prevent stale reads if a process updates the data.

Documentation

Full docs are available on Godoc

Example

Using the LRU is very simple:

// Cache with a 128-byte limit
l, _ := New(128)
for i := 0; i < 256; i++ {
    // You have to pass the byte-size of the item you're putting into the cache
    l.Add(i, i, 8)
}
// Len gives the number of items in the cache
if l.Len() != 16 {
    panic(fmt.Sprintf("bad len: %v", l.Len()))
}

Documentation

Overview

Package lru provides LRU cache based on hashicorp's LRU cache that uses size instead of count

The LRU cache is a simple LRU cache that has a memory size limit. You should know the sizes of objects you're adding to the cache, and you should not be adding objects that are larger than the limit. There are no limits on the number of items in the cache as long as the memory size limit is not exceeded.

This is forked from Hashicorp's LRU cache: https://github.com/hashicorp/golang-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 NewWithEvict

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

NewWithEvict constructs a fixed size cache with the given eviction callback.

func NewWithTTL

func NewWithTTL(sizeLimit int, ttl time.Duration) (*Cache, error)

NewWithTTL creates a LRU cache with the given size limit and a TTL for its elements

func NewWithTTLEvict

func NewWithTTLEvict(sizeLimit int, ttl time.Duration, onEvicted func(key interface{}, value interface{}, size int)) (*Cache, error)

NewWithTTLEvict constructs a lru cache with given size limit, ttl for elements, and an onEvicted callback

func (*Cache) Add

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

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

func (*Cache) Contains

func (c *Cache) Contains(key interface{}) bool

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*Cache) ContainsOrAdd

func (c *Cache) ContainsOrAdd(key, value interface{}, size int) (ok, evicted bool)

ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

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) Peek

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

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

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.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the size of the cache

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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