safecache

package
v0.0.67 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: BSD-3-Clause Imports: 2 Imported by: 1

Documentation

Overview

Package safecache provides a generic, thread-safe, lazily initiated cache that ensures initialization occurs only once unless bypass is requested.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a generic, thread-safe cache that ensures initialization occurs only once unless bypass is requested.

func New

func New[T any](initFunc func() (T, error), bypassFunc func() bool) *Cache[T]

New creates a new Cache instance. If bypassFunc is provided and returns true, initFunc will run every time. Panics if initFunc is nil.

func (*Cache[T]) Get

func (c *Cache[T]) Get() (T, error)

Get retrieves the cached value, initializing it if necessary or bypassing the cache.

type CacheMap added in v0.0.29

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

CacheMap is a generic, thread-safe cache map that caches values based on derived keys.

func NewMap added in v0.0.29

func NewMap[K any, DK comparable, V any](
	initFunc mapInitFunc[K, V],
	mapToKeyFunc mapToKeyFunc[K, DK],
	bypassFunc mapBypassFunc[K],
) *CacheMap[K, DK, V]

NewMap creates a new CacheMap instance. If bypassFunc is provided and returns true, initFunc will run every time. Panics if initFunc or mapToKeyFunc is nil.

func (*CacheMap[K, DK, V]) Get added in v0.0.29

func (c *CacheMap[K, DK, V]) Get(key K) (V, error)

Get retrieves the cached value for the given key, initializing it if necessary or bypassing the cache.

Jump to

Keyboard shortcuts

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