b31d8d0096
- Added migration to harden task audit tracking by modifying audit_logs and related tables. - Introduced operator_id to several tables for better tracking of actions. - Updated article_templates with new prompt templates for various article types, enhancing content generation. - Created prompt_rules and schedule_tasks tables to manage content generation rules and scheduling. - Added foreign key constraints to articles for better data integrity.
68 lines
1.7 KiB
Go
68 lines
1.7 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
|
|
}
|
|
|
|
func (r *cachedTemplateRepository) GetTemplateSchema(ctx context.Context, id int64) (*TemplateSchemaRecord, error) {
|
|
return r.inner.GetTemplateSchema(ctx, id)
|
|
}
|