cache

package
v5.5.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2020 License: BSD-3-Clause, BSD-3-Clause, BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MB is a short cut for 1024 * 1024.
	MB = 1024 * 1024

	// PtrSize is the number of bytes a pointer takes.
	PtrSize = 4 << (^uintptr(0) >> 63) // stolen from runtime/internal/sys
	// IntSize is the number of bytes an int or uint takes.
	IntSize = 4 << (^uint(0) >> 63)
)

Variables

This section is empty.

Functions

func StaticSizeOfMap

func StaticSizeOfMap(
	zeroValueKey, zeroValueValue interface{}, count int) (bytes int)

StaticSizeOfMap provides a best-effort estimate of number of bytes that a map takes in memory. It only includes statically sized content (i.e. struct, array, int types, pointer address itself, slice/map's reference address itself, etc.). If needed, dynamic sized stuff (slice/map content, pointer content should be calculated separately by caller.

func StaticSizeOfMapWithSize

func StaticSizeOfMapWithSize(
	keyStaticSize, valueStaticSize int, count int) (bytes int)

StaticSizeOfMapWithSize is a slightly more efficient version of StaticSizeOfMap for when the caller knows the static size of key and value without having to use `reflect`.

Types

type Cache

type Cache interface {
	// Get tries to find and return data assiciated with key.
	Get(key Measurable) (data Measurable, ok bool)
	// Add adds or replaces data into the cache, associating it with key.
	// Entries are evicted when necessary.
	Add(key Measurable, data Measurable)
}

Cache defines an interface for a cache that stores Measurable content. Eviction only happens when Add() is called, and there's no background goroutine for eviction.

func NewLRUEvictedCache

func NewLRUEvictedCache(maxBytes int) Cache

NewLRUEvictedCache returns a Cache that uses LRU eviction strategy. The cache will have a capacity of maxBytes bytes. A zero-byte capacity cache is valid.

Internally we store a memoizing wrapper for the raw Measurable to avoid unnecessarily frequent size calculations.

Note that this means once the entry is in the cache, we never bother recalculating their size. It's fine if the size changes, but the cache eviction will continue using the old size.

func NewRandomEvictedCache

func NewRandomEvictedCache(maxBytes int) Cache

NewRandomEvictedCache returns a Cache that uses random eviction strategy. The cache will have a capacity of maxBytes bytes. A zero-byte capacity cache is valid.

Internally we store a memoizing wrapper for the raw Measurable to avoid unnecessarily frequent size calculations.

Note that memoizing size means once the entry is in the cache, we never bother recalculating their size. It's fine if the size changes, but the cache eviction will continue using the old size.

type Measurable

type Measurable interface {
	// Size returns the size of the object, in bytes, including both statically
	// and dynamically sized parts.
	Size() int
}

Measurable is an interface for types whose size is measurable.

Jump to

Keyboard shortcuts

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