cache

package
v0.29.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithNotFoundTtl

func WithNotFoundTtl[T any](notFoundTtl time.Duration) func(cache *cache[T])

Types

type Cache

type Cache[T any] interface {
	// Contains checks whether any not yet expired element with key exists in the cache.
	Contains(key string) bool

	// Expire updates the expiry of the item with key if it exists.
	// Returns true if an item was expired.
	Expire(key string) bool

	// Get returns the item stored for key if it exists and is not expired.
	// Returns the item and whether a non expired item was found.
	Get(key string) (T, bool)

	// Set sets the value for key with the cache's default ttl.
	Set(key string, value T)

	// SetX sets the value for key with the provided ttl.
	SetX(key string, value T, ttl time.Duration)

	// Mutate atomically retrieves the item for key from the cache.
	// If it exists and is not expired it is passed to mutate, else a nil pointer is passed.
	// The mutated value returned by mutated is then stored in the cache
	// if it isn't its type's zero value or the [WithNotFoundTtl] option is set on cache creation.
	// Uses the cache's default ttl.
	Mutate(key string, mutate func(value *T) T) T

	// MutateX works the same as [Mutate], just uses the provided ttl instead of the cache's default.
	MutateX(key string, mutate func(value *T) T, ttl time.Duration) T

	// Provide retrieves the non expired item from the cache if it exists.
	// If not, provider is called to retrieve a new value.
	// If the provided value isn't its type's zero value or the [WithNotFoundTtl] option is
	// set on cache creation the provided value is stored in the cache.
	// Returns the retrieved or provided value.
	Provide(key string, provider func() T) T

	// ProvideWithError works the same as [Provide], but if provider returns an error the
	// provided value is immediately returned and not stored in the cache.
	ProvideWithError(key string, provider func() (T, error)) (T, error)

	// Delete deletes the item with key from the cache.
	// Returns true if the item was present, false otherwise.
	Delete(key string) bool
}

func New

func New[T any](maxSize int64, pruneCount uint32, ttl time.Duration, options ...Option[T]) Cache[T]

func NewWithConfiguration

func NewWithConfiguration[T any](config ccache.Configuration, ttl time.Duration, options ...Option[T]) Cache[T]

type Option

type Option[T any] func(*cache[T])

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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