2026-04-15 16:11:05 +08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2026-05-03 02:02:39 +08:00
|
|
|
"errors"
|
2026-04-15 16:11:05 +08:00
|
|
|
"math/rand"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/sync/singleflight"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2026-05-03 02:02:39 +08:00
|
|
|
readthroughDefaultJitterRatio = 0.1
|
2026-04-15 16:11:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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 {
|
2026-05-03 02:02:39 +08:00
|
|
|
recordCacheOperation("readthrough_load", "cache_disabled", time.Duration(0))
|
2026-04-15 16:11:05 +08:00
|
|
|
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 {
|
2026-05-03 02:02:39 +08:00
|
|
|
recordCacheOperation("readthrough_empty", "hit", time.Duration(0))
|
2026-04-15 16:11:05 +08:00
|
|
|
return zero, false, nil
|
|
|
|
|
}
|
2026-05-03 02:02:39 +08:00
|
|
|
recordCacheOperation("readthrough_hit", "ok", time.Duration(0))
|
2026-04-15 16:11:05 +08:00
|
|
|
return envelope.Value, true, nil
|
|
|
|
|
}
|
2026-05-03 02:02:39 +08:00
|
|
|
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))
|
2026-04-15 16:11:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err, _ := group.Do(key, func() (interface{}, error) {
|
2026-05-03 02:02:39 +08:00
|
|
|
start := time.Now()
|
2026-04-15 16:11:05 +08:00
|
|
|
value, found, loadErr := loader(ctx)
|
2026-05-03 02:02:39 +08:00
|
|
|
recordCacheOperation("readthrough_load", cacheResult(loadErr), time.Since(start))
|
2026-04-15 16:11:05 +08:00
|
|
|
if loadErr != nil {
|
|
|
|
|
return nil, loadErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
envelope := payloadEnvelope[T]{
|
|
|
|
|
Empty: !found,
|
|
|
|
|
Value: value,
|
|
|
|
|
}
|
|
|
|
|
if raw, marshalErr := json.Marshal(envelope); marshalErr == nil {
|
2026-05-03 02:02:39 +08:00
|
|
|
recordCacheOperation("readthrough_marshal", "ok", time.Duration(0))
|
2026-04-15 16:11:05 +08:00
|
|
|
cacheTTL := ttl
|
|
|
|
|
if !found {
|
|
|
|
|
cacheTTL = emptyTTL
|
|
|
|
|
}
|
|
|
|
|
if cacheTTL > 0 {
|
2026-05-03 02:02:39 +08:00
|
|
|
setStart := time.Now()
|
|
|
|
|
setErr := c.Set(ctx, key, raw, ApplyJitterWithRatio(cacheTTL, cacheJitterRatio(c)))
|
|
|
|
|
recordCacheOperation("readthrough_set", cacheResult(setErr), time.Since(setStart))
|
2026-04-15 16:11:05 +08:00
|
|
|
}
|
2026-05-03 02:02:39 +08:00
|
|
|
} else {
|
|
|
|
|
recordCacheOperation("readthrough_marshal", "error", time.Duration(0))
|
2026-04-15 16:11:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2026-05-03 02:02:39 +08:00
|
|
|
return ApplyJitterWithRatio(ttl, readthroughDefaultJitterRatio)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ApplyJitterWithRatio(ttl time.Duration, ratio float64) time.Duration {
|
2026-04-15 16:11:05 +08:00
|
|
|
if ttl <= 0 {
|
|
|
|
|
return ttl
|
|
|
|
|
}
|
2026-05-03 02:02:39 +08:00
|
|
|
if ratio <= 0 {
|
|
|
|
|
return ttl
|
|
|
|
|
}
|
|
|
|
|
if ratio > 1 {
|
|
|
|
|
ratio = 1
|
|
|
|
|
}
|
2026-04-15 16:11:05 +08:00
|
|
|
|
2026-05-03 02:02:39 +08:00
|
|
|
maxJitter := int64(float64(ttl) * ratio)
|
2026-04-15 16:11:05 +08:00
|
|
|
if maxJitter <= 0 {
|
|
|
|
|
return ttl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jitterMu.Lock()
|
|
|
|
|
defer jitterMu.Unlock()
|
|
|
|
|
|
|
|
|
|
return ttl + time.Duration(jitterRand.Int63n(maxJitter+1))
|
|
|
|
|
}
|
2026-05-03 02:02:39 +08:00
|
|
|
|
|
|
|
|
func cacheJitterRatio(c Cache) float64 {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return readthroughDefaultJitterRatio
|
|
|
|
|
}
|
|
|
|
|
if provider, ok := c.(optionsProvider); ok {
|
|
|
|
|
return provider.CacheOptions().JitterRatio
|
|
|
|
|
}
|
|
|
|
|
return readthroughDefaultJitterRatio
|
|
|
|
|
}
|