simplelru

package
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: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EvictCallback

type EvictCallback func(key interface{}, value interface{}, size int)

EvictCallback is used to get a callback when a cache entry is evicted

type LRU

type LRU struct {
	// contains filtered or unexported fields
}

LRU implements a non-thread safe size-aware LRU cache

func NewLRU

func NewLRU(sizeLimit int, onEvict EvictCallback) (*LRU, error)

NewLRU constructs an LRU that should occupy approximately the given size in memory

func NewLRUWithTTL

func NewLRUWithTTL(sizeLimit int, ttl time.Duration, onEvict EvictCallback) (*LRU, error)

NewLRUWithTTL constructs a LRU cache with a ttl for elements

func (*LRU) Add

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

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

func (*LRU) Contains

func (c *LRU) Contains(key interface{}) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness. It may delete it if the key expired

func (*LRU) Get

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

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

func (*LRU) GetOldest

func (c *LRU) GetOldest() (key interface{}, value interface{}, ok bool)

GetOldest returns the oldest entry

func (*LRU) Keys

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

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

func (*LRU) Len

func (c *LRU) Len() int

Len returns the number of items in the cache.

func (*LRU) Peek

func (c *LRU) 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 (*LRU) Purge

func (c *LRU) Purge()

Purge is used to completely clear the cache.

func (*LRU) Remove

func (c *LRU) Remove(key interface{}) (present bool)

Remove removes the provided key from the cache, returning if the key was contained.

func (*LRU) RemoveOldest

func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*LRU) Size

func (c *LRU) Size() int

Size returns the current size of the cache.

type LRUCache

type LRUCache interface {
	// Adds a value to the cache, returns true if an eviction occurred
	// and updates the "recently used"-ness of the key. Size should be
	// the size of the value.
	Add(key, value interface{}, size int) bool

	// Returns key's value from the cache and
	// updates the "recently used"-ness of the key. #value, isFound
	Get(key interface{}) (value interface{}, ok bool)

	// Check if a key exsists in cache without updating the recent-ness.
	Contains(key interface{}) (ok bool)

	// Returns key's value without updating the "recently used"-ness of the key.
	Peek(key interface{}) (value interface{}, ok bool)

	// Removes a key from the cache.
	Remove(key interface{}) bool

	// Removes the oldest entry from cache.
	RemoveOldest() (interface{}, interface{}, bool)

	// Returns the oldest entry from the cache. #key, value, isFound
	GetOldest() (interface{}, interface{}, bool)

	// Returns a slice of the keys in the cache, from oldest to newest.
	Keys() []interface{}

	// Returns the number of items in the cache.
	Len() int

	// Returns the total estimated size of the cache
	Size() int

	// Clear all cache entries
	Purge()
}

LRUCache is the interface for simple LRU cache.

Jump to

Keyboard shortcuts

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