cache

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2017 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves an element based on a key, returning nil if the element
	// does not exist
	Get(key string) interface{}

	// Put adds an element to the cache, returning the previous element
	Put(key string, value interface{}) interface{}

	// Delete deletes an element in the cache
	Delete(key string)

	// Size returns the number of entries currently stored in the Cache
	Size() int

	// CompareAndSwap adds an element to the cache if the existing entry matches the old value.
	// It returns the element in cache after function is executed and true if the element was replaced, false otherwise.
	CompareAndSwap(key string, old, new interface{}) (interface{}, bool)
}

A Cache is a generalized interface to a cache. See cache.LRU for a specific implementation (bounded cache with LRU eviction)

type EvictCallback

type EvictCallback func(key string, value interface{})

EvictCallback is a type for notifying applications when an item is scheduled for eviction from the Cache.

type LRU

type LRU struct {
	TimeNow func() time.Time
	// contains filtered or unexported fields
}

LRU is a concurrent fixed size cache that evicts elements in LRU order as well as by TTL.

func NewLRU

func NewLRU(maxSize int) *LRU

NewLRU creates a new LRU cache with default options.

func NewLRUWithOptions

func NewLRUWithOptions(maxSize int, opts *Options) *LRU

NewLRUWithOptions creates a new LRU cache with the given options.

func (*LRU) CompareAndSwap

func (c *LRU) CompareAndSwap(key string, oldValue, newValue interface{}) (itemInCache interface{}, replaced bool)

CompareAndSwap puts a new value associated with a given key if existing value matches oldValue. It returns itemInCache as the element in cache after the function is executed and replaced as true if value is replaced, false otherwise.

func (*LRU) Delete

func (c *LRU) Delete(key string)

Delete deletes a key, value pair associated with a key

func (*LRU) Get

func (c *LRU) Get(key string) interface{}

Get retrieves the value stored under the given key

func (*LRU) Put

func (c *LRU) Put(key string, value interface{}) interface{}

Put puts a new value associated with a given key, returning the existing value (if present)

func (*LRU) Size

func (c *LRU) Size() int

Size returns the number of entries currently in the lru, useful if cache is not full

type Options

type Options struct {
	// TTL controls the time-to-live for a given cache entry.  Cache entries that
	// are older than the TTL will not be returned
	TTL time.Duration

	// InitialCapacity controls the initial capacity of the cache
	InitialCapacity int

	// OnEvict is an optional function called when an element is evicted.
	OnEvict EvictCallback

	// TimeNow is used to override the behavior of default time.Now(), e.g. in tests.
	TimeNow func() time.Time
}

Options control the behavior of the cache

Jump to

Keyboard shortcuts

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