Help us shape the future of Go! Take the 2026 Go Developer Survey

shortcache

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package shortcache holds one value read from the database for a short while, so the requests in between share it rather than each asking.

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 keeps one value for TTL. Concurrent readers of an expired value share one load rather than each running it, and Clear makes the next read load afresh. The zero value is not usable; build one with New.

func New

func New[T any](ttl time.Duration) *Cache[T]

New returns an empty Cache holding values for ttl.

func (*Cache[T]) Clear

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

Clear forgets the value, so the next Get loads it again.

func (*Cache[T]) Get

func (c *Cache[T]) Get(load func() (T, error)) (val T, stale bool, err error)

Get returns the cached value while it is fresh, and otherwise the result of load — run once however many callers are waiting for it.

When load fails, Get returns its error along with the last value it held, if any: stale is usually better than nothing for a caller that can say so, and stale reports which it got.

A load is tied to the generation it began in. One still running when Clear is called neither stores its result nor serves callers that arrive after the Clear: those start a load of their own, so a request made after a write never gets an answer read before it.

Jump to

Keyboard shortcuts

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