redisutil

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2023 License: MIT Imports: 15 Imported by: 0

README

功能特性

基于redigo 的redis操作封装 和缓存cache wrapper

  1. set, get, mget, batchset
  2. 缓存操作封装 CacheWrapper
  3. 缓存批量操作封装 CacheWrapperMget
  4. 缓存支持singleflight , 支持flush

gotest 启动方法

redis_util_test.go 和redis_util_cache_test.go 包含了go test 运行demo

  1. 配置 internal/test/config.yaml 中的redis配置信息
  2. go test -v

使用方法

基础使用
redisUtil := redisutil.NewRedisUtil(redisPool)
cacheKey := "cclehui_test_set_get_key_211022"

_ = redisUtil.Set(ctx, cacheKey, "axxxaa", 3600) // 设置缓存

valueStrRes := ""
_, _ = redisUtil.Get(ctx, cacheKey, &valueStrRes) // 获取缓存
fmt.Println("获取缓存:", valueStrRes)

缓存wrapper

redis_util_cache_test.go 中有更多的使用例子

redisUtil := NewRedisUtil(getTestPool())
key := "gotest:redis_wrapper:get"

type valueStruct struct {
	Name string
	Age  int
}

fallbackFunc := func() (interface{}, error) {
	fmt.Printf( "fallback 缓存穿透 call real function\n")
	time.Sleep(time.Second * 1)

	data := &valueStruct{
		Name: "xxxxxx",
		Age:  18,
	}

	return data, nil
}

resultData := &valueStruct{}

paramsTemp := &WrapperParams{
	Key:           Key,
	ExpireSeconds: 600,
	FallbackFunc:  fallbackFunc,
	Result:        resultData,
}

paramsTemp.Result = &dataResult[j]

redisUtil.CacheWrapper(ctx, paramsTemp)

Documentation

Index

Constants

View Source
const (
	DefaultSingleFlightGroupNum = 10

	TTLNoExpire = -1 // 不过期
)
View Source
const (
	LevelDebug = "DEBUG"
	LevelInfo  = "INFO"
	LevelWarn  = "WARN"
	LevelError = "ERROR"
)

Variables

View Source
var ErrInvalidNum = errors.New("Invalid num")

Functions

func Decode

func Decode(b []byte, ptr interface{}) error

Decode将Encode的结果重新赋值给ptr指向的类型

func Encode

func Encode(val interface{}) ([]byte, error)

Encode将任意类型编码为[]byte类型

func SetDefaultLogger

func SetDefaultLogger(newLogger Logger)

Types

type BatchSetParams

type BatchSetParams struct {
	Keys               []string
	Values             []interface{}
	ExpireSecondsSlice []int
}

type DefaultLogger

type DefaultLogger struct{}

func (*DefaultLogger) Errorf

func (l *DefaultLogger) Errorf(ctx context.Context, format string, args ...interface{})

func (*DefaultLogger) Infof

func (l *DefaultLogger) Infof(ctx context.Context, format string, args ...interface{})

type FallbackFunc

type FallbackFunc func() (interface{}, error)

type LogData

type LogData struct {
	Level   string
	Content string
}

type Logger

type Logger interface {
	Errorf(ctx context.Context, format string, args ...interface{})
	Infof(ctx context.Context, format string, args ...interface{})
}

func GetDefaultLogger

func GetDefaultLogger() Logger

type MgetBatchFallbackFunc

type MgetBatchFallbackFunc func(fallbackIndexes []int) (map[int]interface{}, error)

type MgetFallbackFunc

type MgetFallbackFunc func(fallbackIndex int) (interface{}, error)

type Option

type Option interface {
	Apply(*RedisUtil)
}

func OptionLogger

func OptionLogger(logger Logger) Option

func OptionSingleFlightGroupNum

func OptionSingleFlightGroupNum(num int) Option

type OptionFunc

type OptionFunc func(cacheUtil *RedisUtil)

func (OptionFunc) Apply

