counting

package
v0.0.0-...-b521294 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Similar to cache.Cache except values are Released when evicted, but only after all the Handles of that value are released. This is useful when the value needs to track a reusable item to know all callers are done with the value. Concurrent safe.

func NewCache

func NewCache[K comparable, V Releaser](o CacheOptions[K, V]) Cache[K, V]

func (Cache[K, V]) All

func (a Cache[K, V]) All() iter.Seq2[K, Handle[V]]

Results ordered by most->least. Will block. Caller must release each Handle.

func (Cache[K, V]) Capacity

func (a Cache[K, V]) Capacity() int64

func (Cache[K, V]) Clear

func (a Cache[K, V]) Clear()

Evicts all and resets. Does not change capacity. Will block.

func (Cache[K, V]) Evict

func (a Cache[K, V]) Evict() (noSpace bool)

func (Cache[K, V]) Get

func (a Cache[K, V]) Get(k K) (Handle[V], bool)

Caller must release Handle. Promotes.

func (Cache[K, V]) Handles

func (a Cache[K, V]) Handles() int

Intended for metrics.

func (Cache[K, V]) Len

func (a Cache[K, V]) Len() int

func (Cache[K, V]) Peek

func (a Cache[K, V]) Peek(k K) (Handle[V], bool)

Caller must release Handle. Does not Promote.

func (Cache[K, V]) Promote

func (a Cache[K, V]) Promote(k K)

func (Cache[K, V]) Set

func (a Cache[K, V]) Set(k K, v V) Handle[V]

Alias for SetS(k,v,1).

func (Cache[K, V]) SetAvailableCapacity

func (a Cache[K, V]) SetAvailableCapacity(available, max int64)

available (+/-) should not consider taken space in cache.

func (Cache[K, V]) SetCapacity

func (a Cache[K, V]) SetCapacity(new int64) (old int64)

func (Cache[K, V]) SetS

func (a Cache[K, V]) SetS(k K, v V, size uint32) Handle[V]

Replaces existing values, which are evicted. A min size of 1 will be used. Caller must release Handle.

func (Cache[K, V]) Size

func (a Cache[K, V]) Size() int64

func (Cache[K, V]) SwapCapacity

func (a Cache[K, V]) SwapCapacity(old, new int64) (swapped bool)

type CacheOptions

type CacheOptions[K any, V Releaser] struct {
	Expiration    time.Duration                                   // Defaults to forever.
	Capacity      int64                                           // Defaults to 100.
	MapCreator    func() maps.Map[K, *cache.CacheValue[*Node[V]]] // defaults to maps.Sync
	PolicyCreator func() policy.Policy[K]                         // defaults to policy.NewARC
	Evict         func(_ K, _ V, Release func())                  // Caller must Release, not V.Release.
	EvictSkip     bool
}

type Handle

type Handle[T any] interface {
	Releaser
	Value() T
}

type Node

type Node[T Releaser] struct {
	// contains filtered or unexported fields
}

A node that tracks Releases from its Handles and only releases the underlying value once all Handles and the node itself have been released. Concurrent safe.

func NewNode

func NewNode[T Releaser](v T) *Node[T]

v is Released after all Handles have been Released plus the node Release.

func (*Node[T]) Handle

func (n *Node[T]) Handle() (_ Handle[T], ok bool)

Node already released when !ok. Caller must release Handle.

func (*Node[T]) Handles

func (n *Node[T]) Handles() int

Intended for metrics.

func (*Node[T]) Release

func (n *Node[T]) Release()

func (*Node[T]) Value

func (n *Node[T]) Value() T

type Releaser

type Releaser interface {
	// Idempotent.
	Release()
}

Jump to

Keyboard shortcuts

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