cache

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 3 Imported by: 0

README

Common Go caching code shared between my projects

GoDoc Build Coverage Go Report Card

License

This project is subject to the the MIT License. See LICENSE information for details.

Documentation

Overview

Package cache provides different cache type (e.g. Key/Value) with pluggable backends.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound error = errors.New("not found")

ErrNotFound is returned by LoadFunc to indicate the requested key was not found.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] interface {
	// Get gets the cached value for the given key.
	// Error ErrNotFound indicates a cache miss.
	Get(ctx context.Context, key K) (V, error)
}

Cache defines the basic cache interface used to retrieve a cached value for a key.

type KeyValue

type KeyValue[K comparable, V any] interface {
	Cache[K, V]
	// Put adds key-value association to the cache.
	// Any previously established key-value association
	// is overwritten.
	Put(ctx context.Context, key K, value V)
	// Delete removes the given key from the cache.
	Delete(ctx context.Context, key K)
	io.Closer
}

KeyValue defines the key-value cache type.

type LoadFunc

type LoadFunc[K comparable, V any] func(ctx context.Context, key K) (V, error)

LoadFunc defines the signature for functions responsible for loading cache values based on the value's key.

func NotFound

func NotFound[K comparable, V any](value V) LoadFunc[K, V]

NotFound is a LoadFunc not loading any value at all. This is suitable for test scenarios or to disable caching.

type Name

type Name string

Name defines the cache name type.

func (Name) String

func (n Name) String() string

Stringer interface

type NoCache

type NoCache[K comparable, V any] struct {
	Value V
	Found bool
}

NoCache provides a cache implementation without any caching at all. This is suitable for test scenarios or to disable caching.

func (*NoCache[K, V]) Get

func (c *NoCache[K, V]) Get(ctx context.Context, key K) (V, bool)

Get implements Cache.Get.

Directories

Path Synopsis
Package memory provides memory based implementation for the different cache types.
Package memory provides memory based implementation for the different cache types.

Jump to

Keyboard shortcuts

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