tercul-backend/internal/data/cache/cached_work_repository.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

280 lines
8.7 KiB
Go

package cache
import (
"context"
"time"
"tercul/internal/domain"
platform_cache "tercul/internal/platform/cache"
"github.com/google/uuid"
"gorm.io/gorm"
)
type CachedWorkRepository struct {
inner domain.WorkRepository
opt Options
}
func NewCachedWorkRepository(inner domain.WorkRepository, c platform_cache.Cache, opt *Options) *CachedWorkRepository {
resolved := DefaultOptions(c)
if opt != nil {
resolved = *opt
if resolved.Cache == nil {
resolved.Cache = c
}
if resolved.Keys == nil {
resolved.Keys = platform_cache.NewDefaultKeyGenerator("tercul:repo:")
}
if resolved.EntityTTL == 0 {
resolved.EntityTTL = 1 * time.Hour
}
if resolved.ListTTL == 0 {
resolved.ListTTL = 5 * time.Minute
}
}
return &CachedWorkRepository{inner: inner, opt: resolved}
}
func (r *CachedWorkRepository) Create(ctx context.Context, entity *domain.Work) error {
err := r.inner.Create(ctx, entity)
if err == nil {
invalidateEntityType(ctx, r.opt.Cache, "work")
}
return err
}
func (r *CachedWorkRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Work) error {
err := r.inner.CreateInTx(ctx, tx, entity)
if err == nil {
invalidateEntityType(ctx, r.opt.Cache, "work")
}
return err
}
func (r *CachedWorkRepository) GetByID(ctx context.Context, id uuid.UUID) (*domain.Work, error) {
if r.opt.Enabled && r.opt.Cache != nil {
var cached domain.Work
key := r.opt.Keys.EntityKey("work", id)
if err := r.opt.Cache.Get(ctx, key, &cached); err == nil {
return &cached, nil
}
}
work, err := r.inner.GetByID(ctx, id)
if err != nil {
return nil, err
}
if r.opt.Enabled && r.opt.Cache != nil && work != nil {
_ = r.opt.Cache.Set(ctx, r.opt.Keys.EntityKey("work", id), work, r.opt.EntityTTL)
}
return work, nil
}
func (r *CachedWorkRepository) GetByIDWithOptions(ctx context.Context, id uuid.UUID, options *domain.QueryOptions) (*domain.Work, error) {
// Options can include varying preloads/where/order; avoid caching to prevent incorrect results.
return r.inner.GetByIDWithOptions(ctx, id, options)
}
func (r *CachedWorkRepository) Update(ctx context.Context, entity *domain.Work) error {
err := r.inner.Update(ctx, entity)
if err == nil {
if r.opt.Cache != nil {
_ = r.opt.Cache.Delete(ctx, r.opt.Keys.EntityKey("work", entity.ID))
}
invalidateEntityType(ctx, r.opt.Cache, "work")
}
return err
}
func (r *CachedWorkRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Work) error {
err := r.inner.UpdateInTx(ctx, tx, entity)
if err == nil {
if r.opt.Cache != nil {
_ = r.opt.Cache.Delete(ctx, r.opt.Keys.EntityKey("work", entity.ID))
}
invalidateEntityType(ctx, r.opt.Cache, "work")
}
return err
}
func (r *CachedWorkRepository) Delete(ctx context.Context, id uuid.UUID) error {
err := r.inner.Delete(ctx, id)
if err == nil {
if r.opt.Cache != nil {
_ = r.opt.Cache.Delete(ctx, r.opt.Keys.EntityKey("work", id))
}
invalidateEntityType(ctx, r.opt.Cache, "work")
}
return err
}
func (r *CachedWorkRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uuid.UUID) error {
err := r.inner.DeleteInTx(ctx, tx, id)
if err == nil {
if r.opt.Cache != nil {
_ = r.opt.Cache.Delete(ctx, r.opt.Keys.EntityKey("work", id))
}
invalidateEntityType(ctx, r.opt.Cache, "work")
}
return err
}
func (r *CachedWorkRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
if r.opt.Enabled && r.opt.Cache != nil {
var cached domain.PaginatedResult[domain.Work]
key := r.opt.Keys.ListKey("work", page, pageSize)
if err := r.opt.Cache.Get(ctx, key, &cached); err == nil {
return &cached, nil
}
}
res, err := r.inner.List(ctx, page, pageSize)
if err != nil {
return nil, err
}
if r.opt.Enabled && r.opt.Cache != nil && res != nil {
_ = r.opt.Cache.Set(ctx, r.opt.Keys.ListKey("work", page, pageSize), res, r.opt.ListTTL)
}
return res, nil
}
func (r *CachedWorkRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.Work, error) {
return r.inner.ListWithOptions(ctx, options)
}
func (r *CachedWorkRepository) ListAll(ctx context.Context) ([]domain.Work, error) {
return r.inner.ListAll(ctx)
}
func (r *CachedWorkRepository) Count(ctx context.Context) (int64, error) {
return r.inner.Count(ctx)
}
func (r *CachedWorkRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
return r.inner.CountWithOptions(ctx, options)
}
func (r *CachedWorkRepository) FindWithPreload(ctx context.Context, preloads []string, id uuid.UUID) (*domain.Work, error) {
return r.inner.FindWithPreload(ctx, preloads, id)
}
func (r *CachedWorkRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.Work, error) {
return r.inner.GetAllForSync(ctx, batchSize, offset)
}
func (r *CachedWorkRepository) Exists(ctx context.Context, id uuid.UUID) (bool, error) {
return r.inner.Exists(ctx, id)
}
func (r *CachedWorkRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
return r.inner.BeginTx(ctx)
}
func (r *CachedWorkRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
return r.inner.WithTx(ctx, fn)
}
func (r *CachedWorkRepository) FindByTitle(ctx context.Context, title string) ([]domain.Work, error) {
return r.inner.FindByTitle(ctx, title)
}
func (r *CachedWorkRepository) FindByAuthor(ctx context.Context, authorID uuid.UUID) ([]domain.Work, error) {
return r.inner.FindByAuthor(ctx, authorID)
}
func (r *CachedWorkRepository) FindByCategory(ctx context.Context, categoryID uuid.UUID) ([]domain.Work, error) {
return r.inner.FindByCategory(ctx, categoryID)
}
func (r *CachedWorkRepository) FindByLanguage(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
if r.opt.Enabled && r.opt.Cache != nil {
var cached domain.PaginatedResult[domain.Work]
key := r.opt.Keys.QueryKey("work", "lang", language, page, pageSize)
if err := r.opt.Cache.Get(ctx, key, &cached); err == nil {
return &cached, nil
}
}
res, err := r.inner.FindByLanguage(ctx, language, page, pageSize)
if err != nil {
return nil, err
}
if r.opt.Enabled && r.opt.Cache != nil && res != nil {
_ = r.opt.Cache.Set(ctx, r.opt.Keys.QueryKey("work", "lang", language, page, pageSize), res, r.opt.ListTTL)
}
return res, nil
}
func (r *CachedWorkRepository) GetWithTranslations(ctx context.Context, id uuid.UUID) (*domain.Work, error) {
if r.opt.Enabled && r.opt.Cache != nil {
var cached domain.Work
key := r.opt.Keys.QueryKey("work", "withTranslations", id)
if err := r.opt.Cache.Get(ctx, key, &cached); err == nil {
return &cached, nil
}
}
work, err := r.inner.GetWithTranslations(ctx, id)
if err != nil {
return nil, err
}
if r.opt.Enabled && r.opt.Cache != nil && work != nil {
_ = r.opt.Cache.Set(ctx, r.opt.Keys.QueryKey("work", "withTranslations", id), work, r.opt.EntityTTL)
}
return work, nil
}
func (r *CachedWorkRepository) GetWithAssociations(ctx context.Context, id uuid.UUID) (*domain.Work, error) {
if r.opt.Enabled && r.opt.Cache != nil {
var cached domain.Work
key := r.opt.Keys.QueryKey("work", "withAssociations", id)
if err := r.opt.Cache.Get(ctx, key, &cached); err == nil {
return &cached, nil
}
}
work, err := r.inner.GetWithAssociations(ctx, id)
if err != nil {
return nil, err
}
if r.opt.Enabled && r.opt.Cache != nil && work != nil {
_ = r.opt.Cache.Set(ctx, r.opt.Keys.QueryKey("work", "withAssociations", id), work, r.opt.EntityTTL)
}
return work, nil
}
func (r *CachedWorkRepository) GetWithAssociationsInTx(ctx context.Context, tx *gorm.DB, id uuid.UUID) (*domain.Work, error) {
// Tx-scoped reads should bypass cache.
return r.inner.GetWithAssociationsInTx(ctx, tx, id)
}
func (r *CachedWorkRepository) ListWithTranslations(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
if r.opt.Enabled && r.opt.Cache != nil {
var cached domain.PaginatedResult[domain.Work]
key := r.opt.Keys.QueryKey("work", "listWithTranslations", page, pageSize)
if err := r.opt.Cache.Get(ctx, key, &cached); err == nil {
return &cached, nil
}
}
res, err := r.inner.ListWithTranslations(ctx, page, pageSize)
if err != nil {
return nil, err
}
if r.opt.Enabled && r.opt.Cache != nil && res != nil {
_ = r.opt.Cache.Set(ctx, r.opt.Keys.QueryKey("work", "listWithTranslations", page, pageSize), res, r.opt.ListTTL)
}
return res, nil
}
func (r *CachedWorkRepository) IsAuthor(ctx context.Context, workID uuid.UUID, authorID uuid.UUID) (bool, error) {
return r.inner.IsAuthor(ctx, workID, authorID)
}
func (r *CachedWorkRepository) ListByCollectionID(ctx context.Context, collectionID uuid.UUID) ([]domain.Work, error) {
return r.inner.ListByCollectionID(ctx, collectionID)
}