Documentation
¶
Index ¶
- Variables
- func DataChangeNotify(drv *Driver) ent.Hook
- func Evict(ctx context.Context) context.Context
- func NewContext(ctx context.Context) context.Context
- func Skip(ctx context.Context) context.Context
- func SkipNotFound(ctx context.Context) context.Context
- func WithEntryKey(ctx context.Context, typ string, id any) context.Context
- func WithKey(ctx context.Context, key Key) context.Context
- func WithTTL(ctx context.Context, ttl time.Duration) context.Context
- type AddGetDeleter
- type Cache
- type ChangeSet
- type Driver
- func (d *Driver) ExecContext(ctx context.Context, query string, args ...any) (stdsql.Result, error)
- func (d *Driver) Query(ctx context.Context, query string, args, v any) error
- func (d *Driver) QueryContext(ctx context.Context, query string, args ...any) (*stdsql.Rows, error)
- func (d *Driver) Stats() Stats
- type Entry
- type Invalidator
- type Key
- type Option
- type Options
- type StampedeLocker
- type Stats
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("entcache: entry was not found")
ErrNotFound is returned by Get when an Entry does not exist in the cache.
var ErrRetryLocker = errors.New("entcache: retry locker")
ErrRetryLocker is a sentinel error used to trigger a stampede lock retry loop.
Functions ¶
func DataChangeNotify ¶
DataChangeNotify returns an ent Hook that marks changed entity keys in the Driver's ChangeSet whenever a mutation (create, update, delete) is committed. This enables automatic cache invalidation for key-addressed queries.
Usage:
drv := entcache.NewDriver(sqlDrv, entcache.WithChangeSet(cs)) client := ent.NewClient(ent.Driver(drv)) client.Use(entcache.DataChangeNotify(drv))
func Evict ¶
Evict returns a new Context that tells the Driver to skip and invalidate the cache entry on Query.
client.T.Query().All(entcache.Evict(ctx))
func NewContext ¶
NewContext returns a new Context that carries a request-scoped memory cache.
func Skip ¶
Skip returns a new Context that tells the Driver to skip the cache entry on Query.
client.T.Query().All(entcache.Skip(ctx))
func SkipNotFound ¶
SkipNotFound returns a new Context that tells the Driver to skip caching when the query result contains zero rows. This prevents caching empty results for entities that may be created shortly after.
client.User.Get(entcache.SkipNotFound(ctx), 42)
func WithEntryKey ¶
WithEntryKey returns a new Context with a structured entity key (e.g. "User:42") and marks the query as key-addressed. Key-addressed queries are eligible for the longer KeyTTL and precise invalidation via ChangeSet.
client.User.Get(entcache.WithEntryKey(ctx, "User", 42), 42)
Types ¶
type AddGetDeleter ¶
type AddGetDeleter interface {
Del(ctx context.Context, k Key) error
Add(ctx context.Context, k Key, e *Entry, ttl time.Duration) error
Get(ctx context.Context, k Key) (*Entry, error)
}
AddGetDeleter defines the interface for getting, adding and deleting entries from the cache.
type Cache ¶ added in v0.3.0
type Cache interface {
AddGetDeleter
}
Cache combines AddGetDeleter with optional StampedeLocker and Invalidator.
type ChangeSet ¶
type ChangeSet struct {
// contains filtered or unexported fields
}
ChangeSet tracks entity keys that have been modified (created, updated, or deleted). It is used by the Driver to detect stale cache entries and force re-queries. A background GC goroutine prunes entries older than the GC interval.
func NewChangeSet ¶
NewChangeSet creates a new ChangeSet with the given GC interval. If gcInterval is <= 0, the default of 5 minutes is used.
func (*ChangeSet) Changed ¶
Changed reports whether the given key has been marked as changed since the given time. This is used by the Driver to decide whether a cache hit should be evicted and re-fetched.
func (*ChangeSet) Clear ¶
Clear removes the change markers for the given keys, acknowledging that the cache has been refreshed.
type Driver ¶
A Driver is an SQL cached client. Users should use the constructor below for creating new driver.
func NewDriver ¶
NewDriver returns a new Driver given an existing driver and optional configuration functions.
func (*Driver) ExecContext ¶
ExecContext calls ExecContext of underlying driver.
func (*Driver) QueryContext ¶
QueryContext calls QueryContext of underlying driver.
type Entry ¶
Entry defines an entry to store in a cache.
func (Entry) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (*Entry) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
type Invalidator ¶ added in v0.3.0
type Invalidator interface {
// WatchInvalidations registers a callback that triggers when a key is modified or deleted remotely.
WatchInvalidations(ctx context.Context, onInvalidate func(key Key)) error
}
Invalidator defines an interface for real-time invalidation event streaming.
type Key ¶
type Key any
A Key defines a comparable Go value. See http://golang.org/ref/spec#Comparison_operators
func DefaultHash ¶
DefaultHash provides the default implementation for converting a query + args to a cache key.
func NewEntryKey ¶
NewEntryKey constructs a structured cache key from an entity type name and ID. This produces keys like "User:42" that enable precise invalidation via ChangeSet.
type Option ¶
type Option func(*Options)
Option allows configuring the cache driver using functional options.
func ContextLevel ¶
func ContextLevel() Option
ContextLevel configures the driver to work with context/request level cache.
func WithChangeSet ¶
WithChangeSet configures the Driver to use the given ChangeSet for mutation-aware cache invalidation.
func WithKeyTTL ¶
WithKeyTTL configures a separate TTL for key-addressed queries (e.g. Get-by-ID).
type Options ¶
type Options struct {
// TTL defines the period of time that an Entry
// is valid in the cache (used for hash-addressed queries).
TTL time.Duration
// KeyTTL defines the period of time that a key-addressed Entry
// (e.g. Get-by-ID queries) is valid in the cache. Key-addressed
// queries can have a longer TTL because they are precisely
// invalidated via the ChangeSet. If zero, TTL is used.
KeyTTL time.Duration
// Cache defines the GetAddDeleter (cache implementation)
// for holding the cache entries.
Cache AddGetDeleter
// Hash defines an optional Hash function for converting
// a query and its arguments to a cache key. If no Hash
// function was provided, the DefaultHash is used.
Hash func(query string, args []any) (Key, error)
// ChangeSet holds the mutation change tracker. When set,
// the Driver checks whether cached entries have been
// invalidated by mutations before returning them.
ChangeSet *ChangeSet
// Logf function. If provided, the Driver will call it with
// errors that can not be handled.
Log func(...any)
}
Options wraps the basic configuration cache options.
type StampedeLocker ¶ added in v0.3.0
type StampedeLocker interface {
// LockOrWait attempts to acquire permission to load a missing key.
// If won == true: caller runs the DB query, calls Add(), then calls release(ctx).
// If won == false: wait(ctx) blocks until another caller populates the cache and returns the Entry.
LockOrWait(ctx context.Context, k Key) (won bool, wait func(context.Context) (*Entry, error), release func(context.Context), err error)
}
StampedeLocker defines an interface for distributed or local lock-waiting on cache misses to prevent cache stampedes.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package natscache provides a NATS JetStream KeyValue cache backend for entcache.
|
Package natscache provides a NATS JetStream KeyValue cache backend for entcache. |
|
Package rediscache provides a Redis cache backend for entcache using go-redis.
|
Package rediscache provides a Redis cache backend for entcache using go-redis. |
|
Package rueidiscache provides a Redis cache backend for entcache using Rueidis.
|
Package rueidiscache provides a Redis cache backend for entcache using Rueidis. |

