Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v1.20.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Strings = New(
		xxhash.Sum64String,
		func(s string) bool {

			return len(s) > 256
		},
		func(a, b string) bool { return a == b },
	)

	StringMaps = New(
		func(m map[string]string) (hash uint64) {
			for k, v := range m {

				_, hashk := Strings.getWithHash(k)
				_, hashv := Strings.getWithHash(v)
				hash = hash ^ hashk ^ hashv
			}
			return
		},
		func(m map[string]string) bool {

			return len(m) > 32
		},
		maps.Equal,
	)
)

Functions

func GetOrPutWith added in v1.18.0

func GetOrPutWith[T any](c *Cache[T], hash uint64, eq func(T) bool, get func() T) T

GetOrPutWith tries to find the object from the cache with the given hash and equality function. . If not found, [get] is called to construct the object.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache is a simple fixed size cache for efficient deduplication of objects. The underlying array is held onto with a weak pointer to allow GC to collect it when under memory pressure.

func New

func New[T any](hashfn func(T) uint64, skipfn func(x T) bool, eqfn func(a, b T) bool) *Cache[T]

func (*Cache[T]) Get

func (c *Cache[T]) Get(x T) T

Get a cached object if any. If Get() was called previously with an object equal to [x] and it is found from the cache then it is returned, otherwise [x] is inserted into cache.

Jump to

Keyboard shortcuts

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