redis.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package util
  2. import (
  3. "fmt"
  4. "github.com/go-redis/redis"
  5. device "iot_manager_service/app/device/dao"
  6. system "iot_manager_service/app/system/dao"
  7. "iot_manager_service/config"
  8. "strconv"
  9. "time"
  10. )
  11. var Redis *redis.Client
  12. func InitRedis() error {
  13. cfg := config.Instance()
  14. addr := cfg.Redis.Host
  15. Redis = redis.NewClient(&redis.Options{
  16. Addr: addr,
  17. DialTimeout: 10 * time.Second,
  18. ReadTimeout: 30 * time.Second,
  19. WriteTimeout: 30 * time.Second,
  20. PoolSize: 10,
  21. PoolTimeout: 30 * time.Second,
  22. Password: cfg.Redis.Password,
  23. })
  24. _, err := Redis.Ping().Result()
  25. if err != nil {
  26. return err
  27. }
  28. return nil
  29. }
  30. const (
  31. UserKey = "%s:sys:user:%d"
  32. ModuleTypeKey = "%s:dict:module_type:%d"
  33. ControlTypeKey = "%s:dict:control_type:%d"
  34. TenantNameKey = "%s:sys:tenant_name:%s"
  35. RoleNameKey = "%s:sys:role_name:%d"
  36. SexNameKey = "%s:sys:sex_name:%d"
  37. ControlType = "control_type"
  38. ModuleType = "module_his_type"
  39. Sex = "sex"
  40. )
  41. func GetUserName(tenantId string, id int64) string {
  42. var name string
  43. key := fmt.Sprintf(UserKey, tenantId, id)
  44. name, err := Redis.Get(key).Result()
  45. if err != nil {
  46. user := &system.User{ID: id}
  47. if err := user.GetUser(); err != nil {
  48. fmt.Printf("GetUser err = %s \n", err)
  49. } else {
  50. name = user.Name
  51. Redis.Set(key, name, time.Second*10)
  52. }
  53. }
  54. return name
  55. }
  56. func GetModuleName(tenantId string, id int) string {
  57. var name string
  58. key := fmt.Sprintf(ModuleTypeKey, tenantId, id)
  59. name, err := Redis.Get(key).Result()
  60. if err != nil {
  61. dict := &device.Dict{Code: ModuleType, DictKey: strconv.Itoa(id)}
  62. if err := dict.GetByCodeAndKey(); err != nil {
  63. fmt.Printf("GetByCodeAndKey err = %d \n", err)
  64. } else {
  65. name = dict.DictValue
  66. Redis.Set(key, name, -1)
  67. }
  68. }
  69. return name
  70. }
  71. func GetControlType(tenantId string, id int) string {
  72. var name string
  73. key := fmt.Sprintf(ControlTypeKey, tenantId, id)
  74. name, err := Redis.Get(key).Result()
  75. if err != nil {
  76. dict := &device.Dict{Code: ControlType, DictKey: strconv.Itoa(id)}
  77. if err := dict.GetByCodeAndKey(); err != nil {
  78. fmt.Printf("GetByCodeAndKey err = %d \n", err)
  79. } else {
  80. name = dict.DictValue
  81. Redis.Set(key, name, -1)
  82. }
  83. }
  84. return name
  85. }
  86. func GetSexName(tenantId string, id int) string {
  87. var name string
  88. key := fmt.Sprintf(SexNameKey, tenantId, id)
  89. name, err := Redis.Get(key).Result()
  90. if err != nil {
  91. dict := &device.Dict{Code: Sex, DictKey: strconv.Itoa(id)}
  92. if err := dict.GetByCodeAndKey(); err != nil {
  93. fmt.Printf("GetByCodeAndKey err = %d \n", err)
  94. } else {
  95. name = dict.DictValue
  96. Redis.Set(key, name, -1)
  97. }
  98. }
  99. return name
  100. }
  101. func GetTenantName(tenantId string, id string) string {
  102. var name string
  103. key := fmt.Sprintf(TenantNameKey, tenantId, id)
  104. name, err := Redis.Get(key).Result()
  105. if err != nil {
  106. tenant := &system.Tenant{TenantId: id}
  107. if err := tenant.GetTenant(); err != nil {
  108. fmt.Printf("GetTenant err = %s \n", err)
  109. } else {
  110. name = tenant.TenantName
  111. Redis.Set(key, name, -1)
  112. }
  113. }
  114. return name
  115. }
  116. func GetRoleName(tenantId string, roleId int64) string {
  117. var name string
  118. key := fmt.Sprintf(RoleNameKey, tenantId, roleId)
  119. name, err := Redis.Get(key).Result()
  120. if err != nil {
  121. role := &system.Role{ID: roleId}
  122. if err := role.GetRole(); err != nil {
  123. fmt.Printf("GetRole err = %s \n", err)
  124. } else {
  125. name = role.RoleName
  126. Redis.Set(key, name, -1)
  127. }
  128. }
  129. return name
  130. }