Files
geo/server/internal/tenant/repository/template_cache_repo.go
T
root 111498a65f feat: Refactor template handling and brand management
- Removed schema_json from article templates and related queries.
- Updated brand service to include website field in requests and responses.
- Simplified template wizard view by eliminating unused schema fields and related logic.
- Added tests for ark text format handling.
- Created migrations for dropping schema_json and adding website to brands.
2026-04-02 11:38:08 +08:00

64 lines
1.5 KiB
Go

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
}