redis.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package cache
  2. import (
  3. "context"
  4. logger "eta_mini_ht_api/component/log"
  5. redisConfig "eta_mini_ht_api/config"
  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. config := redisConfig.GetConfig("redis")
  19. // 检查是否成功获取到RedisConfig实例
  20. redisInstance, ok := config.(*redisConfig.RedisConfig)
  21. if !ok {
  22. // 处理错误情况,比如记录日志或返回错误信息
  23. logger.Info("加载redis配置失败")
  24. return // 或者采取其他适当的错误处理措施
  25. }
  26. // 获取Redis配置选项,这里假设GetConfig方法返回(*RedisOpts, error),需要检查错误
  27. opts := redisInstance.GetConfig().(redisConfig.RedisOpts)
  28. // 检查Host是否已设置,然后初始化或执行相应操作
  29. if opts.Host != "" {
  30. logger.Info("初始化redis")
  31. // 这里可以添加初始化Redis的逻辑
  32. redisCache = newRedis(opts)
  33. } else {
  34. logger.Info("Redis configuration is incomplete: Host is missing")
  35. }
  36. })
  37. return redisCache
  38. }
  39. func newRedis(opts redisConfig.RedisOpts) *RedisCache {
  40. if &opts == nil || opts.Host == "" {
  41. log.Fatalf("redis 连接失败")
  42. }
  43. redisTemplate := redis.NewUniversalClient(&redis.UniversalOptions{
  44. Addrs: strings.Split(opts.Host, ";"),
  45. DB: opts.Database,
  46. Password: opts.Password,
  47. IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
  48. MinIdleConns: opts.MaxIdle,
  49. })
  50. return &RedisCache{redisTemplate: redisTemplate}
  51. }
  52. // RedisCache
  53. // @Description: redis缓存
  54. type RedisCache struct {
  55. redisTemplate redis.UniversalClient
  56. }
  57. // GetString 获取一个值
  58. func (r *RedisCache) GetString(key string) string {
  59. return r.GetStringWithContext(context.Background(), key)
  60. }
  61. // GetStringWithContext 获取一个值
  62. func (r *RedisCache) GetStringWithContext(ctx context.Context, key string) (value string) {
  63. value, _ = r.redisTemplate.Get(ctx, key).Result()
  64. return
  65. }
  66. // SetString 设置一个值
  67. func (r *RedisCache) SetString(key string, val string, timeout time.Duration) error {
  68. return r.redisTemplate.SetEX(context.Background(), key, val, timeout).Err()
  69. }
  70. // SetStringWithContext 设置一个值
  71. func (r *RedisCache) SetStringWithContext(ctx context.Context, key string, val string, timeout time.Duration) error {
  72. return r.redisTemplate.SetEX(ctx, key, val, timeout).Err()
  73. }
  74. // IsExist 判断key是否存在
  75. func (r *RedisCache) IsExist(key string) bool {
  76. return r.IsExistWithContext(context.Background(), key)
  77. }
  78. // IsExistWithContext 判断key是否存在
  79. func (r *RedisCache) IsExistWithContext(ctx context.Context, key string) bool {
  80. result, _ := r.redisTemplate.Exists(ctx, key).Result()
  81. return result > 0
  82. }
  83. // Delete 删除
  84. func (r *RedisCache) Delete(key string) error {
  85. return r.DeleteWithContext(context.Background(), key)
  86. }
  87. // DeleteWithContext 删除
  88. func (r *RedisCache) DeleteWithContext(ctx context.Context, key string) error {
  89. return r.redisTemplate.Del(ctx, key).Err()
  90. }
  91. // Do 执行指令
  92. func (r *RedisCache) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  93. return r.DoWithContext(context.Background(), commandName, args...)
  94. }
  95. // DoWithContext 执行指令
  96. func (r *RedisCache) DoWithContext(ctx context.Context, commandName string, args ...interface{}) (reply interface{}, err error) {
  97. newArgs := []interface{}{commandName}
  98. newArgs = append(newArgs, args...)
  99. return r.redisTemplate.Do(ctx, newArgs...).Result()
  100. }