Documentation
¶
Index ¶
- Variables
- type Cache
- type MemCache
- func (c *MemCache) Close()
- func (c *MemCache) Decrement(ctx context.Context, key string, delta int64) error
- func (c *MemCache) Delete(ctx context.Context, keys ...string) error
- func (c *MemCache) Expire(ctx context.Context, key string, ttl time.Duration) error
- func (c *MemCache) Get(ctx context.Context, key string) (any, error)
- func (c *MemCache) Increment(ctx context.Context, key string, delta int64) error
- func (c *MemCache) Set(ctx context.Context, key string, value any) error
- func (c *MemCache) SetEx(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *MemCache) Size() int
- func (c *MemCache) Take(ctx context.Context, key string, fetch func() (any, error)) (any, error)
- type MemCacheOption
- type RedisCache
- func (c *RedisCache) Decrement(ctx context.Context, key string, delta int64) error
- func (c *RedisCache) Delete(ctx context.Context, keys ...string) error
- func (c *RedisCache) Expire(ctx context.Context, key string, ttl time.Duration) error
- func (c *RedisCache) Get(ctx context.Context, key string) (any, error)
- func (c *RedisCache) Increment(ctx context.Context, key string, delta int64) error
- func (c *RedisCache) Set(ctx context.Context, key string, value any) error
- func (c *RedisCache) SetEx(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *RedisCache) Take(ctx context.Context, key string, fetch func() (any, error)) (any, error)
- type RedisCacheOption
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("cache: key not found")
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, key string) (any, error)
Set(ctx context.Context, key string, value any) error
SetEx(ctx context.Context, key string, value any, ttl time.Duration) error
Delete(ctx context.Context, keys ...string) error
Take(ctx context.Context, key string, fetch func() (any, error)) (any, error)
// Increment 将 key 对应的数值自增 delta;key 不存在时初始化为 delta。
Increment(ctx context.Context, key string, delta int64) error
// Decrement 将 key 对应的数值自减 delta;key 不存在时初始化为 -delta。
Decrement(ctx context.Context, key string, delta int64) error
// Expire 为 key 设置存活时间 ttl,到期后自动失效;ttl <= 0 时立即失效。
Expire(ctx context.Context, key string, ttl time.Duration) error
}
type MemCache ¶
type MemCache struct {
// contains filtered or unexported fields
}
MemCache 是 Cache 接口的内存实现。
特性:
- 过期删除:惰性删除(Get 命中时检查)+ 全局定期扫描,避免每 key 一个定时器
- LRU 淘汰:容量受限时淘汰最久未使用的 key
- 防击穿:Take 通过 SingleFlight 合并相同 key 的并发请求
- 命中率统计:周期性输出 QPS、命中率、元素数量
func NewMemCache ¶
NewMemCache 创建并返回一个内存缓存实例。 ctx 由缓存实例持有,用于后台统计日志关联链路信息; expire 为默认过期时间;通过选项可定制容量上限、名称等。
mc := cache.NewMemCache(ctx, time.Minute, cache.WithLimit(1000))
func (*MemCache) Expire ¶
Expire 为 key 设置存活时间 ttl,到期后自动失效。 ttl <= 0 时立即失效(删除该 key);key 不存在返回 ErrNotFound。 ctx 在本实现中被忽略。
type MemCacheOption ¶
type MemCacheOption func(*memCacheOptions)
MemCacheOption 用于自定义内存缓存行为。
func WithLimit ¶
func WithLimit(limit int) MemCacheOption
WithLimit 设置缓存容量上限,超出后按 LRU 淘汰最久未使用的 key。 limit <= 0 表示不限制容量(默认)。
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache 是 Cache 接口的 Redis 实现。
特性:
- 值以 JSON 序列化存储
- 防缓存击穿:Take 通过 SingleFlight 合并相同 key 的并发请求
- 防缓存穿透:查询无结果时写入短时占位符
- 防缓存雪崩:过期时间带轻微抖动
- 快速失败:Redis 故障时不穿透到 DB
内存实现见 MemCache。两者均实现 Cache 接口,可在本地/分布式间切换。
func NewRedisCache ¶
func NewRedisCache(rds *redisx.Client, opts ...RedisCacheOption) *RedisCache
NewRedisCache 创建并返回一个基于 Redis 的缓存实例。 rds 为 redisx 客户端;通过选项可定制默认过期时间、占位符过期时间、名称等。
var c cache.Cache = cache.NewRedisCache(rds, cache.WithExpire(time.Minute))
func (*RedisCache) Decrement ¶
Decrement 将 key 对应的数值自减 delta;key 不存在时初始化为 -delta。 底层使用 Redis INCRBY 传负值,原子操作。
func (*RedisCache) Delete ¶
func (c *RedisCache) Delete(ctx context.Context, keys ...string) error
Delete 删除一个或多个 key。
func (*RedisCache) Expire ¶
Expire 为 key 设置存活时间 ttl,到期后自动失效。 ttl <= 0 时立即失效(删除该 key);key 不存在返回 ErrNotFound。 注意:Redis EXPIRE 精度为秒级,此处直接透传 ttl,不加抖动(避免被截断为 0 秒立即删除)。
func (*RedisCache) Increment ¶
Increment 将 key 对应的数值自增 delta;key 不存在时初始化为 delta。 底层使用 Redis INCRBY,原子操作。
type RedisCacheOption ¶
type RedisCacheOption func(*redisCacheOptions)
RedisCacheOption 用于自定义 Redis 缓存行为。
func WithExpire ¶
func WithExpire(d time.Duration) RedisCacheOption
WithExpire 设置默认过期时间。 未设置时默认 7 天。
func WithNotFoundExpire ¶
func WithNotFoundExpire(d time.Duration) RedisCacheOption
WithNotFoundExpire 设置未命中占位符的过期时间。 占位符用于防缓存穿透:查询无结果时短暂缓存"不存在"标记。 未设置时默认 1 分钟。