Documentation
¶
Overview ¶
Package cache 提供缓存抽象层。
核心功能:
- 多级缓存(内存 + Redis)
- 缓存穿透/击穿/雪崩防护
- 序列化策略(JSON/MessagePack)
- 统计信息(命中率、键数量)
使用示例:
c := cache.NewRedis(client, cache.WithPrefix("app:"))
err := c.Set(ctx, "key", value, time.Hour)
err = c.Get(ctx, "key", &dest)
Index ¶
- Variables
- type BatchCache
- type Cache
- type JSONSerializer
- type LoadFunc
- type Loader
- type Memory
- func (m *Memory) Close() error
- func (m *Memory) Delete(ctx context.Context, keys ...string) error
- func (m *Memory) Exists(ctx context.Context, key string) (bool, error)
- func (m *Memory) Get(ctx context.Context, key string, dest any) error
- func (m *Memory) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (m *Memory) Stats() Stats
- type MemoryConfig
- type Metriced
- func (m *Metriced) Close() error
- func (m *Metriced) Delete(ctx context.Context, keys ...string) error
- func (m *Metriced) Exists(ctx context.Context, key string) (bool, error)
- func (m *Metriced) Get(ctx context.Context, key string, dest any) error
- func (m *Metriced) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (m *Metriced) Unwrap() Cache
- func (m *Metriced) UpdateHitRate()
- type Metrics
- type MsgpackSerializer
- type MultiLevel
- func (m *MultiLevel) Close() error
- func (m *MultiLevel) Delete(ctx context.Context, keys ...string) error
- func (m *MultiLevel) Exists(ctx context.Context, key string) (bool, error)
- func (m *MultiLevel) Get(ctx context.Context, key string, dest any) error
- func (m *MultiLevel) GetOrLoad(ctx context.Context, key string, dest any, loader LoadFunc, ttl time.Duration) error
- func (m *MultiLevel) L1() Cache
- func (m *MultiLevel) L2() Cache
- func (m *MultiLevel) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (m *MultiLevel) Stats() Stats
- type MultiLevelConfig
- type Option
- type Options
- type Redis
- func (r *Redis) Client() *redis.Client
- func (r *Redis) Close() error
- func (r *Redis) Delete(ctx context.Context, keys ...string) error
- func (r *Redis) Exists(ctx context.Context, key string) (bool, error)
- func (r *Redis) Get(ctx context.Context, key string, dest any) error
- func (r *Redis) MDelete(ctx context.Context, keys []string) error
- func (r *Redis) MGet(ctx context.Context, keys []string) (map[string][]byte, error)
- func (r *Redis) MSet(ctx context.Context, items map[string]any, ttl time.Duration) error
- func (r *Redis) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type RedisConfig
- type Serializer
- type Stats
- type StatsProvider
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNotFound = errors.New("cache: key not found") ErrNil = errors.New("cache: nil value") ErrTypeMismatch = errors.New("cache: type mismatch") )
预定义错误
Functions ¶
This section is empty.
Types ¶
type BatchCache ¶
type BatchCache interface {
Cache
// MGet 批量获取
MGet(ctx context.Context, keys []string) (map[string][]byte, error)
// MSet 批量设置
MSet(ctx context.Context, items map[string]any, ttl time.Duration) error
// MDelete 批量删除
MDelete(ctx context.Context, keys []string) error
}
BatchCache 批量操作接口
type Cache ¶
type Cache interface {
// Get 获取缓存
Get(ctx context.Context, key string, dest any) error
// Set 设置缓存
Set(ctx context.Context, key string, value any, ttl time.Duration) error
// Delete 删除缓存
Delete(ctx context.Context, keys ...string) error
// Exists 检查是否存在
Exists(ctx context.Context, key string) (bool, error)
// Close 关闭缓存
Close() error
}
Cache 缓存接口
type Loader ¶
type Loader interface {
Cache
// GetOrLoad 获取或加载
GetOrLoad(ctx context.Context, key string, dest any, loader LoadFunc, ttl time.Duration) error
}
Loader 带加载功能的缓存
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory 内存缓存
type MemoryConfig ¶
MemoryConfig 内存缓存配置
type Metriced ¶
type Metriced struct {
// contains filtered or unexported fields
}
Metriced 指标装饰器
func NewMetriced ¶
NewMetriced 创建指标装饰器
type Metrics ¶
type Metrics struct {
OpsTotal observability.Counter
OpsDuration observability.Histogram
HitRate observability.Gauge
}
Metrics 缓存指标
type MultiLevel ¶
type MultiLevel struct {
// contains filtered or unexported fields
}
MultiLevel 多级缓存
func NewMultiLevel ¶
func NewMultiLevel(cfg MultiLevelConfig, opts ...Option) *MultiLevel
NewMultiLevel 创建多级缓存
func NewMultiLevelCache ¶
func NewMultiLevelCache(redisAddr, password string, db int) (*MultiLevel, error)
NewMultiLevelCache 便捷构造器
func (*MultiLevel) Close ¶
func (m *MultiLevel) Close() error
func (*MultiLevel) GetOrLoad ¶
func (m *MultiLevel) GetOrLoad(ctx context.Context, key string, dest any, loader LoadFunc, ttl time.Duration) error
GetOrLoad 获取或加载(防击穿)
func (*MultiLevel) Stats ¶
func (m *MultiLevel) Stats() Stats
type Options ¶
type Options struct {
Prefix string
Serializer Serializer
DefaultTTL time.Duration
NullTTL time.Duration // 空值缓存时间(防穿透)
EnableStats bool
}
Options 缓存配置
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis Redis 缓存
func NewRedisFromClient ¶
NewRedisFromClient 从已有客户端创建
type RedisConfig ¶
RedisConfig Redis 配置
type Serializer ¶
Serializer 序列化器接口
Click to show internal directories.
Click to hide internal directories.