func (of OptionFunc) Apply(cacheUtil *RedisUtil)

type RedisUtil

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

func NewRedisUtil

func NewRedisUtil(pool *redis.Pool, options ...Option) *RedisUtil

func (*RedisUtil) BatchSet

func (ru *RedisUtil) BatchSet(ctx context.Context, params *BatchSetParams) (err error)

func (*RedisUtil) CacheWrapper

func (ru *RedisUtil) CacheWrapper(ctx context.Context,
	params *WrapperParams) (err error)

func (*RedisUtil) CacheWrapperMget

func (ru *RedisUtil) CacheWrapperMget(ctx context.Context,
	params *WrapperParamsMget) (err error)

多key 缓存获取wrapper mget

func (*RedisUtil) Decr

func (ru *RedisUtil) Decr(ctx context.Context, key string) (res int64, err error)

func (*RedisUtil) DecrBy

func (ru *RedisUtil) DecrBy(ctx context.Context, key string, diff int64) (res int64, err error)

func (*RedisUtil) Del

func (ru *RedisUtil) Del(ctx context.Context, key string) (err error)

func (*RedisUtil) Expire

func (ru *RedisUtil) Expire(ctx context.Context, key string, ttl int) (err error)

func (*RedisUtil) Get

func (ru *RedisUtil) Get(ctx context.Context, key string, value interface{}) (hit bool, err error)

func (*RedisUtil) Incr

func (ru *RedisUtil) Incr(ctx context.Context, key string) (res int64, err error)

func (*RedisUtil) IncrBy

func (ru *RedisUtil) IncrBy(ctx context.Context, key string, diff int64) (res int64, err error)

func (*RedisUtil) MGet

func (ru *RedisUtil) MGet(ctx context.Context,
	keys []string, valuesInter interface{}) (hits []bool, err error)

func (*RedisUtil) Set

func (ru *RedisUtil) Set(ctx context.Context, key string, value interface{}, ttl int) (err error)

func (*RedisUtil) TTL

func (ru *RedisUtil) TTL(ctx context.Context, key string) (ttl int, err error)

func (*RedisUtil) WrapDo

func (ru *RedisUtil) WrapDo(ctx context.Context, doFunction func(con redis.Conn) error) error

func (*RedisUtil) ZAdd

func (ru *RedisUtil) ZAdd(ctx context.Context, key string, infos []*SortSetInfo) (err error)

func (*RedisUtil) ZCard

func (ru *RedisUtil) ZCard(ctx context.Context, key string) (res int64, err error)

func (*RedisUtil) ZRange

func (ru *RedisUtil) ZRange(ctx context.Context, key string, start, end int) (result []string, err error)

func (*RedisUtil) ZRem

func (ru *RedisUtil) ZRem(ctx context.Context, key string, names []string) (err error)

func (*RedisUtil) ZRevRange

func (ru *RedisUtil) ZRevRange(ctx context.Context, key string, start, end int) (result []string, err error)

type SortSetInfo

type SortSetInfo struct {
	Score int64
	Name  string
}

type WrapperParams

type WrapperParams struct {
	Key           string
	ExpireSeconds int
	Result        interface{} // 结果
	FallbackFunc  FallbackFunc

	SingleFlight bool // 是否启动 singleflight
	FlushCache   bool // 是否用SetFunc刷新缓存
}

type WrapperParamsMget

type WrapperParamsMget struct {
	Keys          []string
	ExpireSeconds []int
	ResultSlice   interface{} // 结果 slice of your result

	// 批量或并发二选一
	FallbackFuncSlice []MgetFallbackFunc    // 并发获取
	BatchFallbackFunc MgetBatchFallbackFunc // 批量获取

	SingleFlight bool // 是否启动 singleflight
	FlushCache   bool // 用SetFunc刷新缓存
}

Directories

Path Synopsis
internal
singleflight
Package singleflight provides a duplicate function call suppression mechanism.
Package singleflight provides a duplicate function call suppression mechanism.

Jump to

Keyboard shortcuts

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