item.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/library/cache/redis"
  6. "go-common/app/service/openplatform/ticket-item/model"
  7. "go-common/library/log"
  8. )
  9. // RawItems 取项目信息
  10. func (d *Dao) RawItems(c context.Context, ids []int64) (info map[int64]*model.Item, err error) {
  11. rows, err := d.db.Model(&model.Item{}).Where("id in (?)", ids).Rows()
  12. info = make(map[int64]*model.Item)
  13. if err != nil {
  14. log.Error("QueryItem(%v) error(%v)", ids, err)
  15. return
  16. }
  17. defer rows.Close()
  18. for rows.Next() {
  19. var item model.Item
  20. err = d.db.ScanRows(rows, &item)
  21. json.Unmarshal([]byte(item.PerformanceImage), &item.Img)
  22. info[item.ID] = &item
  23. }
  24. return
  25. }
  26. // CacheItems 缓存取项目信息
  27. func (d *Dao) CacheItems(c context.Context, ids []int64) (info map[int64]*model.Item, err error) {
  28. var data [][]byte
  29. keys := make([]interface{}, len(ids))
  30. conn := d.redis.Get(c)
  31. defer conn.Close()
  32. keyPidMap := make(map[string]int64, len(ids))
  33. for k, id := range ids {
  34. key := keyItem(id)
  35. if _, ok := keyPidMap[key]; !ok {
  36. // duplicate id
  37. keyPidMap[key] = id
  38. keys[k] = key
  39. }
  40. }
  41. log.Info("MGET %v", model.JSONEncode(keys))
  42. if data, err = redis.ByteSlices(conn.Do("MGET", keys...)); err != nil {
  43. log.Error("MGET ERR: %v", err)
  44. return
  45. }
  46. info = make(map[int64]*model.Item)
  47. for _, d := range data {
  48. if d != nil {
  49. item := &model.Item{}
  50. json.Unmarshal(d, item)
  51. info[item.ID] = item
  52. }
  53. }
  54. return
  55. }
  56. // AddCacheItems 缓存取项目信息
  57. func (d *Dao) AddCacheItems(c context.Context, info map[int64]*model.Item) (err error) {
  58. conn := d.redis.Get(c)
  59. defer func() {
  60. conn.Flush()
  61. conn.Close()
  62. }()
  63. var data []interface{}
  64. var keys []string
  65. for k, v := range info {
  66. b, _ := json.Marshal(v)
  67. key := keyItem(k)
  68. keys = append(keys, key)
  69. data = append(data, key, b)
  70. }
  71. log.Info("MSET %v", keys)
  72. if err = conn.Send("MSET", data...); err != nil {
  73. return
  74. }
  75. for i := 0; i < len(data); i += 2 {
  76. conn.Send("EXPIRE", data[i], CacheTimeout)
  77. }
  78. return
  79. }
  80. // RawItemDetails 批量获取项目详情
  81. func (d *Dao) RawItemDetails(c context.Context, ids []int64) (detail map[int64]*model.ItemDetail, err error) {
  82. rows, err := d.db.Model(&model.ItemDetail{}).Where("project_id in (?)", ids).Rows()
  83. detail = make(map[int64]*model.ItemDetail)
  84. if err != nil {
  85. log.Error("RawItemDetail(%v) error(%v)", ids, err)
  86. return
  87. }
  88. defer rows.Close()
  89. for rows.Next() {
  90. var item model.ItemDetail
  91. err = d.db.ScanRows(rows, &item)
  92. detail[item.ProjectID] = &item
  93. }
  94. return
  95. }
  96. // CacheItemDetails 缓存批量获取项目详情
  97. func (d *Dao) CacheItemDetails(c context.Context, ids []int64) (detail map[int64]*model.ItemDetail, err error) {
  98. var data [][]byte
  99. var keys []interface{}
  100. keyPidMap := make(map[string]int64, len(ids))
  101. for _, id := range ids {
  102. key := keyItemDetail(id)
  103. if _, ok := keyPidMap[key]; !ok {
  104. // duplicate pid
  105. keyPidMap[key] = id
  106. keys = append(keys, key)
  107. }
  108. }
  109. conn := d.redis.Get(c)
  110. defer conn.Close()
  111. log.Info("MGET %v", model.JSONEncode(keys))
  112. if data, err = redis.ByteSlices((conn.Do("MGET", keys...))); err != nil {
  113. return
  114. }
  115. detail = make(map[int64]*model.ItemDetail)
  116. for _, d := range data {
  117. if d != nil {
  118. var v *model.ItemDetail
  119. json.Unmarshal(d, &v)
  120. detail[v.ProjectID] = v
  121. }
  122. }
  123. return
  124. }
  125. // AddCacheItemDetails 缓存取项目详情
  126. func (d *Dao) AddCacheItemDetails(c context.Context, detail map[int64]*model.ItemDetail) (err error) {
  127. conn := d.redis.Get(c)
  128. defer func() {
  129. conn.Flush()
  130. conn.Close()
  131. }()
  132. var keys []string
  133. var data []interface{}
  134. for k, v := range detail {
  135. b, _ := json.Marshal(v)
  136. key := keyItemDetail(k)
  137. keys = append(keys, key)
  138. data = append(data, key, b)
  139. }
  140. log.Info("MSET %v", keys)
  141. if err = conn.Send("MSET", data...); err != nil {
  142. return
  143. }
  144. for i := 0; i < len(data); i += 2 {
  145. conn.Send("EXPIRE", data[i], CacheTimeout)
  146. }
  147. return
  148. }
  149. // DelItemCache 删除项目相关缓存
  150. func (d *Dao) DelItemCache(c context.Context, ids []int64) (res bool, err error) {
  151. var (
  152. keys []interface{}
  153. )
  154. conn := d.redis.Get(c)
  155. defer func() {
  156. conn.Flush()
  157. conn.Close()
  158. }()
  159. for _, id := range ids {
  160. keys = append(keys, keyItem(id))
  161. keys = append(keys, keyItemDetail(id))
  162. }
  163. log.Info("DEL %v", keys)
  164. if err = conn.Send("DEL", keys...); err != nil {
  165. return
  166. }
  167. return
  168. }