getDataFromCacheOrDb.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package redisclient
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "time"
  8. )
  9. // GetFromCacheOrDB 通用缓存获取方法
  10. // cacheKey: 缓存键
  11. // cacheTime: 缓存时间
  12. // target: 目标结构体指针(需要传入指针)
  13. // dbFunc: 数据库查询函数,当缓存不存在时调用
  14. func GetBytesFromCacheOrDB(cacheKey string, cacheTime time.Duration, target interface{}, dbFunc func() (interface{}, error)) error {
  15. // 1. 尝试从缓存获取
  16. cacheDataStr, err := DefaultClient().Get(context.Background(), cacheKey).Result()
  17. if err == nil {
  18. if err := json.Unmarshal([]byte(cacheDataStr), target); err == nil {
  19. return nil
  20. }
  21. }
  22. // 2. 缓存未命中,从数据库获取
  23. data, err := dbFunc()
  24. if err != nil {
  25. return err
  26. }
  27. // 3. 序列化数据并存入缓存(异步)
  28. go func() {
  29. cacheData, err := json.Marshal(data)
  30. if err != nil {
  31. return
  32. }
  33. if err := DefaultClient().Set(context.Background(), cacheKey, cacheData, cacheTime).Err(); err != nil {
  34. fmt.Println(fmt.Sprintf("failed to set cache for key %s: %v", cacheKey, err))
  35. }
  36. }()
  37. // 4. 将数据赋值给target
  38. val := reflect.ValueOf(target)
  39. if val.Kind() != reflect.Ptr || val.IsNil() {
  40. return fmt.Errorf("target must be a non-nil pointer")
  41. }
  42. dataVal := reflect.ValueOf(data)
  43. if dataVal.Type().AssignableTo(val.Elem().Type()) {
  44. val.Elem().Set(dataVal)
  45. return nil
  46. }
  47. // 如果类型不匹配,尝试通过JSON转换
  48. dataBytes, err := json.Marshal(data)
  49. if err != nil {
  50. return fmt.Errorf("failed to marshal data: %v", err)
  51. }
  52. return json.Unmarshal(dataBytes, target)
  53. }
  54. // 适用于保存json数据
  55. func GetJsonDataFromCacheOrDB(cacheKey string, cacheTime time.Duration, target interface{}, dbFunc func() (interface{}, error)) error {
  56. // 1. 尝试从缓存获取
  57. cacheDataStr, err := DefaultClient().Get(context.Background(), cacheKey).Result()
  58. if err == nil {
  59. if err := json.Unmarshal([]byte(cacheDataStr), target); err == nil {
  60. return nil
  61. }
  62. }
  63. // 2. 缓存未命中,从数据库获取
  64. data, err := dbFunc()
  65. if err != nil {
  66. return err
  67. }
  68. // 3. 序列化数据并存入缓存(异步)
  69. go func() {
  70. cacheData, err := json.Marshal(data)
  71. if err != nil {
  72. return
  73. }
  74. if err := DefaultClient().Set(context.Background(), cacheKey, cacheData, cacheTime).Err(); err != nil {
  75. fmt.Println(fmt.Sprintf("failed to set cache for key %s: %v", cacheKey, err))
  76. }
  77. }()
  78. // 4. 将数据赋值给target
  79. val := reflect.ValueOf(target)
  80. if val.Kind() != reflect.Ptr || val.IsNil() {
  81. return fmt.Errorf("target must be a non-nil pointer")
  82. }
  83. dataVal := reflect.ValueOf(data)
  84. if dataVal.Type().AssignableTo(val.Elem().Type()) {
  85. val.Elem().Set(dataVal)
  86. return nil
  87. }
  88. // 如果类型不匹配,尝试通过JSON转换
  89. dataBytes, err := json.Marshal(data)
  90. if err != nil {
  91. return fmt.Errorf("failed to marshal data: %v", err)
  92. }
  93. return json.Unmarshal(dataBytes, target)
  94. }