cache

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package cache provides a thread-safe key/value store.

Use this package when you need a concurrent-safe cache. The cache provides atomic get-or-set operations to prevent redundant computation for the same key.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

Cache stores key/value pairs.

func New

func New[Key comparable, Value any]() *Cache[Key, Value]

New creates a new Cache instance. The benefit of using Cache instead of a regular map is that Cache is thread safe.

func (*Cache[Key, Value]) Clear

func (c *Cache[Key, Value]) Clear()

Clear removes all the values in the cache.

func (*Cache[Key, Value]) Get

func (c *Cache[Key, Value]) Get(key Key) (Value, bool)

Get retrieves a value from the cache if present.

func (*Cache[Key, Value]) GetOrSet

func (c *Cache[Key, Value]) GetOrSet(key Key, getOrSetFn GetOrSetFn[Key, Value]) (Value, error)

GetOrSet tries to get the value, and if not present, it calls getOrSetFn to fetch and set the value.

func (*Cache[Key, Value]) Remove

func (c *Cache[Key, Value]) Remove(key Key) (Value, bool)

Remove removes a key and its value from the cache if present.

func (*Cache[Key, Value]) Set

func (c *Cache[Key, Value]) Set(key Key, value Value)

Set sets a value in the cache.

type GetOrSetFn

type GetOrSetFn[Key comparable, Value any] func(Key) (Value, error)

GetOrSetFn is used in the GetOrSet function of the Cache interface. If the value is not present, the function gets called.

Jump to

Keyboard shortcuts

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