specialized

package
v0.0.0-...-fc01e91 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

This example demonstrates a priority queue built using the heap interface.

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 Least-Recently-Used Most-Frequently-Accessed concurrent safe cache. All its methods are safe to call concurrently.

func NewCache

func NewCache(size int, evictMetrics bool) (*Cache, error)

NewCache constructs a new Cache ready for use. The specified size should never be bigger or roughly as big as the maximum available value for uint. evictMetrics tells the cache to collect metrics on recently evicted items, which doubles the memory size of the cache. If evictMetrics is false only normal hits and misses will be collected.

func (*Cache) Cap

func (c *Cache) Cap() int

Cap returns the maximum amount of items the cache can hold.

func (*Cache) Get

func (c *Cache) Get(k string) (v Value, ok bool)

Get retrieves an item from the cache. Its amortized worst-case complexity is ~O(log(c.Len())).

func (*Cache) Len

func (c *Cache) Len() int

Len returns the amount of items currently stored in the cache.

func (*Cache) Metrics

func (c *Cache) Metrics() CacheMetrics

Metrics copies current metrics values and returns the snapshot. If the cache has size<=0 zero metrics will be returned.

func (*Cache) Put

func (c *Cache) Put(k string, v Value)

Put stores an item in the cache. Its amortized worst-case complexity is ~O(log(c.Len())).

func (*Cache) SetTimer

func (c *Cache) SetTimer(timer func() uint)

SetTimer will set the cache internal timer to the given one. The given timer should behave as a monotonic clock and should update its value at least once a second. Calling this after the cache has already been used leads to undefined behavior.

type CacheMetrics

type CacheMetrics struct {

	// HitMFA is the count of hits that returned a value from MFA.
	// Note that a hit on MFA will make the cache skip LRU.
	HitMFA uint
	// MissMFA is the count of misses on MFA access.
	MissMFA uint

	// HitLRU is the count of hits that returned a value from LRU.
	// Note that a LRU is only accessed on MFA misses.
	HitLRU uint
	// MissLRU is the count of misses on LRU access.
	MissLRU uint

	// Miss is the overall amount of misses, which means the requested key was not in MFA nor in LRU.
	Miss uint

	// RecentlyEvictedMiss is the amount of misses that had a key which was recently evicted.
	// If the cache is not running with evicMetrics it will be set to 0.
	RecentlyEvictedMiss uint
}

CacheMetrics carries metrics about a cache usage.

func (CacheMetrics) Hit

func (m CacheMetrics) Hit() uint

Hit returns the total amount of hits.

func (CacheMetrics) Tot

func (m CacheMetrics) Tot() uint

Tot returns the total amount of accesses.

type Value

type Value interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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