feat(cache): add read-through cache layer across all app services

Introduce a generic read-through caching infrastructure and wire it into
all major tenant app services to reduce database load on hot read paths.

Key changes:
- Add `DeletePrefix` to Cache interface with memory (prefix scan) and
  Redis (SCAN + DEL) implementations
- New `readthrough.go`: generic `LoadJSON` / `LoadJSONWithEmpty` helpers
  backed by singleflight to prevent cache stampedes; supports jittered TTL
- New `cache_support.go`: centralized cache key builders and invalidation
  helpers for all entities (workspace, brand, prompt rules, schedule tasks,
  articles)
- Wire optional cache into ArticleService, BrandService, WorkspaceService,
  PromptRuleService, ScheduleTaskService, TemplateService, MediaService,
  PromptGenerateService via `WithCache()` builder pattern
- ScheduleDispatchWorker invalidates schedule task cache after dispatching
- ArticleService gains a new `Detail` endpoint with empty-result caching
- Update cmd entrypoints and transport handlers to propagate cache
This commit is contained in:
2026-04-15 16:11:05 +08:00
parent 4d06938565
commit 1538a12042
28 changed files with 1316 additions and 634 deletions
+204 -168
View File
@@ -3,26 +3,36 @@ package app
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/sync/singleflight"
"github.com/geo-platform/tenant-api/internal/shared/auditlog"
"github.com/geo-platform/tenant-api/internal/shared/auth"
sharedcache "github.com/geo-platform/tenant-api/internal/shared/cache"
"github.com/geo-platform/tenant-api/internal/shared/middleware"
"github.com/geo-platform/tenant-api/internal/shared/response"
)
type PromptRuleService struct {
pool *pgxpool.Pool
auditLogs *auditlog.AsyncWriter
pool *pgxpool.Pool
auditLogs *auditlog.AsyncWriter
cache sharedcache.Cache
cacheGroup singleflight.Group
}
func NewPromptRuleService(pool *pgxpool.Pool, auditLogs *auditlog.AsyncWriter) *PromptRuleService {
return &PromptRuleService{pool: pool, auditLogs: auditLogs}
}
func (s *PromptRuleService) WithCache(c sharedcache.Cache) *PromptRuleService {
s.cache = c
return s
}
// --- Group types ---
type PromptRuleGroupRequest struct {
@@ -97,30 +107,9 @@ type PromptRuleSimple struct {
func (s *PromptRuleService) ListGroups(ctx context.Context) ([]PromptRuleGroupResponse, error) {
actor := auth.MustActor(ctx)
rows, err := s.pool.Query(ctx, `
SELECT id, name, sort_order, created_at
FROM prompt_rule_groups WHERE tenant_id = $1 AND deleted_at IS NULL
ORDER BY sort_order, created_at
`, actor.TenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list groups")
}
defer rows.Close()
var groups []PromptRuleGroupResponse
for rows.Next() {
var g PromptRuleGroupResponse
var ca interface{}
if err := rows.Scan(&g.ID, &g.Name, &g.SortOrder, &ca); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
g.CreatedAt = fmt.Sprintf("%v", ca)
groups = append(groups, g)
}
if groups == nil {
groups = []PromptRuleGroupResponse{}
}
return groups, nil
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleGroupsCacheKey(actor.TenantID), defaultCacheTTL(), func(loadCtx context.Context) ([]PromptRuleGroupResponse, error) {
return s.loadPromptRuleGroups(loadCtx, actor.TenantID)
})
}
func (s *PromptRuleService) CreateGroup(ctx context.Context, req PromptRuleGroupRequest) (*PromptRuleGroupResponse, error) {
@@ -155,6 +144,7 @@ func (s *PromptRuleService) CreateGroup(ctx context.Context, req PromptRuleGroup
Result: &result,
})
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, nil)
return &PromptRuleGroupResponse{ID: id, Name: req.Name, SortOrder: sortOrder, CreatedAt: fmt.Sprintf("%v", ca)}, nil
}
@@ -171,6 +161,7 @@ func (s *PromptRuleService) UpdateGroup(ctx context.Context, id int64, req Promp
if err != nil || tag.RowsAffected() == 0 {
return response.ErrNotFound(40430, "group_not_found", "prompt rule group not found")
}
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, nil)
return nil
}
@@ -206,6 +197,7 @@ func (s *PromptRuleService) DeleteGroup(ctx context.Context, id int64) error {
AfterJSON: afterJSON,
Result: &result,
})
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, nil)
return nil
}
@@ -213,148 +205,23 @@ func (s *PromptRuleService) DeleteGroup(ctx context.Context, id int64) error {
func (s *PromptRuleService) List(ctx context.Context, params PromptRuleListParams) (*PromptRuleListResponse, error) {
actor := auth.MustActor(ctx)
if params.Page < 1 {
params.Page = 1
}
if params.PageSize < 1 || params.PageSize > 100 {
params.PageSize = 20
}
offset := (params.Page - 1) * params.PageSize
var total int64
var rows interface{ Close() }
var queryErr error
if params.Ungrouped {
err := s.pool.QueryRow(ctx, `
SELECT COUNT(*) FROM prompt_rules
WHERE tenant_id = $1 AND deleted_at IS NULL AND group_id IS NULL
`, actor.TenantID).Scan(&total)
if err != nil {
return nil, response.ErrInternal(50010, "count_failed", "failed to count rules")
}
r, err := s.pool.Query(ctx, `
SELECT pr.id, pr.group_id, pr.name, pr.prompt_content,
pr.scene, pr.default_tone, pr.default_word_count, pr.status,
pr.created_at, pr.updated_at,
COUNT(a.id)::INT AS article_count
FROM prompt_rules pr
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
WHERE pr.tenant_id = $1 AND pr.deleted_at IS NULL AND pr.group_id IS NULL
GROUP BY pr.id
ORDER BY pr.created_at DESC
LIMIT $2 OFFSET $3
`, actor.TenantID, params.PageSize, offset)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
}
rows = r
queryErr = err
} else {
err := s.pool.QueryRow(ctx, `
SELECT COUNT(*) FROM prompt_rules
WHERE tenant_id = $1 AND deleted_at IS NULL
AND ($2::bigint IS NULL OR group_id = $2)
AND ($3::text IS NULL OR status = $3)
AND ($4::text IS NULL OR name ILIKE '%' || $4 || '%')
`, actor.TenantID, params.GroupID, params.Status, params.Keyword).Scan(&total)
if err != nil {
return nil, response.ErrInternal(50010, "count_failed", "failed to count rules")
}
r, err := s.pool.Query(ctx, `
SELECT pr.id, pr.group_id, pr.name, pr.prompt_content,
pr.scene, pr.default_tone, pr.default_word_count, pr.status,
pr.created_at, pr.updated_at,
COUNT(a.id)::INT AS article_count
FROM prompt_rules pr
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
WHERE pr.tenant_id = $1 AND pr.deleted_at IS NULL
AND ($2::bigint IS NULL OR pr.group_id = $2)
AND ($3::text IS NULL OR pr.status = $3)
AND ($4::text IS NULL OR pr.name ILIKE '%' || $4 || '%')
GROUP BY pr.id
ORDER BY pr.created_at DESC
LIMIT $5 OFFSET $6
`, actor.TenantID, params.GroupID, params.Status, params.Keyword, params.PageSize, offset)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
}
rows = r
queryErr = err
}
_ = queryErr
pgxRows := rows.(interface {
Close()
Next() bool
Scan(dest ...interface{}) error
Err() error
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleListCacheKey(actor.TenantID, params), defaultCacheTTL(), func(loadCtx context.Context) (*PromptRuleListResponse, error) {
return s.loadPromptRules(loadCtx, actor.TenantID, params)
})
defer pgxRows.Close()
var items []PromptRuleResponse
for pgxRows.Next() {
var r PromptRuleResponse
var ca, ua interface{}
if err := pgxRows.Scan(&r.ID, &r.GroupID, &r.Name, &r.PromptContent,
&r.Scene, &r.DefaultTone, &r.DefaultWordCount, &r.Status,
&ca, &ua, &r.ArticleCount); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
r.CreatedAt = fmt.Sprintf("%v", ca)
r.UpdatedAt = fmt.Sprintf("%v", ua)
r.KnowledgeGroupIDs = []int64{}
r.KnowledgeGroups = []PromptRuleKnowledgeGroup{}
items = append(items, r)
}
if items == nil {
items = []PromptRuleResponse{}
}
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, actor.TenantID, collectPromptRuleIDs(items))
if err != nil {
return nil, err
}
for index := range items {
items[index].KnowledgeGroups = groupMap[items[index].ID]
items[index].KnowledgeGroupIDs = promptRuleKnowledgeGroupIDs(items[index].KnowledgeGroups)
}
return &PromptRuleListResponse{Items: items, Total: total}, nil
}
func (s *PromptRuleService) Detail(ctx context.Context, id int64) (*PromptRuleResponse, error) {
actor := auth.MustActor(ctx)
var r PromptRuleResponse
var ca, ua interface{}
var articleCount int
err := s.pool.QueryRow(ctx, `
SELECT pr.id, pr.group_id, pr.name, pr.prompt_content,
pr.scene, pr.default_tone, pr.default_word_count, pr.status,
pr.created_at, pr.updated_at,
(SELECT COUNT(*) FROM articles WHERE prompt_rule_id = pr.id AND deleted_at IS NULL)::INT
FROM prompt_rules pr
WHERE pr.id = $1 AND pr.tenant_id = $2 AND pr.deleted_at IS NULL
`, id, actor.TenantID).Scan(&r.ID, &r.GroupID, &r.Name, &r.PromptContent,
&r.Scene, &r.DefaultTone, &r.DefaultWordCount, &r.Status,
&ca, &ua, &articleCount)
record, found, err := sharedcache.LoadJSONWithEmpty(ctx, s.cache, &s.cacheGroup, promptRuleDetailCacheKey(actor.TenantID, id), defaultCacheTTL(), defaultCacheEmptyTTL(), func(loadCtx context.Context) (*PromptRuleResponse, bool, error) {
return s.loadPromptRuleDetail(loadCtx, actor.TenantID, id)
})
if err != nil {
return nil, err
}
if !found || record == nil {
return nil, response.ErrNotFound(40431, "rule_not_found", "prompt rule not found")
}
r.CreatedAt = fmt.Sprintf("%v", ca)
r.UpdatedAt = fmt.Sprintf("%v", ua)
r.ArticleCount = articleCount
groupMap, groupErr := s.loadPromptRuleKnowledgeGroupMap(ctx, actor.TenantID, []int64{id})
if groupErr != nil {
return nil, groupErr
}
r.KnowledgeGroups = groupMap[id]
r.KnowledgeGroupIDs = promptRuleKnowledgeGroupIDs(r.KnowledgeGroups)
return &r, nil
return record, nil
}
func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (*PromptRuleResponse, error) {
@@ -403,6 +270,7 @@ func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (
Result: &result,
})
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
return &PromptRuleResponse{
ID: id,
GroupID: req.GroupID,
@@ -447,6 +315,7 @@ func (s *PromptRuleService) Update(ctx context.Context, id int64, req PromptRule
if err := tx.Commit(ctx); err != nil {
return response.ErrInternal(50010, "update_failed", "failed to commit prompt rule")
}
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
return nil
}
@@ -475,6 +344,7 @@ func (s *PromptRuleService) Delete(ctx context.Context, id int64) error {
AfterJSON: afterJSON,
Result: &result,
})
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
return nil
}
@@ -487,31 +357,197 @@ func (s *PromptRuleService) ToggleStatus(ctx context.Context, id int64, req Prom
if err != nil || tag.RowsAffected() == 0 {
return response.ErrNotFound(40431, "rule_not_found", "prompt rule not found")
}
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
return nil
}
func (s *PromptRuleService) ListSimple(ctx context.Context) ([]PromptRuleSimple, error) {
actor := auth.MustActor(ctx)
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleSimpleCacheKey(actor.TenantID), defaultCacheTTL(), func(loadCtx context.Context) ([]PromptRuleSimple, error) {
return s.loadPromptRuleSimple(loadCtx, actor.TenantID)
})
}
func (s *PromptRuleService) loadPromptRuleGroups(ctx context.Context, tenantID int64) ([]PromptRuleGroupResponse, error) {
rows, err := s.pool.Query(ctx, `
SELECT id, name, sort_order, created_at
FROM prompt_rule_groups WHERE tenant_id = $1 AND deleted_at IS NULL
ORDER BY sort_order, created_at
`, tenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list groups")
}
defer rows.Close()
items := make([]PromptRuleGroupResponse, 0)
for rows.Next() {
var item PromptRuleGroupResponse
var createdAt interface{}
if err := rows.Scan(&item.ID, &item.Name, &item.SortOrder, &createdAt); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
item.CreatedAt = fmt.Sprintf("%v", createdAt)
items = append(items, item)
}
return items, nil
}
func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID int64, params PromptRuleListParams) (*PromptRuleListResponse, error) {
if params.Page < 1 {
params.Page = 1
}
if params.PageSize < 1 || params.PageSize > 100 {
params.PageSize = 20
}
offset := (params.Page - 1) * params.PageSize
var total int64
var rows interface{ Close() }
if params.Ungrouped {
if err := s.pool.QueryRow(ctx, `
SELECT COUNT(*) FROM prompt_rules
WHERE tenant_id = $1 AND deleted_at IS NULL AND group_id IS NULL
`, tenantID).Scan(&total); err != nil {
return nil, response.ErrInternal(50010, "count_failed", "failed to count rules")
}
r, err := s.pool.Query(ctx, `
SELECT pr.id, pr.group_id, pr.name, pr.prompt_content,
pr.scene, pr.default_tone, pr.default_word_count, pr.status,
pr.created_at, pr.updated_at,
COUNT(a.id)::INT AS article_count
FROM prompt_rules pr
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
WHERE pr.tenant_id = $1 AND pr.deleted_at IS NULL AND pr.group_id IS NULL
GROUP BY pr.id
ORDER BY pr.created_at DESC
LIMIT $2 OFFSET $3
`, tenantID, params.PageSize, offset)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
}
rows = r
} else {
if err := s.pool.QueryRow(ctx, `
SELECT COUNT(*) FROM prompt_rules
WHERE tenant_id = $1 AND deleted_at IS NULL
AND ($2::bigint IS NULL OR group_id = $2)
AND ($3::text IS NULL OR status = $3)
AND ($4::text IS NULL OR name ILIKE '%' || $4 || '%')
`, tenantID, params.GroupID, params.Status, params.Keyword).Scan(&total); err != nil {
return nil, response.ErrInternal(50010, "count_failed", "failed to count rules")
}
r, err := s.pool.Query(ctx, `
SELECT pr.id, pr.group_id, pr.name, pr.prompt_content,
pr.scene, pr.default_tone, pr.default_word_count, pr.status,
pr.created_at, pr.updated_at,
COUNT(a.id)::INT AS article_count
FROM prompt_rules pr
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
WHERE pr.tenant_id = $1 AND pr.deleted_at IS NULL
AND ($2::bigint IS NULL OR pr.group_id = $2)
AND ($3::text IS NULL OR pr.status = $3)
AND ($4::text IS NULL OR pr.name ILIKE '%' || $4 || '%')
GROUP BY pr.id
ORDER BY pr.created_at DESC
LIMIT $5 OFFSET $6
`, tenantID, params.GroupID, params.Status, params.Keyword, params.PageSize, offset)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
}
rows = r
}
pgxRows := rows.(interface {
Close()
Next() bool
Scan(dest ...interface{}) error
Err() error
})
defer pgxRows.Close()
items := make([]PromptRuleResponse, 0)
for pgxRows.Next() {
var item PromptRuleResponse
var createdAt interface{}
var updatedAt interface{}
if err := pgxRows.Scan(&item.ID, &item.GroupID, &item.Name, &item.PromptContent,
&item.Scene, &item.DefaultTone, &item.DefaultWordCount, &item.Status,
&createdAt, &updatedAt, &item.ArticleCount); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
item.CreatedAt = fmt.Sprintf("%v", createdAt)
item.UpdatedAt = fmt.Sprintf("%v", updatedAt)
item.KnowledgeGroupIDs = []int64{}
item.KnowledgeGroups = []PromptRuleKnowledgeGroup{}
items = append(items, item)
}
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, tenantID, collectPromptRuleIDs(items))
if err != nil {
return nil, err
}
for idx := range items {
items[idx].KnowledgeGroups = groupMap[items[idx].ID]
items[idx].KnowledgeGroupIDs = promptRuleKnowledgeGroupIDs(items[idx].KnowledgeGroups)
}
return &PromptRuleListResponse{Items: items, Total: total}, nil
}
func (s *PromptRuleService) loadPromptRuleDetail(ctx context.Context, tenantID, ruleID int64) (*PromptRuleResponse, bool, error) {
var item PromptRuleResponse
var createdAt interface{}
var updatedAt interface{}
var articleCount int
err := s.pool.QueryRow(ctx, `
SELECT pr.id, pr.group_id, pr.name, pr.prompt_content,
pr.scene, pr.default_tone, pr.default_word_count, pr.status,
pr.created_at, pr.updated_at,
(SELECT COUNT(*) FROM articles WHERE prompt_rule_id = pr.id AND deleted_at IS NULL)::INT
FROM prompt_rules pr
WHERE pr.id = $1 AND pr.tenant_id = $2 AND pr.deleted_at IS NULL
`, ruleID, tenantID).Scan(&item.ID, &item.GroupID, &item.Name, &item.PromptContent,
&item.Scene, &item.DefaultTone, &item.DefaultWordCount, &item.Status,
&createdAt, &updatedAt, &articleCount)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, false, nil
}
return nil, false, response.ErrInternal(50010, "query_failed", "failed to fetch prompt rule")
}
item.CreatedAt = fmt.Sprintf("%v", createdAt)
item.UpdatedAt = fmt.Sprintf("%v", updatedAt)
item.ArticleCount = articleCount
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, tenantID, []int64{ruleID})
if err != nil {
return nil, false, err
}
item.KnowledgeGroups = groupMap[ruleID]
item.KnowledgeGroupIDs = promptRuleKnowledgeGroupIDs(item.KnowledgeGroups)
return &item, true, nil
}
func (s *PromptRuleService) loadPromptRuleSimple(ctx context.Context, tenantID int64) ([]PromptRuleSimple, error) {
rows, err := s.pool.Query(ctx, `
SELECT id, name FROM prompt_rules
WHERE tenant_id = $1 AND deleted_at IS NULL AND status = 'enabled'
ORDER BY name
`, actor.TenantID)
`, tenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
}
defer rows.Close()
var items []PromptRuleSimple
items := make([]PromptRuleSimple, 0)
for rows.Next() {
var r PromptRuleSimple
if err := rows.Scan(&r.ID, &r.Name); err != nil {
var item PromptRuleSimple
if err := rows.Scan(&item.ID, &item.Name); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
items = append(items, r)
}
if items == nil {
items = []PromptRuleSimple{}
items = append(items, item)
}
return items, nil
}