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:
+39
-3
@@ -3,6 +3,7 @@ package cache
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -11,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defaultJitterRatio = 0.1
|
||||
readthroughDefaultJitterRatio = 0.1
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -62,6 +63,7 @@ func loadJSONInternal[T any](
|
||||
) (T, bool, error) {
|
||||
var zero T
|
||||
if c == nil {
|
||||
recordCacheOperation("readthrough_load", "cache_disabled", time.Duration(0))
|
||||
return loader(ctx)
|
||||
}
|
||||
|
||||
@@ -69,14 +71,23 @@ func loadJSONInternal[T any](
|
||||
var envelope payloadEnvelope[T]
|
||||
if unmarshalErr := json.Unmarshal(raw, &envelope); unmarshalErr == nil {
|
||||
if envelope.Empty {
|
||||
recordCacheOperation("readthrough_empty", "hit", time.Duration(0))
|
||||
return zero, false, nil
|
||||
}
|
||||
recordCacheOperation("readthrough_hit", "ok", time.Duration(0))
|
||||
return envelope.Value, true, nil
|
||||
}
|
||||
recordCacheOperation("readthrough_unmarshal", "error", time.Duration(0))
|
||||
} else if errors.Is(err, ErrNotFound) {
|
||||
recordCacheOperation("readthrough_miss", "not_found", time.Duration(0))
|
||||
} else {
|
||||
recordCacheOperation("readthrough_get", "error", time.Duration(0))
|
||||
}
|
||||
|
||||
result, err, _ := group.Do(key, func() (interface{}, error) {
|
||||
start := time.Now()
|
||||
value, found, loadErr := loader(ctx)
|
||||
recordCacheOperation("readthrough_load", cacheResult(loadErr), time.Since(start))
|
||||
if loadErr != nil {
|
||||
return nil, loadErr
|
||||
}
|
||||
@@ -86,13 +97,18 @@ func loadJSONInternal[T any](
|
||||
Value: value,
|
||||
}
|
||||
if raw, marshalErr := json.Marshal(envelope); marshalErr == nil {
|
||||
recordCacheOperation("readthrough_marshal", "ok", time.Duration(0))
|
||||
cacheTTL := ttl
|
||||
if !found {
|
||||
cacheTTL = emptyTTL
|
||||
}
|
||||
if cacheTTL > 0 {
|
||||
_ = c.Set(ctx, key, raw, ApplyJitter(cacheTTL))
|
||||
setStart := time.Now()
|
||||
setErr := c.Set(ctx, key, raw, ApplyJitterWithRatio(cacheTTL, cacheJitterRatio(c)))
|
||||
recordCacheOperation("readthrough_set", cacheResult(setErr), time.Since(setStart))
|
||||
}
|
||||
} else {
|
||||
recordCacheOperation("readthrough_marshal", "error", time.Duration(0))
|
||||
}
|
||||
|
||||
return envelope, nil
|
||||
@@ -112,11 +128,21 @@ func loadJSONInternal[T any](
|
||||
}
|
||||
|
||||
func ApplyJitter(ttl time.Duration) time.Duration {
|
||||
return ApplyJitterWithRatio(ttl, readthroughDefaultJitterRatio)
|
||||
}
|
||||
|
||||
func ApplyJitterWithRatio(ttl time.Duration, ratio float64) time.Duration {
|
||||
if ttl <= 0 {
|
||||
return ttl
|
||||
}
|
||||
if ratio <= 0 {
|
||||
return ttl
|
||||
}
|
||||
if ratio > 1 {
|
||||
ratio = 1
|
||||
}
|
||||
|
||||
maxJitter := int64(float64(ttl) * defaultJitterRatio)
|
||||
maxJitter := int64(float64(ttl) * ratio)
|
||||
if maxJitter <= 0 {
|
||||
return ttl
|
||||
}
|
||||
@@ -126,3 +152,13 @@ func ApplyJitter(ttl time.Duration) time.Duration {
|
||||
|
||||
return ttl + time.Duration(jitterRand.Int63n(maxJitter+1))
|
||||
}
|
||||
|
||||
func cacheJitterRatio(c Cache) float64 {
|
||||
if c == nil {
|
||||
return readthroughDefaultJitterRatio
|
||||
}
|
||||
if provider, ok := c.(optionsProvider); ok {
|
||||
return provider.CacheOptions().JitterRatio
|
||||
}
|
||||
return readthroughDefaultJitterRatio
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user