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

cache

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package cache is the read-through Redis cache of the GORM repositories. CachedConn runs a repository's queries and writes: a cached query reads Redis first and fills it from the database on a miss (a missing record is remembered briefly too), and a write invalidates the keys it names. A per-key version fence keeps a read that raced a committed write from putting the old value back, and inside a transaction invalidations wait in an InvalidationQueue until the transaction commits, so no entry is filled from uncommitted data.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is the error when cache not found.
	ErrNotFound = redis.Nil
)

Functions

This section is empty.

Types

type CachedConn

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

CachedConn is a database connection with the cache in front of it; the zero value is not usable, see NewConn.

func NewConn

func NewConn(db *gorm.DB, c *redis.Client, opts ...Option) CachedConn

NewConn returns a CachedConn with a redis cluster cache.

func (CachedConn) DelCacheCtx

func (cc CachedConn) DelCacheCtx(ctx context.Context, keys ...string) error

DelCacheCtx deletes cache with keys.

func (CachedConn) ExecCtx

func (cc CachedConn) ExecCtx(ctx context.Context, execCtx ExecCtxFn, keys ...string) error

ExecCtx runs given exec on given keys, and returns execution result.

func (CachedConn) ExecNoCacheCtx

func (cc CachedConn) ExecNoCacheCtx(ctx context.Context, execCtx ExecCtxFn) (err error)

ExecNoCacheCtx runs exec with given sql statement, without affecting cache.

func (CachedConn) GetCacheCtx added in v1.2.15

func (cc CachedConn) GetCacheCtx(ctx context.Context, key string, v any) error

GetCacheCtx unmarshals cache with given key and context into v.

func (CachedConn) QueryCtx

func (cc CachedConn) QueryCtx(ctx context.Context, v any, key string, query QueryCtxFn) (err error)

QueryCtx fills v from the cache entry key, or on a miss by running query and caching its result; a query that finds no record is remembered for the not-found expiry, so repeated lookups of a missing key stay off the database. Redis failures fall back to the database, and so does every read inside a transaction, which must see the transaction's own writes.

func (CachedConn) QueryNoCacheCtx

func (cc CachedConn) QueryNoCacheCtx(ctx context.Context, v any, query QueryCtxFn) (err error)

QueryNoCacheCtx runs query with given sql statement, without affecting cache.

func (CachedConn) SetCacheCtx added in v1.2.15

func (cc CachedConn) SetCacheCtx(ctx context.Context, key string, v any) error

SetCacheCtx sets cache with key, value, and context.

func (CachedConn) TransactCtx

func (cc CachedConn) TransactCtx(ctx context.Context, fn func(db *gorm.DB) error, opts ...*sql.TxOptions) error

TransactCtx runs given fn in transaction mode.

type ExecCtxFn

type ExecCtxFn func(conn *gorm.DB) error

ExecCtxFn defines the sql exec method.

type InvalidationQueue added in v1.14.12

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

InvalidationQueue deduplicates cache keys that must be deleted after a database transaction commits. It is safe for use by repositories that share one Store transaction.

func NewInvalidationQueue added in v1.14.12

func NewInvalidationQueue() *InvalidationQueue

NewInvalidationQueue returns an empty queue.

func (*InvalidationQueue) Add added in v1.14.12

func (q *InvalidationQueue) Add(keys ...string)

Add queues keys; empty keys are ignored.

func (*InvalidationQueue) Flush added in v1.14.12

func (q *InvalidationQueue) Flush(ctx context.Context, client *redis.Client) error

Flush invalidates the queued keys through client. The keys stay queued when it fails, so the caller can hand them to an InvalidationRetrier.

func (*InvalidationQueue) Keys added in v1.14.12

func (q *InvalidationQueue) Keys() []string

Keys returns a snapshot of pending invalidations. It is intended for handing failed post-commit work to the shared retry worker.

type InvalidationRetrier added in v1.14.12

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

InvalidationRetrier coalesces failed post-commit invalidations into one bounded worker. It starts only while work is pending, so Redis outages do not create one goroutine per committed transaction.

func NewInvalidationRetrier added in v1.14.12

func NewInvalidationRetrier(client *redis.Client) *InvalidationRetrier

NewInvalidationRetrier returns a retrier that invalidates through client.

func (*InvalidationRetrier) Enqueue added in v1.14.12

func (r *InvalidationRetrier) Enqueue(keys ...string)

Enqueue schedules keys for invalidation and starts the worker unless it is running; it does not wait for Redis.

type Option added in v1.0.1

type Option func(o *Options)

Option defines the method to customize an Options.

func WithInvalidationQueue added in v1.14.12

func WithInvalidationQueue(queue *InvalidationQueue) Option

WithInvalidationQueue defers cache-key invalidation until the owner flushes the queue. It is used by database transactions so cache entries cannot be repopulated from data that has not committed yet.

type Options added in v1.0.1

type Options struct {
	Expiry         time.Duration
	NotFoundExpiry time.Duration
	Invalidations  *InvalidationQueue
}

Options is used to store the cache options.

type QueryCtxFn

type QueryCtxFn func(conn *gorm.DB, v any) error

QueryCtxFn defines the query method.

Jump to

Keyboard shortcuts

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