cache

package
v2.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: LGPL-3.0 Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Key, Value any
}

Entry entry of cache.

type PrioCache

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

PrioCache a cache holds entries with priority. If len of cache reaches limit, the entry has lowest priority will be evicted.

func NewPrioCache

func NewPrioCache(limit int) *PrioCache

NewPrioCache create a new PrioCache.

func (*PrioCache) Contains

func (pc *PrioCache) Contains(key any) bool

Contains returns whether the given key is contained.

func (*PrioCache) ForEach

func (pc *PrioCache) ForEach(cb func(*PrioEntry) bool) bool

ForEach iterates all cache entries.

func (*PrioCache) Get

func (pc *PrioCache) Get(key any) (any, float64, bool)

Get retrieves value for given key.

func (*PrioCache) Len

func (pc *PrioCache) Len() int

Len returns count of entries in the cache.

func (*PrioCache) Remove

func (pc *PrioCache) Remove(key any) *PrioEntry

Remove removes the given key, and returns the removed entry if any.

func (*PrioCache) Set

func (pc *PrioCache) Set(key, value any, priority float64)

Set set value and priority for given key.

type PrioEntry

type PrioEntry struct {
	Entry
	Priority float64
}

PrioEntry cache entry with priority.

type RandCache

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

RandCache a simple cache which randomly evicts entries when length exceeds limit.

func NewRandCache

func NewRandCache(limit int) *RandCache

NewRandCache create a new RandCache.

func (*RandCache) Contains

func (rc *RandCache) Contains(key any) bool

Contains returns whether the given key is contained.

func (*RandCache) ForEach

func (rc *RandCache) ForEach(cb func(*Entry) bool) bool

ForEach iterates all entries in the cache.

func (*RandCache) Get

func (rc *RandCache) Get(key any) (any, bool)

Get get value for the given key.

func (*RandCache) Len

func (rc *RandCache) Len() int

Len returns count of entries in the cache.

func (*RandCache) Pick

func (rc *RandCache) Pick() *Entry

Pick pick an entry randomly.

func (*RandCache) Remove

func (rc *RandCache) Remove(key any) bool

Remove removes key.

func (*RandCache) Set

func (rc *RandCache) Set(key, value any)

Set sets value for given key.

type Stats added in v2.2.0

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

Stats is a utility for collecting cache hit/miss.

func (*Stats) Hit added in v2.2.0

func (cs *Stats) Hit() int64

Hit records a hit.

func (*Stats) Miss added in v2.2.0

func (cs *Stats) Miss() int64

Miss records a miss.

func (*Stats) Stats added in v2.2.0

func (cs *Stats) Stats() (bool, int64, int64)

Stats returns the number of hits and misses and whether the hit rate was changed comparing to the last call.

Jump to

Keyboard shortcuts

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