rs

package
v0.0.0-...-656b6e9 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const RedisNil = redis.Nil

Variables

This section is empty.

Functions

This section is empty.

Types

type Getter

type Getter[T any] func() (*T, error)

type ICache

type ICache interface {
	Get(ctx context.Context, key string) *redis.StringCmd
	Set(ctx context.Context, key string, value any, ex time.Duration) *redis.StatusCmd

	MGet(ctx context.Context, keys ...string) *redis.SliceCmd
	MSet(ctx context.Context, values ...interface{}) *redis.StatusCmd

	Pipeline() redis.Pipeliner
}

type JSON

type JSON[T any] struct {
	Conn   ICache
	Key    string
	Expire time.Duration // 默认缓存时间 1 小时
	Getter Getter[T]
}

JSON 结构包含与 Redis 缓存的连接、操作的 key、 数据失效时间和构建缓存数据的 Builder 函数。

func (*JSON[T]) Get

func (m *JSON[T]) Get(ctx context.Context) (data *T, ok bool, err error)

func (*JSON[T]) Sugar

func (m *JSON[T]) Sugar(ctx context.Context) (*T, error)

Sugar 函数首先尝试从缓存中获得数据;如果数据不存在, 它会调用提供的 Builder 函数来构建数据,并将其存储在缓存中。

type MGetJson

type MGetJson[T, K any] struct {
	Conn    ICache
	KeysMap map[string]K  // 缓存key map, value是每个key回源需要的参数
	Expire  time.Duration // 过期时间
	Getter  MGetter[T, K] // 回源方法,如果回源没有找到数据,缓存默认存空字符串
	// contains filtered or unexported fields
}

func (MGetJson[T, K]) Sugar

func (m MGetJson[T, K]) Sugar(ctx context.Context) (map[string]*T, error)

Sugar 批量获取缓存消息

type MGetter

type MGetter[T, K any] func(context.Context, []K) (map[string]*T, error)

type PipelineGetGetter

type PipelineGetGetter[T, K any] func(context.Context, []K) (map[string]*T, error)

type PipelineGetJson

type PipelineGetJson[T, K any] struct {
	Conn    ICache
	KeysMap map[string]K  // 缓存key map, value是每个key回源需要的参数
	Expire  time.Duration // 过期时间
	Getter  MGetter[T, K] // 回源方法,如果回源没有找到数据,缓存默认存空字符串
}

PipelineGetJson pipeline get 命令, 注意控制key的数量

func (PipelineGetJson[T, K]) Sugar

func (p PipelineGetJson[T, K]) Sugar(ctx context.Context) (map[string]*T, error)

Sugar 批量获取缓存消息

Jump to

Keyboard shortcuts

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