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:
+60
-5
@@ -3,17 +3,19 @@ package cache
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type redisCache struct {
|
||||
client *goredis.Client
|
||||
client *goredis.Client
|
||||
deleteScanCount int64
|
||||
}
|
||||
|
||||
func newRedisCache(client *goredis.Client) Cache {
|
||||
return &redisCache{client: client}
|
||||
func newRedisCache(client *goredis.Client, deleteScanCount int64) Cache {
|
||||
return &redisCache{client: client, deleteScanCount: deleteScanCount}
|
||||
}
|
||||
|
||||
func (c *redisCache) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
@@ -24,28 +26,81 @@ func (c *redisCache) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
return val, err
|
||||
}
|
||||
|
||||
func (c *redisCache) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error) {
|
||||
if len(keys) == 0 {
|
||||
return map[string][]byte{}, nil
|
||||
}
|
||||
|
||||
values, err := c.client.MGet(ctx, keys...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[string][]byte, len(keys))
|
||||
for idx, value := range values {
|
||||
if value == nil {
|
||||
continue
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
result[keys[idx]] = []byte(typed)
|
||||
case []byte:
|
||||
dst := make([]byte, len(typed))
|
||||
copy(dst, typed)
|
||||
result[keys[idx]] = dst
|
||||
default:
|
||||
result[keys[idx]] = []byte(fmt.Sprint(value))
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *redisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
|
||||
return c.client.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
func (c *redisCache) SetMulti(ctx context.Context, items []Item) error {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
pipe := c.client.Pipeline()
|
||||
for _, item := range items {
|
||||
pipe.Set(ctx, item.Key, item.Value, item.TTL)
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *redisCache) Delete(ctx context.Context, key string) error {
|
||||
return c.client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func (c *redisCache) DeleteMany(ctx context.Context, keys []string) error {
|
||||
if len(keys) == 0 {
|
||||
return nil
|
||||
}
|
||||
return c.client.Del(ctx, keys...).Err()
|
||||
}
|
||||
|
||||
func (c *redisCache) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
if prefix == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
pattern := prefix + "*"
|
||||
scanCount := c.deleteScanCount
|
||||
if scanCount <= 0 {
|
||||
scanCount = defaultDeleteScanCount
|
||||
}
|
||||
var cursor uint64
|
||||
for {
|
||||
keys, nextCursor, err := c.client.Scan(ctx, cursor, pattern, 100).Result()
|
||||
keys, nextCursor, err := c.client.Scan(ctx, cursor, pattern, scanCount).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(keys) > 0 {
|
||||
if err := c.client.Del(ctx, keys...).Err(); err != nil {
|
||||
if err := c.DeleteMany(ctx, keys); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user