feat(cache,worker): overhaul cache layer and add generation task lease recovery
- shared/cache: add Options/L1/async/metrics/prefix decorators, multi-key ops, Redis pool tuning, and JSON readthrough metrics - worker-generate: claim tasks via DB lease + heartbeat, requeue stale queued tasks, expire dead leases with refund/cache invalidation - tenant: version article cache keys so worker recovery invalidations propagate cleanly - shared/config: expand Redis (pool/timeouts/TLS) and Generation (lease/recovery) configs with defaults Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,14 +6,18 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
sharedcache "github.com/geo-platform/tenant-api/internal/shared/cache"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultReadCacheTTL = 5 * time.Minute
|
||||
defaultReadCacheEmptyTTL = 1 * time.Minute
|
||||
articleCacheVersionTTL = 24 * time.Hour
|
||||
)
|
||||
|
||||
func defaultCacheTTL() time.Duration {
|
||||
@@ -203,18 +207,54 @@ func articleDetailCachePrefix(tenantID int64) string {
|
||||
return fmt.Sprintf("article:detail:%d:", tenantID)
|
||||
}
|
||||
|
||||
func articleDetailCacheKey(tenantID, articleID int64) string {
|
||||
return fmt.Sprintf("%s%d", articleDetailCachePrefix(tenantID), articleID)
|
||||
func articleDetailCacheArticlePrefix(tenantID, articleID int64) string {
|
||||
return fmt.Sprintf("%s%d:", articleDetailCachePrefix(tenantID), articleID)
|
||||
}
|
||||
|
||||
func articleListCacheKey(tenantID int64, params interface{}) string {
|
||||
return articleListCachePrefix(tenantID) + digestCacheKey(params)
|
||||
func articleDetailCacheKey(tenantID, articleID int64, version string) string {
|
||||
return fmt.Sprintf("%s%s", articleDetailCacheArticlePrefix(tenantID, articleID), normalizeArticleCacheVersion(version))
|
||||
}
|
||||
|
||||
func articleListCacheKey(tenantID int64, version string, params interface{}) string {
|
||||
return articleListCachePrefix(tenantID) + normalizeArticleCacheVersion(version) + ":" + digestCacheKey(params)
|
||||
}
|
||||
|
||||
func articleCacheVersionKey(tenantID int64) string {
|
||||
return fmt.Sprintf("cache:version:article:%d", tenantID)
|
||||
}
|
||||
|
||||
func articleCacheVersion(ctx context.Context, c sharedcache.Cache, tenantID int64) string {
|
||||
if c == nil || tenantID <= 0 {
|
||||
return "0"
|
||||
}
|
||||
raw, err := c.Get(ctx, articleCacheVersionKey(tenantID))
|
||||
if err != nil || len(raw) == 0 {
|
||||
return "0"
|
||||
}
|
||||
return normalizeArticleCacheVersion(string(raw))
|
||||
}
|
||||
|
||||
func bumpArticleCacheVersion(ctx context.Context, c sharedcache.Cache, tenantID int64) {
|
||||
if c == nil || tenantID <= 0 {
|
||||
return
|
||||
}
|
||||
version := uuid.NewString()
|
||||
_ = c.Set(ctx, articleCacheVersionKey(tenantID), []byte(version), articleCacheVersionTTL)
|
||||
}
|
||||
|
||||
func normalizeArticleCacheVersion(version string) string {
|
||||
version = strings.TrimSpace(version)
|
||||
if version == "" {
|
||||
return "0"
|
||||
}
|
||||
return version
|
||||
}
|
||||
|
||||
func invalidateArticleCaches(ctx context.Context, c sharedcache.Cache, tenantID int64, articleID *int64) {
|
||||
bumpArticleCacheVersion(ctx, c, tenantID)
|
||||
deleteCachePrefix(ctx, c, articleListCachePrefix(tenantID))
|
||||
if articleID != nil && *articleID > 0 {
|
||||
deleteCacheKey(ctx, c, articleDetailCacheKey(tenantID, *articleID))
|
||||
deleteCachePrefix(ctx, c, articleDetailCacheArticlePrefix(tenantID, *articleID))
|
||||
} else {
|
||||
deleteCachePrefix(ctx, c, articleDetailCachePrefix(tenantID))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user