redis.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package cache
  2. import (
  3. "context"
  4. "eta_mini_ht_api/common/component/config"
  5. logger "eta_mini_ht_api/common/component/log"
  6. "github.com/go-redis/redis/v8"
  7. "log"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. var (
  13. redisCache *RedisCache
  14. once sync.Once
  15. )
  16. func GetInstance() *RedisCache {
  17. once.Do(func() {
  18. redisConf, ok := config.GetConfig("redis").(*config.RedisConfig)
  19. // 检查是否成功获取到RedisConfig实例
  20. if !ok {
  21. logger.Info("加载redis配置失败")
  22. return
  23. }
  24. opts, ok := redisConf.GetConfig().(config.RedisOpts)
  25. // 检查Host是否已设置,然后初始化或执行相应操作
  26. if ok && opts.Host != "" {
  27. logger.Info("初始化redis")
  28. // 这里可以添加初始化Redis的逻辑
  29. redisCache = newRedis(opts)
  30. } else {
  31. logger.Info("Redis configuration is incomplete: Host is missing")
  32. return
  33. }
  34. })
  35. return redisCache
  36. }
  37. func newRedis(opts config.RedisOpts) *RedisCache {
  38. if &opts == nil || opts.Host == "" {
  39. log.Fatalf("redis 连接失败")
  40. }
  41. redisTemplate := redis.NewUniversalClient(&redis.UniversalOptions{
  42. Addrs: strings.Split(opts.Host, ";"),
  43. DB: opts.Database,
  44. Password: opts.Password,
  45. IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
  46. MinIdleConns: opts.MaxIdle,
  47. })
  48. return &RedisCache{redisTemplate: redisTemplate}
  49. }
  50. // RedisCache
  51. // @Description: redis缓存
  52. type RedisCache struct {
  53. redisTemplate redis.UniversalClient
  54. }
  55. // GetString 获取一个值
  56. func (r *RedisCache) GetString(key string) string {
  57. return r.GetStringWithContext(context.Background(), key)
  58. }
  59. // GetStringWithContext 获取一个值
  60. func (r *RedisCache) GetStringWithContext(ctx context.Context, key string) (value string) {
  61. value, _ = r.redisTemplate.Get(ctx, key).Result()
  62. return
  63. }
  64. // SetString 设置一个值
  65. func (r *RedisCache) SetString(key string, val string, timeout time.Duration) error {
  66. return r.redisTemplate.SetEX(context.Background(), key, val, timeout).Err()
  67. }
  68. // SetStringWithContext 设置一个值
  69. func (r *RedisCache) SetStringWithContext(ctx context.Context, key string, val string, timeout time.Duration) error {
  70. return r.redisTemplate.SetEX(ctx, key, val, timeout).Err()
  71. }
  72. // IsExist 判断key是否存在
  73. func (r *RedisCache) IsExist(key string) bool {
  74. return r.IsExistWithContext(context.Background(), key)
  75. }
  76. // IsExistWithContext 判断key是否存在
  77. func (r *RedisCache) IsExistWithContext(ctx context.Context, key string) bool {
  78. result, _ := r.redisTemplate.Exists(ctx, key).Result()
  79. return result > 0
  80. }
  81. // Delete 删除
  82. func (r *RedisCache) Delete(key string) error {
  83. return r.DeleteWithContext(context.Background(), key)
  84. }
  85. // DeleteWithContext 删除
  86. func (r *RedisCache) DeleteWithContext(ctx context.Context, key string) error {
  87. return r.redisTemplate.Del(ctx, key).Err()
  88. }
  89. // Do 执行指令
  90. func (r *RedisCache) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  91. return r.DoWithContext(context.Background(), commandName, args...)
  92. }
  93. // DoWithContext 执行指令
  94. func (r *RedisCache) DoWithContext(ctx context.Context, commandName string, args ...interface{}) (reply interface{}, err error) {
  95. newArgs := []interface{}{commandName}
  96. newArgs = append(newArgs, args...)
  97. return r.redisTemplate.Do(ctx, newArgs...).Result()
  98. }