caches

package module
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const IdentifierPrefix = "gorm-caches::"

Variables

This section is empty.

Functions

func ExtractEntityIDs added in v0.8.1

func ExtractEntityIDs(db *gorm.DB) []interface{}

ExtractEntityIDs returns the primary key values from the current GORM statement.

func ExtractTables added in v0.8.1

func ExtractTables(db *gorm.DB) []string

ExtractTables returns the table names referenced in the current GORM statement.

func SetPointedValue

func SetPointedValue(dest interface{}, src interface{})

func TagsFromContext

func TagsFromContext(ctx context.Context) []string

TagsFromContext returns the cache tags stored on ctx by WithTags. Cacher implementations use this in Store to build tag→key index entries.

func WithInvalidateTags

func WithInvalidateTags(ctx context.Context, tags ...string) context.Context

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.

func WithTags

func WithTags(ctx context.Context, tags ...string) context.Context

WithTags associates cache tags with the context, merging with any existing tags. Used by both the caching system (TagsFunc) and application code at query sites.

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 Caches

type Caches struct {
	Conf *Config
	// contains filtered or unexported fields
}

func (*Caches) Initialize

func (c *Caches) Initialize(db *gorm.DB) error

func (*Caches) Name

func (c *Caches) Name() string

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

type Query

type Query[T any] struct {
	Dest         T
	RowsAffected int64
}

func (*Query[T]) Marshal

func (q *Query[T]) Marshal() ([]byte, error)

func (*Query[T]) Unmarshal

func (q *Query[T]) Unmarshal(bytes []byte) error

Jump to

Keyboard shortcuts

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