Documentation
¶
Index ¶
- func IsNilMarker(v interface{}) bool
- func UnwrapNilMarker(v interface{}) interface{}
- func WrapNilMarker(v interface{}) interface{}
- type BackendFactory
- type CacheBackend
- type CacheConfig
- type CacheItem
- type CacheManager
- type CacheProtection
- func (p *CacheProtection) ApplyAvalancheProtection(baseTTL time.Duration) time.Duration
- func (p *CacheProtection) ApplyBreakdownProtection(ctx context.Context, key string, fn func() (interface{}, error)) (interface{}, error, bool)
- func (p *CacheProtection) ApplyPenetrationProtection(value interface{}) (interface{}, bool)
- func (p *CacheProtection) CalculateTTLWithJitter(baseTTL time.Duration, jitterFactor float64) time.Duration
- func (p *CacheProtection) CancelSingleFlight(key string)
- func (p *CacheProtection) GetEmptyValueTTL() time.Duration
- func (p *CacheProtection) GetProtectionConfig() *ProtectionConfig
- func (p *CacheProtection) GetStats() *ProtectionStats
- func (p *CacheProtection) ProtectedGet(ctx context.Context, key string, cacheGet func() (interface{}, bool, error), ...) (interface{}, error)
- func (p *CacheProtection) SingleFlightDo(key string, fn func() (interface{}, error)) (interface{}, error, bool)
- func (p *CacheProtection) UnwrapFromStorage(value interface{}) interface{}
- func (p *CacheProtection) WrapForStorage(value interface{}) interface{}
- type CacheStats
- type CacheWarmer
- func (w *CacheWarmer) AddEntries(entries []WarmUpEntry)
- func (w *CacheWarmer) AddEntry(entry WarmUpEntry)
- func (w *CacheWarmer) Clear()
- func (w *CacheWarmer) GetStats() *WarmerStats
- func (w *CacheWarmer) WarmUp(ctx context.Context, entries []WarmUpEntry) error
- func (w *CacheWarmer) WarmUpAll(ctx context.Context) error
- func (w *CacheWarmer) WarmUpConcurrent(ctx context.Context, entries []WarmUpEntry, concurrency int) error
- func (w *CacheWarmer) WarmUpWithConfig(ctx context.Context, entries []WarmUpEntry, config *WarmerConfig) error
- type LoaderFunc
- type MethodMeta
- type ProtectionConfig
- type ProtectionStats
- type StatsCounter
- type WarmUpBuilder
- func (b *WarmUpBuilder) Add(cache, key string, loader LoaderFunc, ttl time.Duration) *WarmUpBuilder
- func (b *WarmUpBuilder) AddWithTimeout(cache, key string, loader LoaderFunc, ttl, timeout time.Duration) *WarmUpBuilder
- func (b *WarmUpBuilder) Build() *CacheWarmer
- func (b *WarmUpBuilder) Execute(ctx context.Context, concurrency int) error
- type WarmUpEntry
- type WarmerConfig
- type WarmerStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CacheManager ¶
type CacheManager interface {
GetCache(name string) (CacheBackend, error)
RegisterBackend(name string, factory BackendFactory) error
Execute(ctx context.Context, meta *MethodMeta, args []reflect.Value) (interface{}, error)
Close() error
GetProtection() *CacheProtection
SetProtectionConfig(config *ProtectionConfig) error
// Invalidate 使缓存失效
Invalidate(ctx context.Context, cache string, key string) error
}
CacheManager 缓存管理器接口
type CacheProtection ¶
type CacheProtection struct {
// contains filtered or unexported fields
}
CacheProtection 缓存异常保护器
func NewCacheProtection ¶
func NewCacheProtection(config *ProtectionConfig) *CacheProtection
NewCacheProtection 创建缓存保护器
func (*CacheProtection) ApplyAvalancheProtection ¶
func (p *CacheProtection) ApplyAvalancheProtection(baseTTL time.Duration) time.Duration
ApplyAvalancheProtection 应用雪崩保护(计算带抖动的 TTL)
func (*CacheProtection) ApplyBreakdownProtection ¶
func (p *CacheProtection) ApplyBreakdownProtection(ctx context.Context, key string, fn func() (interface{}, error)) (interface{}, error, bool)
ApplyBreakdownProtection 应用击穿保护(singleflight) key: 缓存键 fn: 实际的数据获取函数 返回:(结果,错误,是否从 singleflight 获取)
func (*CacheProtection) ApplyPenetrationProtection ¶
func (p *CacheProtection) ApplyPenetrationProtection(value interface{}) (interface{}, bool)
ApplyPenetrationProtection 应用穿透保护 如果值是否为空值标记,返回 true 表示应该视为缓存未命中
func (*CacheProtection) CalculateTTLWithJitter ¶
func (p *CacheProtection) CalculateTTLWithJitter(baseTTL time.Duration, jitterFactor float64) time.Duration
CalculateTTLWithJitter 计算带抖动的 TTL(便捷方法)
func (*CacheProtection) CancelSingleFlight ¶
func (p *CacheProtection) CancelSingleFlight(key string)
CancelSingleFlight 取消正在进行的 singleflight 请求(通过删除 key) 注意:这不会取消正在执行的函数,只会移除 singleflight 的缓存
func (*CacheProtection) GetEmptyValueTTL ¶
func (p *CacheProtection) GetEmptyValueTTL() time.Duration
GetEmptyValueTTL 获取空值缓存的 TTL
func (*CacheProtection) GetProtectionConfig ¶
func (p *CacheProtection) GetProtectionConfig() *ProtectionConfig
GetProtectionConfig 获取保护配置
func (*CacheProtection) GetStats ¶
func (p *CacheProtection) GetStats() *ProtectionStats
GetStats 获取保护统计(当前实现返回空统计,可扩展)
func (*CacheProtection) ProtectedGet ¶
func (p *CacheProtection) ProtectedGet( ctx context.Context, key string, cacheGet func() (interface{}, bool, error), cacheMissFn func() (interface{}, error), cacheSet func(interface{}, time.Duration) error, ) (interface{}, error)
ProtectedGet 受保护的缓存获取操作 整合穿透保护和击穿保护
func (*CacheProtection) SingleFlightDo ¶
func (p *CacheProtection) SingleFlightDo(key string, fn func() (interface{}, error)) (interface{}, error, bool)
SingleFlightDo 直接使用 singleflight(高级用法)
func (*CacheProtection) UnwrapFromStorage ¶
func (p *CacheProtection) UnwrapFromStorage(value interface{}) interface{}
UnwrapFromStorage 从存储解包值
func (*CacheProtection) WrapForStorage ¶
func (p *CacheProtection) WrapForStorage(value interface{}) interface{}
WrapForStorage 包装值用于存储(处理 nil 值)
type CacheWarmer ¶
type CacheWarmer struct {
// contains filtered or unexported fields
}
CacheWarmer 缓存预热器
func (*CacheWarmer) AddEntries ¶
func (w *CacheWarmer) AddEntries(entries []WarmUpEntry)
AddEntries 批量添加预热条目
func (*CacheWarmer) WarmUp ¶
func (w *CacheWarmer) WarmUp(ctx context.Context, entries []WarmUpEntry) error
WarmUp 执行缓存预热
func (*CacheWarmer) WarmUpAll ¶
func (w *CacheWarmer) WarmUpAll(ctx context.Context) error
WarmUpAll 预热所有已添加的条目
func (*CacheWarmer) WarmUpConcurrent ¶
func (w *CacheWarmer) WarmUpConcurrent(ctx context.Context, entries []WarmUpEntry, concurrency int) error
WarmUpConcurrent 并发预热(推荐用于生产环境)
func (*CacheWarmer) WarmUpWithConfig ¶
func (w *CacheWarmer) WarmUpWithConfig(ctx context.Context, entries []WarmUpEntry, config *WarmerConfig) error
WarmUpWithConfig 使用配置执行缓存预热
type MethodMeta ¶
type MethodMeta struct {
CacheName, KeyExpr, TTLExpr, Condition, Unless, CacheType string
Sync, Before bool
}
MethodMeta 方法元数据
type ProtectionConfig ¶
type ProtectionConfig struct {
// 穿透保护配置
EnablePenetrationProtection bool // 是否启用穿透保护
EmptyValueTTL time.Duration // 空值缓存 TTL(默认 5 分钟)
// 击穿保护配置
EnableBreakdownProtection bool // 是否启用击穿保护(singleflight)
// 雪崩保护配置
EnableAvalancheProtection bool // 是否启用雪崩保护
TTLJitterFactor float64 // TTL 随机偏移因子(0.0-0.5,默认 0.1 即 10%)
}
ProtectionConfig 缓存保护配置
func DefaultProtectionConfig ¶
func DefaultProtectionConfig() *ProtectionConfig
DefaultProtectionConfig 默认保护配置
type ProtectionStats ¶
type ProtectionStats struct {
PenetrationBlocked int64 // 穿透拦截次数
BreakdownMerged int64 // 击穿合并次数
AvalancheJittered int64 // 雪崩抖动次数
}
ProtectionStats 保护机制统计
type WarmUpBuilder ¶
type WarmUpBuilder struct {
// contains filtered or unexported fields
}
WarmUpBuilder 预热器构建器(流式 API)
func NewWarmUpBuilder ¶
func NewWarmUpBuilder(manager CacheManager) *WarmUpBuilder
NewWarmUpBuilder 创建预热器构建器
func (*WarmUpBuilder) Add ¶
func (b *WarmUpBuilder) Add(cache, key string, loader LoaderFunc, ttl time.Duration) *WarmUpBuilder
Add 添加预热条目
func (*WarmUpBuilder) AddWithTimeout ¶
func (b *WarmUpBuilder) AddWithTimeout(cache, key string, loader LoaderFunc, ttl, timeout time.Duration) *WarmUpBuilder
AddWithTimeout 添加带超时的预热条目
type WarmUpEntry ¶
type WarmUpEntry struct {
Cache string // 缓存名称
Key string // 缓存键
Loader LoaderFunc // 数据加载函数
TTL time.Duration // TTL
}
WarmUpEntry 预热条目