Documentation
¶
Index ¶
- Constants
- func ExtractEntityIDs(db *gorm.DB) []interface{}
- func ExtractTables(db *gorm.DB) []string
- func SetPointedValue(dest interface{}, src interface{})
- func TagsFromContext(ctx context.Context) []string
- func WithInvalidateTags(ctx context.Context, tags ...string) context.Context
- func WithTags(ctx context.Context, tags ...string) context.Context
- type Cacher
- type Caches
- type Config
- type InvalidationEvent
- type MutationType
- type Query
Constants ¶
View Source
const IdentifierPrefix = "gorm-caches::"
Variables ¶
This section is empty.
Functions ¶
func ExtractEntityIDs ¶ added in v0.8.1
ExtractEntityIDs returns the primary key values from the current GORM statement.
func ExtractTables ¶ added in v0.8.1
ExtractTables returns the table names referenced in the current GORM statement.
func SetPointedValue ¶
func SetPointedValue(dest interface{}, src interface{})
func TagsFromContext ¶
TagsFromContext returns the cache tags stored on ctx by WithTags. Cacher implementations use this in Store to build tag→key index entries.
func WithInvalidateTags ¶
WithInvalidateTags sets tags on the context to indicate which cache entries should be invalidated during a mutation. Use this in your application code before performing CREATE/UPDATE/DELETE operations.
Types ¶
type Cacher ¶
type Cacher interface {
// Get impl should check if a specific key exists in the cache and return its value
// look at Query.Marshal
Get(ctx context.Context, key string, q *Query[any]) (*Query[any], error)
// Store impl should store a cached representation of the val param
// look at Query.Unmarshal
Store(ctx context.Context, key string, val *Query[any]) error
// Invalidate impl should invalidate cached values based on the given event.
// The event contains metadata about the mutation (tables, entity IDs, mutation type, and tags).
// It will be called when INSERT / UPDATE / DELETE queries are sent to the DB.
Invalidate(ctx context.Context, event *InvalidationEvent) error
}
type Config ¶
type Config struct {
Easer bool
Cacher Cacher
TagsFunc func(db *gorm.DB) []string
// SkipFunc, when set, is consulted on every SELECT before the cache
// lookup. Returning true bypasses the plugin entirely for that query:
// no cache read, no cache write and no easer dedup — the query always
// hits the database. Statement.SQL and Statement.Table are already
// resolved when it is called.
//
// Note: returning nil from TagsFunc does NOT skip caching — it only
// stores the entry untagged (and therefore not invalidatable by tag).
// Use SkipFunc to exclude tables from the cache.
SkipFunc func(db *gorm.DB) bool
Prefix string
}
type InvalidationEvent ¶
type InvalidationEvent struct {
Tables []string
EntityIDs []interface{}
MutationType MutationType
Tags []string
}
InvalidationEvent contains metadata about a mutation that triggered cache invalidation.
type MutationType ¶
type MutationType int
MutationType represents the type of mutation that triggered cache invalidation.
const ( MutationCreate MutationType = iota MutationUpdate MutationDelete )
func (MutationType) String ¶
func (m MutationType) String() string
Click to show internal directories.
Click to hide internal directories.