cache

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cache Package lru implements an LRU cache.

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 NewCache

func NewCache(size int) (c *Cache)

NewCache creates an LRU of the given size.

func (*Cache) Add

func (c *Cache) Add(key Key, value interface{})

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

func (*Cache) Clear

func (c *Cache) Clear()

Clear is used to completely clear the cache.

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

func (c *Cache) Length() int

Length returns the number of items in 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.

type Key

type Key interface{}

A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators

type Lru

type Lru struct {
	// MaxEntries is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	MaxEntries int

	// OnEvicted optionally specifies a callback function to be
	// executed when an entry is purged from the cache.
	OnEvicted func(key Key, value interface{})
	// contains filtered or unexported fields
}

Lru is an LRU cache. It is not safe for concurrent access.

func New

func New(maxEntries int) *Lru

New creates a new Cache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.

func (*Lru) Add

func (c *Lru) Add(key Key, value interface{})

Add adds a value to the cache.

func (*Lru) Clear

func (c *Lru) Clear()

Clear purges all stored items from the cache.

func (*Lru) Contains

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

func (*Lru) Get

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

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

func (*Lru) Keys

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

func (*Lru) Len

func (c *Lru) Len() int

Len returns the number of items in the cache.

func (*Lru) Remove

func (c *Lru) Remove(key Key)

Remove removes the provided key from the cache.

func (*Lru) RemoveOldest

func (c *Lru) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

Jump to

Keyboard shortcuts

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