goredis.go 978 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package redis
  2. import (
  3. "kpt-pasture/config"
  4. "gitee.com/xuyiping_admin/pkg/xerr"
  5. redisv7 "github.com/go-redis/redis/v7"
  6. )
  7. var Nil = redisv7.Nil
  8. func ErrNil(err error) bool {
  9. return xerr.Cause(err).Error() == Nil.Error()
  10. }
  11. func IgnoreErrNil(err error) error {
  12. if err == nil || ErrNil(err) {
  13. return nil
  14. }
  15. return err
  16. }
  17. func NewClientLatest(cfg *config.AppConfig) *redisv7.Client {
  18. return initClientLatest(cfg.RedisSetting.CacheRedis.Addr, cfg.RedisSetting.CacheRedis.DB)
  19. }
  20. func initClientLatest(addr string, db int, opts ...func(*redisv7.Options)) *redisv7.Client {
  21. option := &redisv7.Options{
  22. Addr: addr,
  23. DB: db,
  24. MaxRetries: 3,
  25. OnConnect: func(cn *redisv7.Conn) error {
  26. return cn.ClientSetName("on_connect").Err()
  27. },
  28. }
  29. for _, opt := range opts {
  30. opt(option)
  31. }
  32. return redisv7.NewClient(option)
  33. }
  34. func NewCache(cfg *config.AppConfig) *Cache {
  35. return &Cache{Client: NewClientLatest(cfg)}
  36. }
  37. type Cache struct {
  38. *redisv7.Client
  39. }