weakcache

package
v0.41.4 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

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