cache

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: GPL-3.0 Imports: 0 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[T comparable] struct {
	// contains filtered or unexported fields
}

Cache is a simple generic datatype that caches key-value pairs.

func New

func New[T comparable]() *Cache[T]

Create a new cache

func (*Cache[T]) Clear

func (c *Cache[T]) Clear()

Clear removes all items from the cache, resetting it to an empty state.

func (*Cache[T]) Forget

func (c *Cache[T]) Forget(key string)

func (Cache[T]) Get

func (c Cache[T]) Get(key string) (v T, hit bool)

Get a value from the cache by its key. It returns the value and a boolean indicating if the key was found in the cache.

func (Cache[T]) Items

func (c Cache[T]) Items() map[string]T

Items returns the entire map of cached items.

func (*Cache[T]) Remember

func (c *Cache[T]) Remember(key string, callback func(key string) (T, error)) (T, error)

Gets a value from the cache or generate the value using the provided callback if the key is not found. The callback function takes the key as input and returns a value and an error. If the callback succeeds (non-nil error), the value is cached and returned.

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, value T)

Set a value in the cache. If the key already exists, its value is updated.

Jump to

Keyboard shortcuts

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