fix(api): 管理后台成功写入后递增缓存代次 (#40)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Vendored
+24
-4
@@ -6,9 +6,9 @@
|
||||
// so the cache can never take the API down or serve stale data after Redis loss.
|
||||
//
|
||||
// Invalidation is global and O(1): keys are namespaced by an epoch counter
|
||||
// stored in Redis (og:cache:epoch). The Python ingestion bumps that counter
|
||||
// after a write run, which logically invalidates every cached entry at once
|
||||
// while old keys age out via their TTL. The epoch is read at most once per
|
||||
// stored in Redis (og:cache:epoch). Python ingestion and successful admin
|
||||
// writes bump that counter, logically invalidating cached entries at once while
|
||||
// old keys age out via their TTL. The epoch is read at most once per
|
||||
// refresh interval per process, so it adds no per-request round trip.
|
||||
package cache
|
||||
|
||||
@@ -35,7 +35,7 @@ const epochRefresh = 10 * time.Second
|
||||
const opTimeout = 150 * time.Millisecond
|
||||
|
||||
// Cache wraps a Redis client. A nil-backed Cache (Redis unconfigured) disables
|
||||
// caching: every Get misses and every Set is a no-op.
|
||||
// caching: every Get misses, every Set is a no-op, and Bump succeeds.
|
||||
type Cache struct {
|
||||
rdb *redis.Client
|
||||
|
||||
@@ -59,6 +59,26 @@ func New(redisURL string) *Cache {
|
||||
// Enabled reports whether a Redis backend is configured.
|
||||
func (c *Cache) Enabled() bool { return c != nil && c.rdb != nil }
|
||||
|
||||
// Bump increments the global cache epoch to invalidate existing entries.
|
||||
func (c *Cache) Bump(ctx context.Context) error {
|
||||
if !c.Enabled() {
|
||||
return nil
|
||||
}
|
||||
cctx, cancel := context.WithTimeout(ctx, opTimeout)
|
||||
defer cancel()
|
||||
epoch, err := c.rdb.Incr(cctx, epochKey).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
c.epoch = epoch
|
||||
c.epochSetAt = time.Now()
|
||||
c.epochOK = true
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// epochNow returns the current cache generation, reading it from Redis at most
|
||||
// once per epochRefresh. On any Redis error it keeps the last known value and
|
||||
// throttles re-reads so a down backend cannot slow the hot path.
|
||||
|
||||
Vendored
+11
-7
@@ -22,6 +22,15 @@ func TestDisabledFailsOpen(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBumpDisabledCache(t *testing.T) {
|
||||
if err := (*Cache)(nil).Bump(context.Background()); err != nil {
|
||||
t.Fatalf("nil cache Bump(): %v", err)
|
||||
}
|
||||
if err := (&Cache{}).Bump(context.Background()); err != nil {
|
||||
t.Fatalf("disabled cache Bump(): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func testCache(t *testing.T) *Cache {
|
||||
t.Helper()
|
||||
url := os.Getenv("OPENGOODS_REDIS_URL")
|
||||
@@ -69,14 +78,9 @@ func TestEpochInvalidation(t *testing.T) {
|
||||
t.Fatal("expected hit before epoch bump")
|
||||
}
|
||||
|
||||
// Simulate an ingestion write bumping the global epoch.
|
||||
if err := c.rdb.Incr(ctx, epochKey).Err(); err != nil {
|
||||
t.Fatalf("incr epoch: %v", err)
|
||||
if err := c.Bump(ctx); err != nil {
|
||||
t.Fatalf("bump epoch: %v", err)
|
||||
}
|
||||
// Force the process to re-read the epoch rather than use its cached value.
|
||||
c.mu.Lock()
|
||||
c.epochOK = false
|
||||
c.mu.Unlock()
|
||||
|
||||
if c.GetJSON(ctx, suffix, &dst) {
|
||||
t.Fatal("entry should be invisible after epoch bump")
|
||||
|
||||
Reference in New Issue
Block a user