cache

package
v1.22.8 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 5 Imported by: 6

README

cache

Redis 相关能力分两层:

组件 用途
cache.C 带本地 LFU 的对象缓存(Get/Set/Exists),如 token 校验
cache.RedisContext 裸 Redis 命令(KV、pub/sub),通过 app.GetRedis() 获取

RedisContext

EnableCache 启动后会注册 RedisSession;业务使用:

r := app.GetRedis()
val, err := r.Get(ctx, "key")
if cache.IsNotFound(err) {
    // key 不存在(内部已映射 redis.Nil)
}
_ = r.Set(ctx, "key", "value", time.Minute)
_ = r.Publish(ctx, "app:logs", line)

或使用对称 helper:

app.Redis(func(r cache.RedisContext) error {
    return r.Set(ctx, "k", "v", time.Minute)
})

错误处理

两条路径,不要混用判断函数
路径 你拿到的 err 该怎么判断
框架 APIRedisContext / cache.C 已在边界 NormalizeErr,为 cache.ErrNotFound cache.IsNotFound(err)
直接用 go-redisclient.Get 等,未走本包封装) 原始 redis.Nil cache.IsRedisNil(err)

框架 API 在 redis_session.goredis.go 出口统一调用 NormalizeErr不会redis.Nil 原样抛给业务。

cache.ErrNotFound 是包内归一化哨兵(errors.New("cache: not found")),供 NormalizeErr 返回、IsNotFound 比较。业务不必不应手写 errors.Is(err, cache.ErrNotFound),统一走下方工具函数。

工具函数(cache/errors.go
函数 只适用于
IsNotFound(err) 框架 API 返回的 err(已归一化)
IsRedisNil(err) 直接 go-redis 返回的原始 err
IsNotFoundAny(err) 来源不确定、或需同时兼容两种 err 时
示例

走框架(推荐):

val, err := app.GetRedis().Get(ctx, "key")
if cache.IsNotFound(err) {
    // key 不存在
}

直接用 go-redis(少数场景):

val, err := client.Get(ctx, "key").Result()
if cache.IsRedisNil(err) {
    // key 不存在
}

业务不要 import "github.com/redis/go-redis/v9" 判断 redis.Nil;走框架用 IsNotFound,直连 driver 用 IsRedisNil

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errNotFound

ErrNotFound is returned by framework APIs after NormalizeErr (not redis.Nil).

Functions

func IsNotFound added in v1.22.7

func IsNotFound(err error) bool

IsNotFound reports normalized not-found errors from RedisContext / cache.C.

func IsNotFoundAny added in v1.22.8

func IsNotFoundAny(err error) bool

IsNotFoundAny reports not-found when err source is unknown or mixed.

func IsRedisNil added in v1.22.8

func IsRedisNil(err error) bool

IsRedisNil reports raw go-redis missing-key errors. Use for direct go-redis calls only.

func NormalizeErr added in v1.22.7

func NormalizeErr(err error) error

NormalizeErr maps redis driver errors to cache-level errors.

func SetRedisSessionResolver added in v1.22.7

func SetRedisSessionResolver(fn func() RedisSession)

SetRedisSessionResolver installs the global session resolver (used by factory).

Types

type C

type C interface {
	Get(key string, value interface{}) (err error)
	GetSkipLocal(key string, value interface{}) (err error)
	GetProxy() *cache.Cache
	Exists(key string) bool
	Set(key string, value interface{}) (err error)
	SetTTL(key string, value interface{}, ttl time.Duration) (err error)
	Del(key string) error
}

func New

func New(ctx context.Context, redisOpts RedisOptions, cacheOpts Options) (c C)

New *

c := New(ctx, RedisOptions{
		Addr: addr,
		Password: "redisUAT",
	}, Options{
		MaxSize: 10,
		Ttl:     time.Second*20,
	})

type Options

type Options struct {
	MaxSize int
	Ttl     time.Duration
}

type RedisContext added in v1.22.7

type RedisContext struct {
	// contains filtered or unexported fields
}

RedisContext is the facade for RedisSession, similar to persistence.OrmContext.

func NewRedis added in v1.22.7

func NewRedis() *RedisContext

func WrapRedisSession added in v1.22.7

func WrapRedisSession(s RedisSession) *RedisContext

func (*RedisContext) Del added in v1.22.7

func (r *RedisContext) Del(ctx context.Context, keys ...string) error

func (*RedisContext) Exists added in v1.22.7

func (r *RedisContext) Exists(ctx context.Context, key string) (bool, error)

func (*RedisContext) Get added in v1.22.7

func (r *RedisContext) Get(ctx context.Context, key string) (string, error)

func (*RedisContext) Publish added in v1.22.7

func (r *RedisContext) Publish(ctx context.Context, channel string, message any) error

func (*RedisContext) Set added in v1.22.7

func (r *RedisContext) Set(ctx context.Context, key string, value any, ttl time.Duration) error

type RedisOptions

type RedisOptions redis.Options

type RedisSession added in v1.22.7

type RedisSession interface {
	Get(ctx context.Context, key string) (string, error)
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	Del(ctx context.Context, keys ...string) error
	Exists(ctx context.Context, key string) (bool, error)
	Publish(ctx context.Context, channel string, message any) error
}

RedisSession is the low-level Redis command surface (KV, pub/sub).

func NewRedisSession added in v1.22.7

func NewRedisSession(c *redis.Client) RedisSession

NewRedisSession wraps a go-redis client; client stays inside cache package.

Jump to

Keyboard shortcuts

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