cache

package
v0.0.0-...-8ef14e1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 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(key string) interface{}

	Put(key string, value interface{}) interface{}

	Delete(key string)

	Size() int

	CompareAndSwap(key string, old, new interface{}) (interface{}, bool)
}

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
}

func NewLRU

func NewLRU(maxSize int) *LRU

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