package repository import ( "context" "encoding/json" "fmt" "time" "github.com/geo-platform/tenant-api/internal/shared/cache" ) const templateCacheTTL = 5 * time.Minute type cachedTemplateRepository struct { inner TemplateRepository cache cache.Cache } func NewCachedTemplateRepository(inner TemplateRepository, c cache.Cache) TemplateRepository { return &cachedTemplateRepository{inner: inner, cache: c} } func (r *cachedTemplateRepository) ListTemplates(ctx context.Context, tenantID int64) ([]TemplateRecord, error) { key := fmt.Sprintf("tmpl_list:%d", tenantID) if data, err := r.cache.Get(ctx, key); err == nil { var records []TemplateRecord if err := json.Unmarshal(data, &records); err == nil { return records, nil } } records, err := r.inner.ListTemplates(ctx, tenantID) if err != nil { return nil, err } if data, err := json.Marshal(records); err == nil { _ = r.cache.Set(ctx, key, data, templateCacheTTL) } return records, nil } func (r *cachedTemplateRepository) GetTemplateByID(ctx context.Context, id, tenantID int64) (*TemplateRecord, error) { key := fmt.Sprintf("tmpl:%d:%d", tenantID, id) if data, err := r.cache.Get(ctx, key); err == nil { var record TemplateRecord if err := json.Unmarshal(data, &record); err == nil { return &record, nil } } record, err := r.inner.GetTemplateByID(ctx, id, tenantID) if err != nil { return nil, err } if data, err := json.Marshal(record); err == nil { _ = r.cache.Set(ctx, key, data, templateCacheTTL) } return record, nil }