Files
geo/server/internal/tenant/transport/workspace_handler.go
T
root 1538a12042 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
2026-04-15 16:11:05 +08:00

63 lines
1.4 KiB
Go

package transport
import (
"github.com/gin-gonic/gin"
"github.com/geo-platform/tenant-api/internal/bootstrap"
"github.com/geo-platform/tenant-api/internal/shared/response"
"github.com/geo-platform/tenant-api/internal/tenant/app"
"github.com/geo-platform/tenant-api/internal/tenant/repository"
)
type WorkspaceHandler struct {
svc *app.WorkspaceService
}
func NewWorkspaceHandler(a *bootstrap.App) *WorkspaceHandler {
return &WorkspaceHandler{
svc: app.NewWorkspaceService(
repository.NewWorkspaceRepository(a.DB),
repository.NewCachedTemplateRepository(
repository.NewTemplateRepository(a.DB),
a.Cache,
),
).WithCache(a.Cache),
}
}
func (h *WorkspaceHandler) Overview(c *gin.Context) {
data, err := h.svc.Overview(c.Request.Context())
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *WorkspaceHandler) RecentArticles(c *gin.Context) {
data, err := h.svc.RecentArticles(c.Request.Context())
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *WorkspaceHandler) QuotaSummary(c *gin.Context) {
data, err := h.svc.QuotaSummary(c.Request.Context())
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}
func (h *WorkspaceHandler) TemplateCards(c *gin.Context) {
data, err := h.svc.TemplateCards(c.Request.Context())
if err != nil {
response.Error(c, err)
return
}
response.Success(c, data)
}