golang 的 rabbitmq 消费项目
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

db_city.go 2.1 KiB

1 anno fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/md"
  5. "applet/app/utils/cache"
  6. "applet/app/utils/logx"
  7. )
  8. func CityGetAll() (*[]model.City, error) {
  9. var CityList []model.City
  10. if err := Db.Cols("id,name").Find(&CityList); err != nil {
  11. return nil, logx.Error(err)
  12. }
  13. return &CityList, nil
  14. }
  15. // 获取一条记录
  16. func CityGetOne(key string) (*model.City, error) {
  17. var City model.City
  18. if has, err := Db.Where("`id`=?", key).Get(&City); err != nil || has == false {
  19. if has == false {
  20. return &City, nil
  21. }
  22. return nil, logx.Error(err)
  23. }
  24. return &City, nil
  25. }
  26. func CityGetOneByName(name string) (*model.City, error) {
  27. var City model.City
  28. if has, err := Db.Where("name LIKE ?", "%"+name+"%").Get(&City); err != nil || has == false {
  29. if has == false {
  30. return &City, nil
  31. }
  32. return nil, logx.Error(err)
  33. }
  34. return &City, nil
  35. }
  36. //单条记录获取DB
  37. func CityGetWithDb(HKey string) string {
  38. cacheKey := md.OfficialCityCacheKey
  39. get, err := cache.HGetString(cacheKey, HKey)
  40. if err != nil || get == "" {
  41. City, err := CityGetOne(HKey)
  42. if err != nil || City == nil {
  43. _ = logx.Error(err)
  44. return ""
  45. }
  46. // key是否存在
  47. cacheKeyExist := false
  48. if cache.Exists(cacheKey) {
  49. cacheKeyExist = true
  50. }
  51. // 设置缓存
  52. _, err = cache.HSet(cacheKey, HKey, City.Name)
  53. if err != nil {
  54. _ = logx.Error(err)
  55. return ""
  56. }
  57. if !cacheKeyExist { // 如果是首次设置 设置过期时间
  58. _, err := cache.Expire(cacheKey, md.CfgCacheTime)
  59. if err != nil {
  60. _ = logx.Error(err)
  61. return ""
  62. }
  63. }
  64. return City.Name
  65. }
  66. return get
  67. }
  68. func CityFindWithDb(keys ...string) map[string]string {
  69. res := map[string]string{}
  70. cacheKey := md.OfficialCityCacheKey
  71. err := cache.GetJson(cacheKey, &res)
  72. if err != nil || len(res) == 0 {
  73. CityList, _ := CityGetAll()
  74. if CityList == nil {
  75. return nil
  76. }
  77. for _, v := range *CityList {
  78. res[v.Id] = v.Name
  79. }
  80. cache.SetJson(cacheKey, res, md.CfgCacheTime)
  81. }
  82. if len(keys) == 0 {
  83. return res
  84. }
  85. tmp := map[string]string{}
  86. for _, v := range keys {
  87. if val, ok := res[v]; ok {
  88. tmp[v] = val
  89. } else {
  90. tmp[v] = ""
  91. }
  92. }
  93. return tmp
  94. }