cache

package
v3.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 4 Imported by: 3 modules, 8 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

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheFull = errors.New("cache is full")

Functions

func DefaultOnExpire

func DefaultOnExpire[D any](D)

Types

type Cache

type Cache[K comparable, D any] struct {
	*sync.Map[K, *Element[D]]
}

func NewCache

func NewCache[K comparable, D any]() *Cache[K, D]

func (*Cache[K, D]) CheckExpirations

func (c *Cache[K, D]) CheckExpirations(now time.Time)

func (*Cache[K, D]) Load

func (c *Cache[K, D]) Load(key K) (actual *Element[D])

func (*Cache[K, D]) LoadOrStore

func (c *Cache[K, D]) LoadOrStore(key K, e *Element[D]) (actual *Element[D], loaded bool)

func (*Cache[K, D]) LoadOrStoreWithLimit added in v3.5.7

func (c *Cache[K, D]) LoadOrStoreWithLimit(key K, e *Element[D], maxEntries int) (actual *Element[D], loaded bool, err error)

LoadOrStoreWithLimit loads an unexpired entry or atomically stores e if there is capacity. A non-positive maxEntries means unlimited. Expired entries are reclaimed when the cache is full. Existing entries can be loaded at capacity.

type Element

type Element[D any] struct {
	ValidUntil atomic.Time
	// contains filtered or unexported fields
}

func NewElement

func NewElement[D any](data D, validUntil time.Time, onExpire func(d D)) *Element[D]

func (*Element[D]) Data

func (e *Element[D]) Data() D

func (*Element[D]) IsExpired

func (e *Element[D]) IsExpired(now time.Time) bool

Jump to

Keyboard shortcuts

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