cache

package
v1.22.7 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)
})

错误处理

业务不要 import "github.com/redis/go-redis/v9" 判断 redis.Nil

if cache.IsNotFound(err) {
    // missing key
}

cache.CGet/Del 同样会归一化 redis.Nil

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotFound added in v1.22.7

func IsNotFound(err error) bool

IsNotFound reports whether err is a missing-key error from cache/redis session.

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