lru

package
v0.0.0-...-d2438c5 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2019 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EvictCallback

type EvictCallback func(EvictionReason, Field, Value)

EvictCallback lets you know when an eviction has happened in the cache

type EvictionReason

type EvictionReason int

EvictionReason describes why the eviction happened

const (
	// Purged by calling reset
	Purged EvictionReason = iota

	// Popped manually from the cache
	Popped

	// Removed manually from the cache
	Removed

	// Dequeued by walking over due to being dequeued
	Dequeued
)

type Field

type Field = int

Field holds the field alias to the underlying type

type FieldValue

type FieldValue struct {
	Field Field
	Value Value
}

FieldValue is a tuple to both Field and Value type

type LRU

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

LRU implements a non-thread safe fixed size LRU cache

func NewLRU

func NewLRU(size int, onEvict EvictCallback) *LRU

NewLRU creates a LRU cache with a size and callback on eviction

func (*LRU) Add

func (l *LRU) Add(key Field, value Value) bool

Add adds a key, value pair. Returns true if an eviction happened.

func (*LRU) Cap

func (l *LRU) Cap() int

Cap returns the current cap limit to the LRU cache

func (*LRU) Capacity

func (l *LRU) Capacity() bool

Capacity returns if the LRU cache is at capacity or not.

func (*LRU) Contains

func (l *LRU) Contains(key Field) bool

Contains finds out if a key is present in the LRU cache

func (*LRU) Dequeue

func (l *LRU) Dequeue(fn func(Field, Value) error) ([]FieldValue, error)

Dequeue iterates over the LRU cache removing an item upon each iteration.

func (*LRU) Get

func (l *LRU) Get(key Field) (value Value, ok bool)

Get returns back a value if it exists. Returns true if found.

func (*LRU) Keys

func (l *LRU) Keys() []Field

Keys returns the keys as a slice

func (*LRU) Len

func (l *LRU) Len() int

Len returns the current length of the LRU cache

func (*LRU) Peek

func (l *LRU) Peek(key Field) (value Value, ok bool)

Peek returns a value, without marking the LRU cache. Returns true if a value is found.

func (*LRU) Pop

func (l *LRU) Pop() (Field, Value, bool)

Pop removes the last LRU item with in the cache

func (*LRU) Purge

func (l *LRU) Purge()

Purge removes all items with in the cache, calling evict callback on each.

func (*LRU) Remove

func (l *LRU) Remove(key Field) (ok bool)

Remove a value using it's key Returns true if a removal happened

func (*LRU) Slice

func (l *LRU) Slice() []FieldValue

Slice returns a snapshot of the FieldValue pairs.

func (*LRU) Walk

func (l *LRU) Walk(fn func(Field, Value) error) (err error)

Walk iterates over the LRU cache removing an item upon each iteration.

type Value

type Value = string

Value holds the value alias to the underlying type

Jump to

Keyboard shortcuts

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