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
+169 -116
View File
@@ -3,13 +3,17 @@ package app
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"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"
)
@@ -18,12 +22,19 @@ type BrandService struct {
pool *pgxpool.Pool
monitoringPool *pgxpool.Pool
auditLogs *auditlog.AsyncWriter
cache sharedcache.Cache
cacheGroup singleflight.Group
}
func NewBrandService(pool, monitoringPool *pgxpool.Pool, auditLogs *auditlog.AsyncWriter) *BrandService {
return &BrandService{pool: pool, monitoringPool: monitoringPool, auditLogs: auditLogs}
}
func (s *BrandService) WithCache(c sharedcache.Cache) *BrandService {
s.cache = c
return s
}
// --- Brand CRUD ---
type BrandRequest struct {
@@ -44,30 +55,9 @@ type BrandResponse struct {
func (s *BrandService) List(ctx context.Context) ([]BrandResponse, error) {
actor := auth.MustActor(ctx)
rows, err := s.pool.Query(ctx, `
SELECT id, name, website, description, status, created_at, updated_at
FROM brands WHERE tenant_id = $1 AND deleted_at IS NULL ORDER BY created_at DESC
`, actor.TenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list brands")
}
defer rows.Close()
var brands []BrandResponse
for rows.Next() {
var b BrandResponse
var ca, ua interface{}
if err := rows.Scan(&b.ID, &b.Name, &b.Website, &b.Description, &b.Status, &ca, &ua); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
b.CreatedAt = fmt.Sprintf("%v", ca)
b.UpdatedAt = fmt.Sprintf("%v", ua)
brands = append(brands, b)
}
if brands == nil {
brands = []BrandResponse{}
}
return brands, nil
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, brandListCacheKey(actor.TenantID), defaultCacheTTL(), func(loadCtx context.Context) ([]BrandResponse, error) {
return s.loadBrands(loadCtx, actor.TenantID)
})
}
func (s *BrandService) Create(ctx context.Context, req BrandRequest) (*BrandResponse, error) {
@@ -102,6 +92,7 @@ func (s *BrandService) Create(ctx context.Context, req BrandRequest) (*BrandResp
Result: &result,
})
invalidateBrandCaches(ctx, s.cache, actor.TenantID, id)
return &BrandResponse{
ID: id,
Name: req.Name,
@@ -114,18 +105,16 @@ func (s *BrandService) Create(ctx context.Context, req BrandRequest) (*BrandResp
func (s *BrandService) Detail(ctx context.Context, id int64) (*BrandResponse, error) {
actor := auth.MustActor(ctx)
var b BrandResponse
var ca, ua interface{}
err := s.pool.QueryRow(ctx, `
SELECT id, name, website, description, status, created_at, updated_at
FROM brands WHERE id = $1 AND tenant_id = $2 AND deleted_at IS NULL
`, id, actor.TenantID).Scan(&b.ID, &b.Name, &b.Website, &b.Description, &b.Status, &ca, &ua)
record, found, err := sharedcache.LoadJSONWithEmpty(ctx, s.cache, &s.cacheGroup, brandDetailCacheKey(actor.TenantID, id), defaultCacheTTL(), defaultCacheEmptyTTL(), func(loadCtx context.Context) (*BrandResponse, bool, error) {
return s.loadBrandDetail(loadCtx, actor.TenantID, id)
})
if err != nil {
return nil, err
}
if !found || record == nil {
return nil, response.ErrNotFound(40420, "brand_not_found", "brand not found")
}
b.CreatedAt = fmt.Sprintf("%v", ca)
b.UpdatedAt = fmt.Sprintf("%v", ua)
return &b, nil
return record, nil
}
func (s *BrandService) Update(ctx context.Context, id int64, req BrandRequest) error {
@@ -141,6 +130,7 @@ func (s *BrandService) Update(ctx context.Context, id int64, req BrandRequest) e
if err != nil || tag.RowsAffected() == 0 {
return response.ErrNotFound(40420, "brand_not_found", "brand not found")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, id)
return nil
}
@@ -199,6 +189,7 @@ func (s *BrandService) Delete(ctx context.Context, id int64) error {
Result: &result,
})
invalidateBrandCaches(ctx, s.cache, actor.TenantID, id)
return nil
}
@@ -218,29 +209,9 @@ type KeywordResponse struct {
func (s *BrandService) ListKeywords(ctx context.Context, brandID int64) ([]KeywordResponse, error) {
actor := auth.MustActor(ctx)
rows, err := s.pool.Query(ctx, `
SELECT id, brand_id, name, status, created_at FROM brand_keywords
WHERE brand_id = $1 AND tenant_id = $2 AND deleted_at IS NULL ORDER BY created_at DESC
`, brandID, actor.TenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list keywords")
}
defer rows.Close()
var keywords []KeywordResponse
for rows.Next() {
var k KeywordResponse
var ca interface{}
if err := rows.Scan(&k.ID, &k.BrandID, &k.Name, &k.Status, &ca); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
k.CreatedAt = fmt.Sprintf("%v", ca)
keywords = append(keywords, k)
}
if keywords == nil {
keywords = []KeywordResponse{}
}
return keywords, nil
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, brandKeywordsCacheKey(actor.TenantID, brandID), defaultCacheTTL(), func(loadCtx context.Context) ([]KeywordResponse, error) {
return s.loadBrandKeywords(loadCtx, actor.TenantID, brandID)
})
}
func (s *BrandService) CreateKeyword(ctx context.Context, brandID int64, req KeywordRequest) (*KeywordResponse, error) {
@@ -253,6 +224,7 @@ func (s *BrandService) CreateKeyword(ctx context.Context, brandID int64, req Key
if err != nil {
return nil, response.ErrConflict(40902, "keyword_exists", "keyword with this name already exists for this brand")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return &KeywordResponse{ID: id, BrandID: brandID, Name: req.Name, Status: "active", CreatedAt: fmt.Sprintf("%v", ca)}, nil
}
@@ -265,6 +237,7 @@ func (s *BrandService) UpdateKeyword(ctx context.Context, brandID, keywordID int
if err != nil || tag.RowsAffected() == 0 {
return response.ErrNotFound(40421, "keyword_not_found", "keyword not found")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return nil
}
@@ -329,6 +302,7 @@ func (s *BrandService) DeleteKeyword(ctx context.Context, brandID, keywordID int
return err
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return nil
}
@@ -350,38 +324,9 @@ type QuestionResponse struct {
func (s *BrandService) ListQuestions(ctx context.Context, brandID int64, keywordID *int64) ([]QuestionResponse, error) {
actor := auth.MustActor(ctx)
query := `
SELECT q.id, q.brand_id, q.keyword_id, q.question_text, q.status, q.created_at
FROM brand_questions q
WHERE q.brand_id = $1 AND q.tenant_id = $2 AND q.deleted_at IS NULL`
args := []interface{}{brandID, actor.TenantID}
if keywordID != nil {
query += ` AND q.keyword_id = $3`
args = append(args, *keywordID)
}
query += ` ORDER BY q.created_at DESC`
rows, err := s.pool.Query(ctx, query, args...)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list questions")
}
defer rows.Close()
var questions []QuestionResponse
for rows.Next() {
var q QuestionResponse
var ca interface{}
if err := rows.Scan(&q.ID, &q.BrandID, &q.KeywordID, &q.QuestionText, &q.Status, &ca); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
q.CreatedAt = fmt.Sprintf("%v", ca)
questions = append(questions, q)
}
if questions == nil {
questions = []QuestionResponse{}
}
return questions, nil
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, brandQuestionsCacheKey(actor.TenantID, brandID, keywordID), defaultCacheTTL(), func(loadCtx context.Context) ([]QuestionResponse, error) {
return s.loadBrandQuestions(loadCtx, actor.TenantID, brandID, keywordID)
})
}
func (s *BrandService) CreateQuestion(ctx context.Context, brandID int64, req QuestionRequest) (*QuestionResponse, error) {
@@ -402,6 +347,7 @@ func (s *BrandService) CreateQuestion(ctx context.Context, brandID int64, req Qu
return nil, response.ErrInternal(50010, "create_failed", "failed to create question")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return &QuestionResponse{
ID: questionID,
BrandID: brandID,
@@ -430,6 +376,7 @@ func (s *BrandService) UpdateQuestion(ctx context.Context, brandID, questionID i
if err != nil || tag.RowsAffected() == 0 {
return response.ErrNotFound(40422, "question_not_found", "question not found")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return nil
}
@@ -459,6 +406,7 @@ func (s *BrandService) DeleteQuestion(ctx context.Context, brandID, questionID i
return err
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return nil
}
@@ -483,34 +431,9 @@ type CompetitorResponse struct {
func (s *BrandService) ListCompetitors(ctx context.Context, brandID int64) ([]CompetitorResponse, error) {
actor := auth.MustActor(ctx)
rows, err := s.pool.Query(ctx, `
SELECT id, brand_id, name, website, description, product_lines_json, created_at
FROM competitors WHERE brand_id = $1 AND tenant_id = $2 AND deleted_at IS NULL ORDER BY created_at DESC
`, brandID, actor.TenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list competitors")
}
defer rows.Close()
var comps []CompetitorResponse
for rows.Next() {
var c CompetitorResponse
var ca interface{}
var plJSON []byte
if err := rows.Scan(&c.ID, &c.BrandID, &c.Name, &c.Website, &c.Description, &plJSON, &ca); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
c.CreatedAt = fmt.Sprintf("%v", ca)
if plJSON != nil {
raw := json.RawMessage(plJSON)
c.ProductLinesJSON = &raw
}
comps = append(comps, c)
}
if comps == nil {
comps = []CompetitorResponse{}
}
return comps, nil
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, brandCompetitorsCacheKey(actor.TenantID, brandID), defaultCacheTTL(), func(loadCtx context.Context) ([]CompetitorResponse, error) {
return s.loadBrandCompetitors(loadCtx, actor.TenantID, brandID)
})
}
func (s *BrandService) CreateCompetitor(ctx context.Context, brandID int64, req CompetitorRequest) (*CompetitorResponse, error) {
@@ -529,6 +452,7 @@ func (s *BrandService) CreateCompetitor(ctx context.Context, brandID int64, req
if err != nil {
return nil, response.ErrInternal(50010, "create_failed", "failed to create competitor")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return &CompetitorResponse{ID: id, BrandID: brandID, Name: req.Name, Website: req.Website, Description: req.Description, ProductLinesJSON: req.ProductLinesJSON, CreatedAt: fmt.Sprintf("%v", ca)}, nil
}
@@ -545,6 +469,7 @@ func (s *BrandService) UpdateCompetitor(ctx context.Context, brandID, competitor
if err != nil || tag.RowsAffected() == 0 {
return response.ErrNotFound(40423, "competitor_not_found", "competitor not found")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return nil
}
@@ -557,5 +482,133 @@ func (s *BrandService) DeleteCompetitor(ctx context.Context, brandID, competitor
if err != nil || tag.RowsAffected() == 0 {
return response.ErrNotFound(40423, "competitor_not_found", "competitor not found")
}
invalidateBrandCaches(ctx, s.cache, actor.TenantID, brandID)
return nil
}
func (s *BrandService) loadBrands(ctx context.Context, tenantID int64) ([]BrandResponse, error) {
rows, err := s.pool.Query(ctx, `
SELECT id, name, website, description, status, created_at, updated_at
FROM brands WHERE tenant_id = $1 AND deleted_at IS NULL ORDER BY created_at DESC
`, tenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list brands")
}
defer rows.Close()
brands := make([]BrandResponse, 0)
for rows.Next() {
var item BrandResponse
var createdAt interface{}
var updatedAt interface{}
if err := rows.Scan(&item.ID, &item.Name, &item.Website, &item.Description, &item.Status, &createdAt, &updatedAt); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
item.CreatedAt = fmt.Sprintf("%v", createdAt)
item.UpdatedAt = fmt.Sprintf("%v", updatedAt)
brands = append(brands, item)
}
return brands, nil
}
func (s *BrandService) loadBrandDetail(ctx context.Context, tenantID, brandID int64) (*BrandResponse, bool, error) {
var item BrandResponse
var createdAt interface{}
var updatedAt interface{}
err := s.pool.QueryRow(ctx, `
SELECT id, name, website, description, status, created_at, updated_at
FROM brands WHERE id = $1 AND tenant_id = $2 AND deleted_at IS NULL
`, brandID, tenantID).Scan(&item.ID, &item.Name, &item.Website, &item.Description, &item.Status, &createdAt, &updatedAt)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, false, nil
}
return nil, false, response.ErrInternal(50010, "query_failed", "failed to fetch brand")
}
item.CreatedAt = fmt.Sprintf("%v", createdAt)
item.UpdatedAt = fmt.Sprintf("%v", updatedAt)
return &item, true, nil
}
func (s *BrandService) loadBrandKeywords(ctx context.Context, tenantID, brandID int64) ([]KeywordResponse, error) {
rows, err := s.pool.Query(ctx, `
SELECT id, brand_id, name, status, created_at FROM brand_keywords
WHERE brand_id = $1 AND tenant_id = $2 AND deleted_at IS NULL ORDER BY created_at DESC
`, brandID, tenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list keywords")
}
defer rows.Close()
items := make([]KeywordResponse, 0)
for rows.Next() {
var item KeywordResponse
var createdAt interface{}
if err := rows.Scan(&item.ID, &item.BrandID, &item.Name, &item.Status, &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 *BrandService) loadBrandQuestions(ctx context.Context, tenantID, brandID int64, keywordID *int64) ([]QuestionResponse, error) {
query := `
SELECT q.id, q.brand_id, q.keyword_id, q.question_text, q.status, q.created_at
FROM brand_questions q
WHERE q.brand_id = $1 AND q.tenant_id = $2 AND q.deleted_at IS NULL`
args := []interface{}{brandID, tenantID}
if keywordID != nil {
query += ` AND q.keyword_id = $3`
args = append(args, *keywordID)
}
query += ` ORDER BY q.created_at DESC`
rows, err := s.pool.Query(ctx, query, args...)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list questions")
}
defer rows.Close()
items := make([]QuestionResponse, 0)
for rows.Next() {
var item QuestionResponse
var createdAt interface{}
if err := rows.Scan(&item.ID, &item.BrandID, &item.KeywordID, &item.QuestionText, &item.Status, &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 *BrandService) loadBrandCompetitors(ctx context.Context, tenantID, brandID int64) ([]CompetitorResponse, error) {
rows, err := s.pool.Query(ctx, `
SELECT id, brand_id, name, website, description, product_lines_json, created_at
FROM competitors WHERE brand_id = $1 AND tenant_id = $2 AND deleted_at IS NULL ORDER BY created_at DESC
`, brandID, tenantID)
if err != nil {
return nil, response.ErrInternal(50010, "query_failed", "failed to list competitors")
}
defer rows.Close()
items := make([]CompetitorResponse, 0)
for rows.Next() {
var item CompetitorResponse
var createdAt interface{}
var productLinesJSON []byte
if err := rows.Scan(&item.ID, &item.BrandID, &item.Name, &item.Website, &item.Description, &productLinesJSON, &createdAt); err != nil {
return nil, response.ErrInternal(50010, "scan_failed", err.Error())
}
item.CreatedAt = fmt.Sprintf("%v", createdAt)
if productLinesJSON != nil {
raw := json.RawMessage(productLinesJSON)
item.ProductLinesJSON = &raw
}
items = append(items, item)
}
return items, nil
}