2026-04-02 00:31:28 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-04-15 16:11:05 +08:00
|
|
|
"errors"
|
2026-04-02 00:31:28 +08:00
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/geo-platform/tenant-api/internal/shared/cache"
|
2026-04-15 16:11:05 +08:00
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
|
"golang.org/x/sync/singleflight"
|
2026-04-02 00:31:28 +08:00
|
|
|
)
|
|
|
|
|
|
2026-04-15 16:11:05 +08:00
|
|
|
const (
|
|
|
|
|
templateCacheTTL = 5 * time.Minute
|
|
|
|
|
templateCacheEmptyTTL = 1 * time.Minute
|
|
|
|
|
)
|
2026-04-02 00:31:28 +08:00
|
|
|
|
|
|
|
|
type cachedTemplateRepository struct {
|
|
|
|
|
inner TemplateRepository
|
|
|
|
|
cache cache.Cache
|
2026-04-15 16:11:05 +08:00
|
|
|
group singleflight.Group
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2026-04-15 16:11:05 +08:00
|
|
|
records, _, err := cache.LoadJSONWithEmpty(ctx, r.cache, &r.group, key, templateCacheTTL, templateCacheEmptyTTL, func(loadCtx context.Context) ([]TemplateRecord, bool, error) {
|
|
|
|
|
records, err := r.inner.ListTemplates(loadCtx, tenantID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false, err
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
2026-04-15 16:11:05 +08:00
|
|
|
return records, true, nil
|
|
|
|
|
})
|
2026-04-02 00:31:28 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-13 16:08:12 +08:00
|
|
|
for i := range records {
|
|
|
|
|
applyPlatformTemplatePromptOverrides(&records[i])
|
|
|
|
|
}
|
2026-04-02 00:31:28 +08:00
|
|
|
return records, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *cachedTemplateRepository) GetTemplateByID(ctx context.Context, id, tenantID int64) (*TemplateRecord, error) {
|
|
|
|
|
key := fmt.Sprintf("tmpl:%d:%d", tenantID, id)
|
|
|
|
|
|
2026-04-15 16:11:05 +08:00
|
|
|
record, found, err := cache.LoadJSONWithEmpty(ctx, r.cache, &r.group, key, templateCacheTTL, templateCacheEmptyTTL, func(loadCtx context.Context) (*TemplateRecord, bool, error) {
|
|
|
|
|
record, err := r.inner.GetTemplateByID(loadCtx, id, tenantID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, false, err
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
2026-04-15 16:11:05 +08:00
|
|
|
return record, true, nil
|
|
|
|
|
})
|
2026-04-02 00:31:28 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-15 16:11:05 +08:00
|
|
|
if !found || record == nil {
|
|
|
|
|
return nil, pgx.ErrNoRows
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
2026-04-13 16:08:12 +08:00
|
|
|
applyPlatformTemplatePromptOverrides(record)
|
2026-04-02 00:31:28 +08:00
|
|
|
return record, nil
|
|
|
|
|
}
|