feat(cache): add read-through cache layer across all app services
Introduce a generic read-through caching infrastructure and wire it into all major tenant app services to reduce database load on hot read paths. Key changes: - Add `DeletePrefix` to Cache interface with memory (prefix scan) and Redis (SCAN + DEL) implementations - New `readthrough.go`: generic `LoadJSON` / `LoadJSONWithEmpty` helpers backed by singleflight to prevent cache stampedes; supports jittered TTL - New `cache_support.go`: centralized cache key builders and invalidation helpers for all entities (workspace, brand, prompt rules, schedule tasks, articles) - Wire optional cache into ArticleService, BrandService, WorkspaceService, PromptRuleService, ScheduleTaskService, TemplateService, MediaService, PromptGenerateService via `WithCache()` builder pattern - ScheduleDispatchWorker invalidates schedule task cache after dispatching - ArticleService gains a new `Detail` endpoint with empty-result caching - Update cmd entrypoints and transport handlers to propagate cache
This commit is contained in:
+1
@@ -12,4 +12,5 @@ type Cache interface {
|
||||
Get(ctx context.Context, key string) ([]byte, error)
|
||||
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
DeletePrefix(ctx context.Context, prefix string) error
|
||||
}
|
||||
|
||||
+15
@@ -58,6 +58,21 @@ func (c *memoryCache) Delete(_ context.Context, key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *memoryCache) DeletePrefix(_ context.Context, prefix string) error {
|
||||
if prefix == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
for key := range c.entries {
|
||||
if len(key) >= len(prefix) && key[:len(prefix)] == prefix {
|
||||
delete(c.entries, key)
|
||||
}
|
||||
}
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *memoryCache) evictLoop() {
|
||||
ticker := time.NewTicker(60 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultJitterRatio = 0.1
|
||||
)
|
||||
|
||||
var (
|
||||
jitterMu sync.Mutex
|
||||
jitterRand = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
)
|
||||
|
||||
type payloadEnvelope[T any] struct {
|
||||
Empty bool `json:"empty,omitempty"`
|
||||
Value T `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func LoadJSON[T any](
|
||||
ctx context.Context,
|
||||
c Cache,
|
||||
group *singleflight.Group,
|
||||
key string,
|
||||
ttl time.Duration,
|
||||
loader func(context.Context) (T, error),
|
||||
) (T, error) {
|
||||
value, _, err := loadJSONInternal(ctx, c, group, key, ttl, 0, func(loadCtx context.Context) (T, bool, error) {
|
||||
value, err := loader(loadCtx)
|
||||
return value, true, err
|
||||
})
|
||||
return value, err
|
||||
}
|
||||
|
||||
func LoadJSONWithEmpty[T any](
|
||||
ctx context.Context,
|
||||
c Cache,
|
||||
group *singleflight.Group,
|
||||
key string,
|
||||
ttl time.Duration,
|
||||
emptyTTL time.Duration,
|
||||
loader func(context.Context) (T, bool, error),
|
||||
) (T, bool, error) {
|
||||
return loadJSONInternal(ctx, c, group, key, ttl, emptyTTL, loader)
|
||||
}
|
||||
|
||||
func loadJSONInternal[T any](
|
||||
ctx context.Context,
|
||||
c Cache,
|
||||
group *singleflight.Group,
|
||||
key string,
|
||||
ttl time.Duration,
|
||||
emptyTTL time.Duration,
|
||||
loader func(context.Context) (T, bool, error),
|
||||
) (T, bool, error) {
|
||||
var zero T
|
||||
if c == nil {
|
||||
return loader(ctx)
|
||||
}
|
||||
|
||||
if raw, err := c.Get(ctx, key); err == nil {
|
||||
var envelope payloadEnvelope[T]
|
||||
if unmarshalErr := json.Unmarshal(raw, &envelope); unmarshalErr == nil {
|
||||
if envelope.Empty {
|
||||
return zero, false, nil
|
||||
}
|
||||
return envelope.Value, true, nil
|
||||
}
|
||||
}
|
||||
|
||||
result, err, _ := group.Do(key, func() (interface{}, error) {
|
||||
value, found, loadErr := loader(ctx)
|
||||
if loadErr != nil {
|
||||
return nil, loadErr
|
||||
}
|
||||
|
||||
envelope := payloadEnvelope[T]{
|
||||
Empty: !found,
|
||||
Value: value,
|
||||
}
|
||||
if raw, marshalErr := json.Marshal(envelope); marshalErr == nil {
|
||||
cacheTTL := ttl
|
||||
if !found {
|
||||
cacheTTL = emptyTTL
|
||||
}
|
||||
if cacheTTL > 0 {
|
||||
_ = c.Set(ctx, key, raw, ApplyJitter(cacheTTL))
|
||||
}
|
||||
}
|
||||
|
||||
return envelope, nil
|
||||
})
|
||||
if err != nil {
|
||||
return zero, false, err
|
||||
}
|
||||
|
||||
envelope, ok := result.(payloadEnvelope[T])
|
||||
if !ok {
|
||||
return zero, false, nil
|
||||
}
|
||||
if envelope.Empty {
|
||||
return zero, false, nil
|
||||
}
|
||||
return envelope.Value, true, nil
|
||||
}
|
||||
|
||||
func ApplyJitter(ttl time.Duration) time.Duration {
|
||||
if ttl <= 0 {
|
||||
return ttl
|
||||
}
|
||||
|
||||
maxJitter := int64(float64(ttl) * defaultJitterRatio)
|
||||
if maxJitter <= 0 {
|
||||
return ttl
|
||||
}
|
||||
|
||||
jitterMu.Lock()
|
||||
defer jitterMu.Unlock()
|
||||
|
||||
return ttl + time.Duration(jitterRand.Int63n(maxJitter+1))
|
||||
}
|
||||
+24
@@ -31,3 +31,27 @@ func (c *redisCache) Set(ctx context.Context, key string, value []byte, ttl time
|
||||
func (c *redisCache) Delete(ctx context.Context, key string) error {
|
||||
return c.client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func (c *redisCache) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
if prefix == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
pattern := prefix + "*"
|
||||
var cursor uint64
|
||||
for {
|
||||
keys, nextCursor, err := c.client.Scan(ctx, cursor, pattern, 100).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(keys) > 0 {
|
||||
if err := c.client.Del(ctx, keys...).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
cursor = nextCursor
|
||||
if cursor == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user