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:
@@ -0,0 +1,50 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
sharedcache "github.com/geo-platform/tenant-api/internal/shared/cache"
|
||||
)
|
||||
|
||||
func TestArticleCacheVersionSeparatesLateWritesAfterInvalidation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
c := sharedcache.New("memory", nil, sharedcache.WithNamespace("test"))
|
||||
tenantID := int64(42)
|
||||
articleID := int64(7)
|
||||
params := ArticleListParams{Page: 1, PageSize: 10}
|
||||
|
||||
oldVersion := articleCacheVersion(ctx, c, tenantID)
|
||||
oldListKey := articleListCacheKey(tenantID, oldVersion, params)
|
||||
oldDetailKey := articleDetailCacheKey(tenantID, articleID, oldVersion)
|
||||
if err := c.Set(ctx, oldListKey, []byte("generating-list"), time.Minute); err != nil {
|
||||
t.Fatalf("set old list cache: %v", err)
|
||||
}
|
||||
if err := c.Set(ctx, oldDetailKey, []byte("generating-detail"), time.Minute); err != nil {
|
||||
t.Fatalf("set old detail cache: %v", err)
|
||||
}
|
||||
|
||||
invalidateArticleCaches(ctx, c, tenantID, &articleID)
|
||||
newVersion := articleCacheVersion(ctx, c, tenantID)
|
||||
if newVersion == oldVersion {
|
||||
t.Fatalf("expected article cache version to change, stayed %q", newVersion)
|
||||
}
|
||||
|
||||
if err := c.Set(ctx, oldListKey, []byte("late-generating-list"), time.Minute); err != nil {
|
||||
t.Fatalf("late set old list cache: %v", err)
|
||||
}
|
||||
if err := c.Set(ctx, oldDetailKey, []byte("late-generating-detail"), time.Minute); err != nil {
|
||||
t.Fatalf("late set old detail cache: %v", err)
|
||||
}
|
||||
|
||||
if _, err := c.Get(ctx, articleListCacheKey(tenantID, newVersion, params)); !errors.Is(err, sharedcache.ErrNotFound) {
|
||||
t.Fatalf("expected new list key to miss stale late write, got err %v", err)
|
||||
}
|
||||
if _, err := c.Get(ctx, articleDetailCacheKey(tenantID, articleID, newVersion)); !errors.Is(err, sharedcache.ErrNotFound) {
|
||||
t.Fatalf("expected new detail key to miss stale late write, got err %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user