Files
geo/server/internal/tenant/repository/template_cache_repo.go
T
root 1b01caac0f feat(prompts): add prompts loader and configuration management
- Implemented a new prompts loader in `loader.go` to manage prompt templates and configurations.
- Introduced caching mechanism for prompt configurations to optimize loading.
- Added functions to apply platform-specific prompt template overrides.
- Created unit tests in `loader_test.go` to validate prompt configuration loading and reloading behavior.
- Ensured that the last valid configuration is retained in case of errors during reload.
2026-04-13 16:08:12 +08:00

72 lines
1.8 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 {
for i := range records {
applyPlatformTemplatePromptOverrides(&records[i])
}
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)
}
for i := range records {
applyPlatformTemplatePromptOverrides(&records[i])
}
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 {
applyPlatformTemplatePromptOverrides(&record)
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)
}
applyPlatformTemplatePromptOverrides(record)
return record, nil
}