cache

package
v2.8.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package cache contains data structures that are useful to build caches.

The types provided by the package are generic building blocks implementing caching algorithms. Synchronization in caching strategies is often very specific to the application and harder to generalize, so the types provided by this package do not make opinionated choices on how synchronization should be handled, which makes them unsafe to use concurrently from multiple goroutines

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache wraps an underlying caching implementation, adding measures of usage.

By default, a LRU caching strategy is used.

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(key K) (value V, deleted bool)

func (*Cache[K, V]) Evict

func (c *Cache[K, V]) Evict() (key K, value V, evicted bool)

func (*Cache[K, V]) Init

func (c *Cache[K, V]) Init(backend Interface[K, V])

func (*Cache[K, V]) Insert

func (c *Cache[K, V]) Insert(key K, value V) (previous V, replaced bool)

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

func (*Cache[K, V]) Lookup

func (c *Cache[K, V]) Lookup(key K) (value V, found bool)

func (*Cache[K, V]) Range

func (c *Cache[K, V]) Range(f func(K, V) bool)

func (*Cache[K, V]) Stats

func (c *Cache[K, V]) Stats() Stats

type Interface

type Interface[K comparable, V any] interface {
	// Returns the number of items in the cache.
	Len() int

	// Inserts an item in the cache, returning the previous value associated
	// with the cache key.
	Insert(key K, value V) (previous V, replaced bool)

	// Returns the value associated with the given key in the cache.
	Lookup(key K) (value V, found bool)

	// Deletes an item from the cache.
	Delete(key K) (value V, deleted bool)

	// Evicts an item from the cache.
	Evict() (key K, value V, evicted bool)

	// Calls f for each entry in the cache. The order in which entries are
	// presented is unspecified. If f returns false, iteration stops.
	Range(f func(K, V) bool)
}

Interface is the interface implemented by caches.

type LRU

type LRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRU is an Interface implementation which caches elements and tracks least recently used items as candidates for eviction.

func (*LRU[K, V]) Delete

func (lru *LRU[K, V]) Delete(key K) (value V, deleted bool)

func (*LRU[K, V]) Evict

func (lru *LRU[K, V]) Evict() (key K, value V, evicted bool)

func (*LRU[K, V]) Insert

func (lru *LRU[K, V]) Insert(key K, value V) (previous V, replaced bool)

func (*LRU[K, V]) Len

func (lru *LRU[K, V]) Len() int

func (*LRU[K, V]) Lookup

func (lru *LRU[K, V]) Lookup(key K) (value V, found bool)

func (*LRU[K, V]) Range

func (lru *LRU[K, V]) Range(f func(K, V) bool)

type Stats

type Stats struct {
	Inserts   int64
	Updates   int64
	Deletes   int64
	Lookups   int64
	Hits      int64
	Evictions int64
}

Stats contains counters tracking usage of a cache.

Jump to

Keyboard shortcuts

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