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 ¶
- Variables
- type CachedConn
- func (cc CachedConn) DelCacheCtx(ctx context.Context, keys ...string) error
- func (cc CachedConn) ExecCtx(ctx context.Context, execCtx ExecCtxFn, keys ...string) error
- func (cc CachedConn) ExecNoCacheCtx(ctx context.Context, execCtx ExecCtxFn) (err error)
- func (cc CachedConn) GetCacheCtx(ctx context.Context, key string, v any) error
- func (cc CachedConn) QueryCtx(ctx context.Context, v any, key string, query QueryCtxFn) (err error)
- func (cc CachedConn) QueryNoCacheCtx(ctx context.Context, v any, query QueryCtxFn) (err error)
- func (cc CachedConn) SetCacheCtx(ctx context.Context, key string, v any) error
- func (cc CachedConn) TransactCtx(ctx context.Context, fn func(db *gorm.DB) error, opts ...*sql.TxOptions) error
- type ExecCtxFn
- type InvalidationQueue
- type InvalidationRetrier
- type Option
- type Options
- type QueryCtxFn
Constants ¶
This section is empty.
Variables ¶
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 (CachedConn) DelCacheCtx ¶
func (cc CachedConn) DelCacheCtx(ctx context.Context, keys ...string) error
DelCacheCtx deletes cache with keys.
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
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
SetCacheCtx sets cache with key, value, and context.
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
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.