126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
// Package cache Redis 缓存与多级缓存封装。
|
|
// MultiLevel:本地 freecache → Redis 两级读取,写/删双层同步(Day 16)。
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/coocood/freecache"
|
|
"github.com/redis/go-redis/v9"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// MultiLevel 两级缓存:本地 freecache(进程内,零网络)优先,Miss 查 Redis。
|
|
// 适用:账号配置/路由规则等读多写少的强读数据;本地 TTL 应短于远端以控制一致性窗口。
|
|
type MultiLevel struct {
|
|
local *freecache.Cache
|
|
remote redis.UniversalClient
|
|
log *zap.Logger
|
|
localTTL time.Duration // 默认本地 TTL
|
|
remoteTTL time.Duration // 默认远端 TTL
|
|
stats MultiLevelStats
|
|
}
|
|
|
|
// MultiLevelStats 命中统计(验收与自观测)。
|
|
type MultiLevelStats struct {
|
|
HitLocal uint64 `json:"hit_local"`
|
|
HitRemote uint64 `json:"hit_remote"`
|
|
Miss uint64 `json:"miss"`
|
|
}
|
|
|
|
// MultiLevelOptions 可选项。
|
|
type MultiLevelOptions struct {
|
|
LocalSizeMB int // 本地缓存容量(默认 16MB)
|
|
LocalTTL time.Duration // 默认本地 TTL(默认 30s)
|
|
RemoteTTL time.Duration // 默认远端 TTL(默认 10min)
|
|
Logger *zap.Logger
|
|
}
|
|
|
|
// NewMultiLevel 构造(rdb 为 nil 时退化为纯本地)。
|
|
func NewMultiLevel(rdb redis.UniversalClient, opt MultiLevelOptions) *MultiLevel {
|
|
if opt.LocalSizeMB <= 0 {
|
|
opt.LocalSizeMB = 16
|
|
}
|
|
if opt.LocalTTL <= 0 {
|
|
opt.LocalTTL = 30 * time.Second
|
|
}
|
|
if opt.RemoteTTL <= 0 {
|
|
opt.RemoteTTL = 10 * time.Minute
|
|
}
|
|
if opt.Logger == nil {
|
|
opt.Logger = zap.NewNop()
|
|
}
|
|
return &MultiLevel{
|
|
local: freecache.NewCache(opt.LocalSizeMB * 1024 * 1024),
|
|
remote: rdb, log: opt.Logger,
|
|
localTTL: opt.LocalTTL, remoteTTL: opt.RemoteTTL,
|
|
}
|
|
}
|
|
|
|
// Get 两级读取:优先本地,Miss 查 Redis;命中远端时回填本地。
|
|
// 返回 (值, 是否命中)。
|
|
func (m *MultiLevel) Get(ctx context.Context, key string) ([]byte, bool) {
|
|
if v, err := m.local.Get([]byte(key)); err == nil {
|
|
m.stats.HitLocal++
|
|
return v, true
|
|
}
|
|
if m.remote == nil {
|
|
m.stats.Miss++
|
|
return nil, false
|
|
}
|
|
v, err := m.remote.Get(ctx, key).Bytes()
|
|
if err == nil {
|
|
m.stats.HitRemote++
|
|
_ = m.local.Set([]byte(key), v, int(m.localTTL.Seconds()))
|
|
return v, true
|
|
}
|
|
m.stats.Miss++
|
|
return nil, false
|
|
}
|
|
|
|
// GetOrLoad 两级读取,全 Miss 时回源 src 并双层回填(负缓存可由调用方空值实现)。
|
|
func (m *MultiLevel) GetOrLoad(ctx context.Context, key string, src func(ctx context.Context) ([]byte, error)) ([]byte, bool, error) {
|
|
if v, ok := m.Get(ctx, key); ok {
|
|
return v, true, nil
|
|
}
|
|
if src == nil {
|
|
return nil, false, nil
|
|
}
|
|
v, err := src(ctx)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
m.Set(ctx, key, v, 0, 0)
|
|
return v, false, nil
|
|
}
|
|
|
|
// Set 双层写入(localTTL/remoteTTL 为 0 时用默认值)。
|
|
func (m *MultiLevel) Set(ctx context.Context, key string, val []byte, localTTL, remoteTTL time.Duration) {
|
|
if localTTL <= 0 {
|
|
localTTL = m.localTTL
|
|
}
|
|
if remoteTTL <= 0 {
|
|
remoteTTL = m.remoteTTL
|
|
}
|
|
_ = m.local.Set([]byte(key), val, int(localTTL.Seconds()))
|
|
if m.remote != nil {
|
|
if err := m.remote.Set(ctx, key, val, remoteTTL).Err(); err != nil {
|
|
m.log.Warn("multilevel_remote_set_failed", zap.String("key", key), zap.Error(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
// Delete 双层删除(远端失败仅告警,本地必删以保证本进程立即失效)。
|
|
func (m *MultiLevel) Delete(ctx context.Context, key string) {
|
|
_ = m.local.Del([]byte(key))
|
|
if m.remote != nil {
|
|
if err := m.remote.Del(ctx, key).Err(); err != nil {
|
|
m.log.Warn("multilevel_remote_del_failed", zap.String("key", key), zap.Error(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
// Stats 返回命中统计快照。
|
|
func (m *MultiLevel) Stats() MultiLevelStats { return m.stats }
|