redis.go 1.91 KB
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
package redis

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-redis/redis/v8"
	"github.com/pkg/errors"
	"time"
)

type (
	WrapClient struct {
		*redis.Client
		ctx context.Context
	}

	Config struct {
		Addr     string
		Password string
		DB       int
	}
)

func NewRedis(conf *Config) (c *WrapClient) {
	client := redis.NewClient(&redis.Options{
		Addr:         conf.Addr,
		Password:     conf.Password,
		DB:           conf.DB,
		MaxConnAge:   time.Minute * 30, // 连接池连接有效时间
		MinIdleConns: 4,
		ReadTimeout:  time.Second,
		WriteTimeout: time.Second,
	})

	ctx := context.Background()
	c = &WrapClient{}
	c.ctx = ctx
	c.Client = client
	ping := c.Ping(ctx)
	if ping.Err() != nil {
		panic(errors.Errorf("连接redis失败:%s", ping.Err()))
	}
	return
}

// GetString 字符串
func (r *WrapClient) GetString(key string) (string, error) {
	return r.Client.Get(r.ctx, key).Result()
}

// SetSimple 通用set
func (r *WrapClient) SetSimple(key string, value interface{}, t ...time.Duration) (string, error) {
	var t2 time.Duration
	if len(t) > 0 {
		t2 = t[0]
	}
	return r.Client.Set(r.ctx, key, value, t2).Result()
}

//GetJson json序列化
func (r *WrapClient) GetJson(key string) (interface{}, error) {
	res := r.Client.Get(r.ctx, key)
	if res.Err() != nil {
		return nil, res.Err()
	}

	b, err := res.Bytes()
	if err != nil {
		return nil, errors.Errorf("get key:%s 反序列化json失败(-1)", key)
	}
	var result interface{}
	err = json.Unmarshal(b, &result)
	if err != nil {
		return nil, errors.Errorf("get key:%s 反序列化json失败(-2)", key)
	}
	return result, nil
}

//SetJson json序列化set
func (r *WrapClient) SetJson(key string, value interface{}, t ...time.Duration) (string, error) {
	var t2 time.Duration
	if len(t) > 0 {
		t2 = t[0]
	}
	v, err := json.Marshal(value)
	if err != nil {
		return "", fmt.Errorf("set key:%s 序列化json失败", key)
	}
	return r.Client.Set(r.ctx, key, v, t2).Result()
}