cache

package
Version: v0.0.0-...-7df5e1c Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2021 License: BSD-3-Clause Imports: 2 Imported by: 11

Documentation

Overview

Package cache implements various caching strategies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EvictionNotifier

type EvictionNotifier interface {
	// OnEviction is called on the value of an LRU entry when it's about
	// to be evicted from the cache. This method must not call the LRU
	// cache nor block indefinitely.
	OnEviction(key interface{})
}

EvictionNotifier is an optional interface used by LRU entries that wish to be notified when they are being deleted from the LRU. If implemented by the value of an LRU entry, OnEviction is called when it's time for that entry to be evicted, due to an Add. It is not called if the entry is removed by the LRU's Remove or RemoveOldest methods.

type Iterator

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

Iterator is an iterator through the list. The iterator points to a nil element at the end of the list.

func (*Iterator) GetAndAdvance

func (i *Iterator) GetAndAdvance() (interface{}, interface{}, bool)

GetAndAdvance returns key, value, true if the current entry is valid and advances the iterator. Otherwise it returns nil, nil, false.

type LRU

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

LRU is a least-recently used cache, safe for concurrent access.

func NewLRU

func NewLRU(maxEntries int) *LRU

NewLRU returns a new cache with the provided maximum items.

func (*LRU) Add

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

Add adds the provided key and value to the cache, evicting an old item if necessary.

func (*LRU) Get

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

Get fetches the key's value from the cache. The ok result will be true if the item was found.

func (*LRU) Len

func (c *LRU) Len() int

Len returns the number of items in the cache.

func (*LRU) NewIterator

func (c *LRU) NewIterator() *Iterator

NewIterator returns a new iterator for the LRU.

func (*LRU) NewReverseIterator

func (c *LRU) NewReverseIterator() *Iterator

NewReverseIterator returns a new reverse iterator for the LRU.

func (*LRU) PeekNewest

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

PeekNewest peeks at the most-recently used entry, without modifying the cache in any way. If the cache is empty, two nils are returned.

func (*LRU) PeekOldest

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

PeekOldest peeks at the least-recently used entry, without modifying the cache in any way. If the cache is empty, two nils are returned.

func (*LRU) Remove

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

Remove removes a key from the cache. The return value is the value that was removed or nil if the key was not present. The value's EvictionNotifier is not run by Remove.

func (*LRU) RemoveOldest

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

RemoveOldest removes the oldest item in the cache and returns its key and value. If the cache is empty, the empty string and nil are returned. The value's EvictionNotifier is not run.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL