weakcache

package
v0.41.5 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 5 Imported by: 0 modules, 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

Package weakcache provides a concurrency-safe cache keyed by pointer identity that does not keep its keys alive.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K, V any] struct {
	// contains filtered or unexported fields
}

Cache maps pointers to values without retaining the pointers. Entries are found by address and verified through a weak pointer, so a hit costs a map lookup, an entry is dropped once the garbage collector reclaims its key, and an object later allocated at a reused address never sees the entry of the object that lived there before.

The zero value is ready to use. Keys must not be nil.

func (*Cache[K, V]) Clear

func (c *Cache[K, V]) Clear()

Clear invalidates every entry. Invalidated entries for live keys are updated in place by their next Store and entries for unreachable keys are removed as the keys are reclaimed, so clearing never registers a second cleanup for a key that is still alive.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

Len reports the number of entries currently held, including invalidated entries that have not been replaced or reclaimed yet.

func (*Cache[K, V]) Load

func (c *Cache[K, V]) Load(key *K) (V, bool)

Load returns the value stored for key, if there is one and it was stored after the last Clear.

func (*Cache[K, V]) Store

func (c *Cache[K, V]) Store(key *K, value V)

Store records value for key. The entry is dropped automatically after key becomes unreachable.

Jump to

Keyboard shortcuts

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