Documentation
¶
Index ¶
- type Entity
- type GenericRepository
- func (r *GenericRepository[T]) Count(ctx context.Context) (int64, bool, bool, error)
- func (r *GenericRepository[T]) Create(ctx context.Context, entity *T) (bool, error)
- func (r *GenericRepository[T]) CreateBatch(ctx context.Context, entities []*T) error
- func (r *GenericRepository[T]) Delete(ctx context.Context, id interface{}) (bool, error)
- func (r *GenericRepository[T]) Exists(ctx context.Context, id interface{}) (bool, bool, bool, error)
- func (r *GenericRepository[T]) FindAll(ctx context.Context) ([]T, bool, bool, error)
- func (r *GenericRepository[T]) FindByID(ctx context.Context, id interface{}) (*T, bool, bool, error)
- func (r *GenericRepository[T]) FindWhere(ctx context.Context, query interface{}, args ...interface{}) ([]T, bool, bool, error)
- func (r *GenericRepository[T]) First(ctx context.Context, query interface{}, args ...interface{}) (*T, bool, bool, error)
- func (r *GenericRepository[T]) InvalidateCache(ctx context.Context) error
- func (r *GenericRepository[T]) Joins(ctx context.Context, query string, args ...interface{}) Repository[T]
- func (r *GenericRepository[T]) Limit(ctx context.Context, limit int) Repository[T]
- func (r *GenericRepository[T]) Offset(ctx context.Context, offset int) Repository[T]
- func (r *GenericRepository[T]) Order(ctx context.Context, value interface{}) Repository[T]
- func (r *GenericRepository[T]) Preload(ctx context.Context, associations ...string) Repository[T]
- func (r *GenericRepository[T]) Update(ctx context.Context, entity *T) (bool, error)
- func (r *GenericRepository[T]) UpdateBatch(ctx context.Context, entities []*T) error
- func (r *GenericRepository[T]) WarmCache(ctx context.Context) error
- type RelatedEntity
- type RelationshipAware
- type Repository
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entity ¶
type Entity interface {
// TableName returns the database table name for this entity
// This should match GORM's table naming convention
// If not implemented, GORM's default naming will be used
TableName() string
// GetPrimaryKeyValue returns the actual value of the primary key
// Used for cache invalidation and dependency tracking
// If not implemented, reflection will be used to find 'ID' field
GetPrimaryKeyValue() interface{}
}
Entity interface defines the minimal contract for repository entities GORM models should implement this for optimal caching and relationship detection If not implemented, the repository will use reflection as fallback
type GenericRepository ¶
type GenericRepository[T Entity] struct { // contains filtered or unexported fields }
GenericRepository provides comprehensive CRUD operations with intelligent caching It automatically handles cache-first reads and relationship-aware invalidation
func (*GenericRepository[T]) Create ¶
func (r *GenericRepository[T]) Create(ctx context.Context, entity *T) (bool, error)
Create creates a new record with automatic cache invalidation
func (*GenericRepository[T]) CreateBatch ¶
func (r *GenericRepository[T]) CreateBatch(ctx context.Context, entities []*T) error
CreateBatch creates multiple records in batch with cache invalidation
func (*GenericRepository[T]) Delete ¶
func (r *GenericRepository[T]) Delete(ctx context.Context, id interface{}) (bool, error)
Delete deletes a record by ID with cache invalidation
func (*GenericRepository[T]) Exists ¶
func (r *GenericRepository[T]) Exists(ctx context.Context, id interface{}) (bool, bool, bool, error)
Exists checks if a record exists by ID
func (*GenericRepository[T]) FindByID ¶
func (r *GenericRepository[T]) FindByID(ctx context.Context, id interface{}) (*T, bool, bool, error)
FindByID finds a record by ID with cache-first strategy
func (*GenericRepository[T]) FindWhere ¶
func (r *GenericRepository[T]) FindWhere(ctx context.Context, query interface{}, args ...interface{}) ([]T, bool, bool, error)
FindWhere finds records with conditions and caching
func (*GenericRepository[T]) First ¶
func (r *GenericRepository[T]) First(ctx context.Context, query interface{}, args ...interface{}) (*T, bool, bool, error)
First finds the first record matching conditions
func (*GenericRepository[T]) InvalidateCache ¶
func (r *GenericRepository[T]) InvalidateCache(ctx context.Context) error
InvalidateCache invalidates all caches for this entity type in this database
func (*GenericRepository[T]) Joins ¶
func (r *GenericRepository[T]) Joins(ctx context.Context, query string, args ...interface{}) Repository[T]
Joins specifies joins to perform
func (*GenericRepository[T]) Limit ¶
func (r *GenericRepository[T]) Limit(ctx context.Context, limit int) Repository[T]
Limit specifies limit
func (*GenericRepository[T]) Offset ¶
func (r *GenericRepository[T]) Offset(ctx context.Context, offset int) Repository[T]
Offset specifies offset
func (*GenericRepository[T]) Order ¶
func (r *GenericRepository[T]) Order(ctx context.Context, value interface{}) Repository[T]
Order specifies ordering
func (*GenericRepository[T]) Preload ¶
func (r *GenericRepository[T]) Preload(ctx context.Context, associations ...string) Repository[T]
Preload specifies associations to preload (returns new repository instance)
func (*GenericRepository[T]) Update ¶
func (r *GenericRepository[T]) Update(ctx context.Context, entity *T) (bool, error)
Update updates a record with relationship-aware cache invalidation
func (*GenericRepository[T]) UpdateBatch ¶
func (r *GenericRepository[T]) UpdateBatch(ctx context.Context, entities []*T) error
UpdateBatch updates multiple records in batch with cache invalidation
type RelatedEntity ¶
type RelatedEntity struct {
EntityType string // The related entity type (table name)
EntityID interface{} // The related entity ID (nil for has_many without specific ID)
}
RelatedEntity represents a relationship to another entity
type RelationshipAware ¶
type RelationshipAware interface {
Entity
// GetRelationships returns a map of relationship types to related entity info
// Format: map[relationType][]RelatedEntity
// Example: {"belongs_to": [{"customer", customerID}], "has_many": [{"orders", nil}]}
GetRelationships() map[string][]RelatedEntity
}
RelationshipAware allows entities to define their relationships for cache invalidation When implemented, the repository can automatically invalidate related caches
type Repository ¶
type Repository[T any] interface { // Queries (Read Operations - Cache-First) // Returns: (result, cacheHit, cacheStored, error) // - cacheHit: true if data retrieved from Redis cache // - cacheStored: true if data successfully stored to Redis after DB query FindByID(ctx context.Context, id interface{}) (*T, bool, bool, error) FindAll(ctx context.Context) ([]T, bool, bool, error) FindWhere(ctx context.Context, query interface{}, args ...interface{}) ([]T, bool, bool, error) First(ctx context.Context, query interface{}, args ...interface{}) (*T, bool, bool, error) Count(ctx context.Context) (int64, bool, bool, error) Exists(ctx context.Context, id interface{}) (bool, bool, bool, error) // GORM Query Methods (Cached) Preload(ctx context.Context, associations ...string) Repository[T] Joins(ctx context.Context, query string, args ...interface{}) Repository[T] Order(ctx context.Context, value interface{}) Repository[T] Limit(ctx context.Context, limit int) Repository[T] Offset(ctx context.Context, offset int) Repository[T] // Commands (Write Operations - Relationship-Aware Cache Invalidation) // Returns: (cacheInvalidated, error) // - cacheInvalidated: true if related caches were successfully invalidated Create(ctx context.Context, entity *T) (bool, error) Update(ctx context.Context, entity *T) (bool, error) Delete(ctx context.Context, id interface{}) (bool, error) // Batch Operations CreateBatch(ctx context.Context, entities []*T) error UpdateBatch(ctx context.Context, entities []*T) error // Cache Management InvalidateCache(ctx context.Context) error WarmCache(ctx context.Context) error }
Repository defines the generic repository interface
func NewGenericRepository ¶
func NewGenericRepository[T Entity](dbManager *db.Manager, redisManager *redis.Manager) Repository[T]
NewGenericRepository creates a new generic repository with GORM and Redis integration
func NewGenericRepositoryDBOnly ¶
func NewGenericRepositoryDBOnly[T Entity](manager *db.Manager) Repository[T]
NewGenericRepositoryDBOnly creates a repository without Redis (database only) For cases where caching is not needed