cache

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cache keeps values that are slow to make: load results, pages rendered on demand, answers from other services. A Cache sits on a Store, in memory, on disk or in Redis; with Redis or a shared disk, every process of the app shares it.

c := cache.New(cache.Memory(64 << 20))
posts, err := cache.Get(ctx, c, "posts:recent", cache.For(time.Minute, "posts"), func(ctx context.Context) ([]Post, error) {
	return db.RecentPosts(ctx)
})
...
err = c.Invalidate(ctx, "posts") // after a post changes

Entries carry tags. Invalidating a tag makes every entry with it stale at once, in every process: each tag has a version in the store, and an entry is fresh only while its tags' versions are the ones it was made with.

While an entry is made, other callers for the same key in the process wait for it instead of making it too.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](ctx context.Context, c *Cache, key string, o Options, fn func(ctx context.Context) (T, error)) (T, error)

Get returns the cached value of key, decoded from JSON, or calls fn, stores its result as JSON, and returns it. An error from fn is returned and nothing is stored.

func NoStore

func NoStore() error

NoStore is what fn returns with a value to be served but not stored, such as an error page.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache keeps entries in a Store, with tags. Create it with New.

func New

func New(store Store) *Cache

New returns a cache on store.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, keys ...string) error

Delete removes entries.

func (*Cache) Fetch

func (c *Cache) Fetch(ctx context.Context, key string, o Options, fn func(ctx context.Context) ([]byte, error)) ([]byte, error)

Fetch returns the entry at key if it's fresh; otherwise it calls fn, stores what it returns, and returns that. A stale entry within o.Stale is returned at once while fn runs in the background. Callers for the same key in this process share one call of fn.

func (*Cache) Invalidate

func (c *Cache) Invalidate(ctx context.Context, tags ...string) error

Invalidate makes every entry with any of tags stale, in every process that shares the store.

func (*Cache) Lookup

func (c *Cache) Lookup(ctx context.Context, key string) (Entry, bool, error)

Lookup returns the entry at key: fresh, stale, or ok false if there's none.

func (*Cache) Make

func (c *Cache) Make(ctx context.Context, key string, o Options, fn func(ctx context.Context) ([]byte, []string, error)) ([]byte, error)

Make makes the entry at key with fn and stores it, whether or not one is there, with o.Tags and the tags fn returns: those found while making it, such as what a page's loads read. Callers for the same key in this process share one call of fn.

func (*Cache) Put

func (c *Cache) Put(ctx context.Context, key string, value []byte, o Options) error

Put stores value at key. The store keeps it for TTL plus Stale, so a stale entry can be served while a fresh one is made.

func (*Cache) Store

func (c *Cache) Store() Store

Store returns the cache's store.

type Entry

type Entry struct {
	Value []byte
	// Made is when it was stored.
	Made time.Time
	// Fresh is false once its TTL passed or a tag was invalidated.
	Fresh bool
	Tags  []string
}

Entry is a stored value with when it was made and its tags' versions.

type Options

type Options struct {
	// TTL is how long the entry is fresh; 0 is until a tag is invalidated.
	TTL time.Duration
	// Stale is how long after TTL the entry may still be served while a
	// fresh one is made in the background: stale-while-revalidate.
	Stale time.Duration
	Tags  []string
}

Options say how long an entry is fresh, and its tags.

func For

func For(ttl time.Duration, tags ...string) Options

For is Options with a TTL and tags.

type Store

type Store interface {
	// Get returns the value of key, or ok false if there's none or it
	// expired.
	Get(ctx context.Context, key string) (value []byte, ok bool, err error)
	// Set stores value for ttl; 0 keeps it until it's evicted.
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	// Delete removes keys.
	Delete(ctx context.Context, keys ...string) error
	// Incr adds one to the number at key, starting from 0, and returns it.
	Incr(ctx context.Context, key string) (int64, error)
}

Store keeps bytes by key. Memory, Disk and Redis are stores; a Store of your own works too.

func Disk

func Disk(dir string) (Store, error)

Disk returns a store of files in dir, one per key, which processes on the same machine or a shared volume share. Expired files are removed as they're read.

func Memory

func Memory(maxBytes int64) Store

Memory returns a store in this process's memory, keeping at most maxBytes of values and evicting the least recently used first. It isn't shared between processes: each has its own.

Directories

Path Synopsis
Package redis is a cache.Store in Redis, or anything that speaks its protocol (Valkey, KeyDB, Dragonfly), shared by every process of the app.
Package redis is a cache.Store in Redis, or anything that speaks its protocol (Valkey, KeyDB, Dragonfly), shared by every process of the app.

Jump to

Keyboard shortcuts

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