cache

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: Apache-2.0 Imports: 4 Imported by: 9

Documentation

Overview

Package cache implements a LRU cache.

The implementation borrows heavily from SmallLRUCache (originally by Nathan Schrenk). The object maintains a doubly-linked list of elements. When an element is accessed, it is promoted to the head of the list. When space is needed, the element at the tail of the list (the least recently used element) is evicted.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = &Config{
	MaxEntries:     5000,
	MaxMemoryUsage: 32 * 1024 * 1024,
	LFU:            true,
}

DefaultConfig is the default configuration for a cache instance in Vitess

Functions

func NewRistrettoCache added in v0.10.0

func NewRistrettoCache(maxEntries, maxCost int64, cost func(any) int64) *ristretto.Cache

NewRistrettoCache returns a Cache implementation based on Ristretto

Types

type Cache added in v0.10.0

type Cache interface {
	Get(key string) (any, bool)
	Set(key string, val any) bool
	ForEach(callback func(any) bool)

	Delete(key string)
	Clear()

	// Wait waits for all pending operations on the cache to settle. Since cache writes
	// are asynchronous, a write may not be immediately accessible unless the user
	// manually calls Wait.
	Wait()

	Len() int
	Evictions() int64
	Hits() int64
	Misses() int64
	UsedCapacity() int64
	MaxCapacity() int64
	SetCapacity(int64)
}

Cache is a generic interface type for a data structure that keeps recently used objects in memory and evicts them when it becomes full.

func NewDefaultCacheImpl added in v0.10.0

func NewDefaultCacheImpl(cfg *Config) Cache

NewDefaultCacheImpl returns the default cache implementation for Vitess. The options in the Config struct control the memory and entry limits for the cache, and the underlying cache implementation.

type Config added in v0.10.0

type Config struct {
	// MaxEntries is the estimated amount of entries that the cache will hold at capacity
	MaxEntries int64
	// MaxMemoryUsage is the maximum amount of memory the cache can handle
	MaxMemoryUsage int64
	// LFU toggles whether to use a new cache implementation with a TinyLFU admission policy
	LFU bool
}

Config is the configuration options for a cache instance

type Item

type Item struct {
	Key   string
	Value any
}

Item is what is stored in the cache

type LRUCache

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

LRUCache is a typical LRU cache implementation. If the cache reaches the capacity, the least recently used item is deleted from the cache. Note the capacity is not the number of items, but the total sum of the CachedSize() of each item.

func NewLRUCache

func NewLRUCache(capacity int64, cost func(any) int64) *LRUCache

NewLRUCache creates a new empty cache with the given capacity.

func (*LRUCache) Clear

func (lru *LRUCache) Clear()

Clear will clear the entire cache.

func (*LRUCache) Delete

func (lru *LRUCache) Delete(key string)

Delete removes an entry from the cache

func (*LRUCache) Evictions

func (lru *LRUCache) Evictions() int64

Evictions returns the number of evictions

func (*LRUCache) ForEach added in v0.10.0

func (lru *LRUCache) ForEach(callback func(value any) bool)

ForEach yields all the values for the cache, ordered from most recently used to least recently used.

func (*LRUCache) Get

func (lru *LRUCache) Get(key string) (v any, ok bool)

Get returns a value from the cache, and marks the entry as most recently used.

func (*LRUCache) Hits added in v0.13.0

func (lru *LRUCache) Hits() int64

Hits returns number of cache hits since creation

func (*LRUCache) Items

func (lru *LRUCache) Items() []Item

Items returns all the values for the cache, ordered from most recently used to least recently used.

func (*LRUCache) Len added in v0.10.0

func (lru *LRUCache) Len() int

Len returns the size of the cache (in entries)

func (*LRUCache) MaxCapacity added in v0.10.0

func (lru *LRUCache) MaxCapacity() int64

MaxCapacity returns the cache maximum capacity.

func (*LRUCache) Misses added in v0.13.0

func (lru *LRUCache) Misses() int64

Misses returns number of cache misses since creation

func (*LRUCache) Set

func (lru *LRUCache) Set(key string, value any) bool

Set sets a value in the cache.

func (*LRUCache) SetCapacity

func (lru *LRUCache) SetCapacity(capacity int64)

SetCapacity will set the capacity of the cache. If the capacity is smaller, and the current cache size exceed that capacity, the cache will be shrank.

func (*LRUCache) UsedCapacity added in v0.10.0

func (lru *LRUCache) UsedCapacity() int64

UsedCapacity returns the size of the cache (in bytes)

func (*LRUCache) Wait added in v0.10.0

func (lru *LRUCache) Wait()

Wait is a no-op in the LRU cache

Directories

Path Synopsis
Package ristretto is a fast, fixed size, in-memory cache with a dual focus on throughput and hit ratio performance.
Package ristretto is a fast, fixed size, in-memory cache with a dual focus on throughput and hit ratio performance.

Jump to

Keyboard shortcuts

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