cache

package
v0.0.0-...-3be1fcf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func NewMemCache(ctx context.Context, expire time.Duration, opts ...MemCacheOption) *MemCache

NewMemCache 创建并返回一个内存缓存实例。 ctx 由缓存实例持有,用于后台统计日志关联链路信息; expire 为默认过期时间;通过选项可定制容量上限、名称等。

mc := cache.NewMemCache(ctx, time.Minute, cache.WithLimit(1000))

func (*MemCache) Close

func (c *MemCache) Close()

Close 停止后台统计 goroutine。

func (*MemCache) Decrement

func (c *MemCache) Decrement(ctx context.Context, key string, delta int64) error

Decrement 将 key 对应的数值自减 delta;key 不存在时初始化为 -delta。 ctx 在本实现中被忽略。

func (*MemCache) Delete

func (c *MemCache) Delete(ctx context.Context, keys ...string) error

Delete 删除一个或多个 key。 ctx 在本实现中被忽略。

func (*MemCache) Expire

func (c *MemCache) Expire(ctx context.Context, key string, ttl time.Duration) error

Expire 为 key 设置存活时间 ttl,到期后自动失效。 ttl <= 0 时立即失效(删除该 key);key 不存在返回 ErrNotFound。 ctx 在本实现中被忽略。

func (*MemCache) Get

func (c *MemCache) Get(ctx context.Context, key string) (any, error)

Get 返回指定 key 的值;未命中或已过期返回 ErrNotFound。 ctx 在本实现中被忽略。

func (*MemCache) Increment

func (c *MemCache) Increment(ctx context.Context, key string, delta int64) error

Increment 将 key 对应的数值自增 delta;key 不存在时初始化为 delta。 ctx 在本实现中被忽略。

func (*MemCache) Set

func (c *MemCache) Set(ctx context.Context, key string, value any) error

Set 将 value 存入缓存,使用默认过期时间。 ctx 在本实现中被忽略。

func (*MemCache) SetEx

func (c *MemCache) SetEx(ctx context.Context, key string, value any, ttl time.Duration) error

SetEx 将 value 存入缓存并指定存活时间 ttl。 ctx 在本实现中被忽略。

func (*MemCache) Size

func (c *MemCache) Size() int

Size 返回当前缓存元素数量。

func (*MemCache) Take

func (c *MemCache) Take(ctx context.Context, key string, fetch func() (any, error)) (any, error)

Take 返回 key 的值;未命中时调用 fetch 获取并写入缓存,防缓存击穿。 ctx 在本实现中被忽略。

type MemCacheOption

type MemCacheOption func(*memCacheOptions)

MemCacheOption 用于自定义内存缓存行为。

func WithLimit

func WithLimit(limit int) MemCacheOption

WithLimit 设置缓存容量上限,超出后按 LRU 淘汰最久未使用的 key。 limit <= 0 表示不限制容量(默认)。

func WithName

func WithName(name string) MemCacheOption

WithName 设置缓存名称,用于统计日志标识。

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

func (c *RedisCache) Decrement(ctx context.Context, key string, delta int64) error

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

func (c *RedisCache) Expire(ctx context.Context, key string, ttl time.Duration) error

Expire 为 key 设置存活时间 ttl,到期后自动失效。 ttl <= 0 时立即失效(删除该 key);key 不存在返回 ErrNotFound。 注意:Redis EXPIRE 精度为秒级,此处直接透传 ttl,不加抖动(避免被截断为 0 秒立即删除)。

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) (any, error)

Get 返回指定 key 的值;未命中或命中占位符返回 ErrNotFound。

func (*RedisCache) Increment

func (c *RedisCache) Increment(ctx context.Context, key string, delta int64) error

Increment 将 key 对应的数值自增 delta;key 不存在时初始化为 delta。 底层使用 Redis INCRBY,原子操作。

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value any) error

Set 将 value 写入缓存,使用默认过期时间。

func (*RedisCache) SetEx

func (c *RedisCache) SetEx(ctx context.Context, key string, value any, ttl time.Duration) error

SetEx 将 value 写入缓存并指定存活时间 ttl。

func (*RedisCache) Take

func (c *RedisCache) Take(ctx context.Context, key string, fetch func() (any, error)) (any, error)

Take 返回 key 的值;未命中时调用 fetch 获取并写入缓存。 防击穿:相同 key 并发只执行一次 fetch; 防穿透:fetch 返回 ErrNotFound 时写入短时占位符; 快速失败:Redis 故障时不穿透到 DB。

type RedisCacheOption

type RedisCacheOption func(*redisCacheOptions)

RedisCacheOption 用于自定义 Redis 缓存行为。

func WithCacheName

func WithCacheName(name string) RedisCacheOption

WithCacheName 设置缓存名称,用于日志标识。

func WithExpire

func WithExpire(d time.Duration) RedisCacheOption

WithExpire 设置默认过期时间。 未设置时默认 7 天。

func WithNotFoundExpire

func WithNotFoundExpire(d time.Duration) RedisCacheOption

WithNotFoundExpire 设置未命中占位符的过期时间。 占位符用于防缓存穿透:查询无结果时短暂缓存"不存在"标记。 未设置时默认 1 分钟。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL