package redis import ( "kpt-pasture/config" "gitee.com/xuyiping_admin/pkg/xerr" redisv7 "github.com/go-redis/redis/v7" ) var Nil = redisv7.Nil func ErrNil(err error) bool { return xerr.Cause(err).Error() == Nil.Error() } func IgnoreErrNil(err error) error { if err == nil || ErrNil(err) { return nil } return err } func NewClientLatest(cfg *config.AppConfig) *redisv7.Client { return initClientLatest(cfg.RedisSetting.CacheRedis.Addr, cfg.RedisSetting.CacheRedis.DB) } func initClientLatest(addr string, db int, opts ...func(*redisv7.Options)) *redisv7.Client { option := &redisv7.Options{ Addr: addr, DB: db, MaxRetries: 3, OnConnect: func(cn *redisv7.Conn) error { return cn.ClientSetName("on_connect").Err() }, } for _, opt := range opts { opt(option) } return redisv7.NewClient(option) } func NewCache(cfg *config.AppConfig) *Cache { return &Cache{Client: NewClientLatest(cfg)} } type Cache struct { *redisv7.Client }