redis.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package cache
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/go-redis/redis/v8"
  7. "log"
  8. "strings"
  9. "sync"
  10. "time"
  11. "xps/viewmodels"
  12. )
  13. var ErrRedisInit = errors.New("缓存初始化失败")
  14. var (
  15. once sync.Once
  16. cacheClient redis.UniversalClient
  17. )
  18. var CONFIG = Redis{
  19. DB: 0,
  20. Addr: "127.0.0.1:6379",
  21. Password: "",
  22. PoolSize: 0,
  23. }
  24. type Redis struct {
  25. DB int `mapstructure:"db" json:"db" yaml:"db"`
  26. Addr string `mapstructure:"addr" json:"addr" yaml:"addr"`
  27. Password string `mapstructure:"password" json:"password" yaml:"password"`
  28. PoolSize int `mapstructure:"pool-size" json:"pool-size" yaml:"pool-size"`
  29. }
  30. func init() {
  31. if Instance() == nil {
  32. log.Fatal(ErrRedisInit)
  33. }
  34. }
  35. func Instance() redis.UniversalClient {
  36. once.Do(func() {
  37. universalOptions := &redis.UniversalOptions{
  38. Addrs: strings.Split(CONFIG.Addr, ","),
  39. Password: CONFIG.Password,
  40. PoolSize: int(CONFIG.PoolSize),
  41. IdleTimeout: 300 * time.Second,
  42. }
  43. cacheClient = redis.NewUniversalClient(universalOptions)
  44. })
  45. return cacheClient
  46. }
  47. func SetCache(key string, value interface{}, expiration time.Duration) error {
  48. err := Instance().Set(context.Background(), key, value, expiration).Err()
  49. if err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. func GetCache(key string) (interface{}, error) {
  55. return Instance().Get(context.Background(), key).Result()
  56. }
  57. func DeleteCache(key string) (int64, error) {
  58. return Instance().Del(context.Background(), key).Result()
  59. }
  60. func GetCacheString(key string) (string, error) {
  61. value, err := GetCacheBytes(key)
  62. if err != nil {
  63. return "", err
  64. }
  65. return string(value), nil
  66. }
  67. func GetCacheBytes(key string) ([]byte, error) {
  68. return Instance().Get(context.Background(), key).Bytes()
  69. }
  70. func GetCacheUint(key string) (uint64, error) {
  71. return Instance().Get(context.Background(), key).Uint64()
  72. }
  73. func SetUserData(userId int64, userData *viewmodels.UserData) error {
  74. keyStr := fmt.Sprintf("user_%d", userId)
  75. return SetCache(keyStr, userData, 0)
  76. }
  77. func GetUserData(userId int64) (*viewmodels.UserData, error) {
  78. keyStr := fmt.Sprintf("user_%d", userId)
  79. userData := &viewmodels.UserData{}
  80. err := Instance().Get(context.Background(), keyStr).Scan(userData)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return userData, nil
  85. }
  86. func DeleteUserData(userId int64) (int64, error) {
  87. keyStr := fmt.Sprintf("user_%d", userId)
  88. return Instance().Del(context.Background(), keyStr).Result()
  89. }