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:
@@ -42,7 +42,7 @@ func NewArticleHandler(a *bootstrap.App) *ArticleHandler {
|
||||
)
|
||||
|
||||
return &ArticleHandler{
|
||||
svc: app.NewArticleService(a.DB, a.AuditLogs, a.ObjectStorage),
|
||||
svc: app.NewArticleService(a.DB, a.AuditLogs, a.ObjectStorage).WithCache(a.Cache),
|
||||
selectionOptimize: app.NewArticleSelectionOptimizeService(
|
||||
a.DB,
|
||||
a.LLM,
|
||||
@@ -56,7 +56,7 @@ func NewArticleHandler(a *bootstrap.App) *ArticleHandler {
|
||||
a.GenerationStreams,
|
||||
a.Config.Generation,
|
||||
a.Config.LLM.MaxOutputTokens,
|
||||
),
|
||||
).WithCache(a.Cache),
|
||||
streams: a.GenerationStreams,
|
||||
streamEnabled: a.Config.Generation.StreamEnabled,
|
||||
assets: NewAssetHandler(a),
|
||||
|
||||
@@ -15,7 +15,7 @@ type BrandHandler struct {
|
||||
}
|
||||
|
||||
func NewBrandHandler(a *bootstrap.App) *BrandHandler {
|
||||
return &BrandHandler{svc: app.NewBrandService(a.DB, a.MonitoringDB, a.AuditLogs)}
|
||||
return &BrandHandler{svc: app.NewBrandService(a.DB, a.MonitoringDB, a.AuditLogs).WithCache(a.Cache)}
|
||||
}
|
||||
|
||||
func (h *BrandHandler) List(c *gin.Context) {
|
||||
|
||||
@@ -18,7 +18,7 @@ type MediaHandler struct {
|
||||
|
||||
func NewMediaHandler(a *bootstrap.App) *MediaHandler {
|
||||
return &MediaHandler{
|
||||
svc: app.NewMediaService(a.DB, a.Logger),
|
||||
svc: app.NewMediaService(a.DB, a.Logger).WithCache(a.Cache),
|
||||
logger: a.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ type PromptRuleHandler struct {
|
||||
}
|
||||
|
||||
func NewPromptRuleHandler(a *bootstrap.App) *PromptRuleHandler {
|
||||
return &PromptRuleHandler{svc: app.NewPromptRuleService(a.DB, a.AuditLogs)}
|
||||
return &PromptRuleHandler{svc: app.NewPromptRuleService(a.DB, a.AuditLogs).WithCache(a.Cache)}
|
||||
}
|
||||
|
||||
// --- Groups ---
|
||||
|
||||
@@ -16,7 +16,7 @@ type ScheduleTaskHandler struct {
|
||||
}
|
||||
|
||||
func NewScheduleTaskHandler(a *bootstrap.App) *ScheduleTaskHandler {
|
||||
return &ScheduleTaskHandler{svc: app.NewScheduleTaskService(a.DB, a.AuditLogs)}
|
||||
return &ScheduleTaskHandler{svc: app.NewScheduleTaskService(a.DB, a.AuditLogs).WithCache(a.Cache)}
|
||||
}
|
||||
|
||||
func (h *ScheduleTaskHandler) List(c *gin.Context) {
|
||||
|
||||
@@ -42,7 +42,7 @@ func NewTemplateHandler(a *bootstrap.App) *TemplateHandler {
|
||||
a.GenerationStreams,
|
||||
a.Config.Generation,
|
||||
a.Config.LLM.MaxOutputTokens,
|
||||
),
|
||||
).WithCache(a.Cache),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ func NewWorkspaceHandler(a *bootstrap.App) *WorkspaceHandler {
|
||||
repository.NewTemplateRepository(a.DB),
|
||||
a.Cache,
|
||||
),
|
||||
),
|
||||
).WithCache(a.Cache),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user