medal_mc.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/admin/main/usersuit/model"
  6. gmc "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _prefixActivatedNid = "usma:" // key of activated medal nid
  11. _prefixOwners = "umos:" // key of owners info
  12. _prefixRedPoint = "usrp:" // key of red point
  13. _prefixPopup = "uspp:" // key of new medal popup
  14. )
  15. // medalactivated medal nid key.
  16. func activatedNidKey(mid int64) string {
  17. return _prefixActivatedNid + strconv.FormatInt(mid, 10)
  18. }
  19. // ownersKey medal_owner key.
  20. func ownersKey(mid int64) string {
  21. return _prefixOwners + strconv.FormatInt(mid, 10)
  22. }
  23. //RedPointKey new medal RedPoint key.
  24. func RedPointKey(mid int64) string {
  25. return _prefixRedPoint + strconv.FormatInt(mid, 10)
  26. }
  27. // PopupKey new medal popup key.
  28. func PopupKey(mid int64) string {
  29. return _prefixPopup + strconv.FormatInt(mid, 10)
  30. }
  31. func (d *Dao) pingMC(c context.Context) (err error) {
  32. conn := d.mc.Get(c)
  33. defer conn.Close()
  34. if err = conn.Set(&gmc.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}); err != nil {
  35. log.Error("conn.Store(set, ping, 1) error(%v)", err)
  36. }
  37. return
  38. }
  39. // MedalOwnersCache get medal_owner cache.
  40. func (d *Dao) MedalOwnersCache(c context.Context, mid int64) (res []*model.MedalOwner, err error) {
  41. conn := d.mc.Get(c)
  42. defer conn.Close()
  43. item, err := conn.Get(ownersKey(mid))
  44. if err != nil {
  45. if err == gmc.ErrNotFound {
  46. res = nil
  47. err = nil
  48. return
  49. }
  50. log.Error("d.MedalOwnersCache err(%v)", err)
  51. return
  52. }
  53. res = make([]*model.MedalOwner, 0)
  54. if err = conn.Scan(item, &res); err != nil {
  55. log.Error("d.MedalOwnersCache err(%v)", err)
  56. }
  57. return
  58. }
  59. // SetMedalOwnersache set medal_owner cache.
  60. func (d *Dao) SetMedalOwnersache(c context.Context, mid int64, nos []*model.MedalOwner) (err error) {
  61. key := ownersKey(mid)
  62. item := &gmc.Item{Key: key, Object: nos, Expiration: d.mcExpire, Flags: gmc.FlagJSON}
  63. conn := d.mc.Get(c)
  64. defer conn.Close()
  65. if err = conn.Set(item); err != nil {
  66. log.Error("SetMedalOwnersache err(%v)", err)
  67. }
  68. return
  69. }
  70. // DelMedalOwnersCache delete medal_owner cache.
  71. func (d *Dao) DelMedalOwnersCache(c context.Context, mid int64) (err error) {
  72. key := ownersKey(mid)
  73. conn := d.mc.Get(c)
  74. defer conn.Close()
  75. if err = conn.Delete(key); err != nil {
  76. if err == gmc.ErrNotFound {
  77. err = nil
  78. } else {
  79. log.Error("d.DelMedalOwnersCache(%s) error(%v)", key, err)
  80. }
  81. }
  82. return
  83. }
  84. // MedalActivatedCache get user activated medal nid.
  85. func (d *Dao) MedalActivatedCache(c context.Context, mid int64) (nid int64, err error) {
  86. conn := d.mc.Get(c)
  87. defer conn.Close()
  88. item, err := conn.Get(activatedNidKey(mid))
  89. if err != nil {
  90. if err == gmc.ErrNotFound {
  91. nid = 0
  92. err = nil
  93. return
  94. }
  95. log.Error("d.MedalActivatedCache(mid:%d) err(%v)", mid, err)
  96. return
  97. }
  98. if err = conn.Scan(item, &nid); err != nil {
  99. log.Error("d.MedalActivatedCache(mid:%d) err(%v)", mid, err)
  100. }
  101. return
  102. }
  103. // SetMedalActivatedCache set activated medal cache.
  104. func (d *Dao) SetMedalActivatedCache(c context.Context, mid, nid int64) (err error) {
  105. key := activatedNidKey(mid)
  106. item := &gmc.Item{Key: key, Object: nid, Expiration: d.mcExpire, Flags: gmc.FlagJSON}
  107. conn := d.mc.Get(c)
  108. defer conn.Close()
  109. if err = conn.Set(item); err != nil {
  110. log.Error("SetMedalActivatedCache err(%v)", err)
  111. }
  112. return
  113. }
  114. // DelMedalActivatedCache delete activated medal cache.
  115. func (d *Dao) DelMedalActivatedCache(c context.Context, mid int64) (err error) {
  116. key := activatedNidKey(mid)
  117. conn := d.mc.Get(c)
  118. defer conn.Close()
  119. if err = conn.Delete(key); err != nil {
  120. if err == gmc.ErrNotFound {
  121. err = nil
  122. } else {
  123. log.Error("d.DelMedalActivatedCache(%s) error(%v)", key, err)
  124. }
  125. }
  126. return
  127. }
  128. // PopupCache get new medal info popup cache.
  129. func (d *Dao) PopupCache(c context.Context, mid int64) (nid int64, err error) {
  130. conn := d.mc.Get(c)
  131. defer conn.Close()
  132. item, err := conn.Get(PopupKey(mid))
  133. if err != nil {
  134. if err == gmc.ErrNotFound {
  135. nid = 0
  136. err = nil
  137. return
  138. }
  139. log.Error("d.PopupCache(mid:%d) err(%v)", mid, err)
  140. return
  141. }
  142. if err = conn.Scan(item, &nid); err != nil {
  143. log.Error("d.PopupCache(mid:%d) err(%v)", mid, err)
  144. }
  145. return
  146. }
  147. // SetPopupCache set popup cache.
  148. func (d *Dao) SetPopupCache(c context.Context, mid, nid int64) (err error) {
  149. key := PopupKey(mid)
  150. item := &gmc.Item{Key: key, Object: nid, Expiration: d.pointExpire, Flags: gmc.FlagJSON}
  151. conn := d.mc.Get(c)
  152. defer conn.Close()
  153. if err = conn.Set(item); err != nil {
  154. log.Error("SetMedalOwnersache err(%v)", err)
  155. }
  156. return
  157. }
  158. // DelPopupCache delete new medal info popup cache.
  159. func (d *Dao) DelPopupCache(c context.Context, mid int64) (err error) {
  160. key := PopupKey(mid)
  161. conn := d.mc.Get(c)
  162. defer conn.Close()
  163. if err = conn.Delete(key); err != nil {
  164. if err == gmc.ErrNotFound {
  165. err = nil
  166. } else {
  167. log.Error("d.DelPopupCache(%s) error(%v)", key, err)
  168. }
  169. }
  170. return
  171. }
  172. // RedPointCache get new medal info red point cache.
  173. func (d *Dao) RedPointCache(c context.Context, mid int64) (nid int64, err error) {
  174. conn := d.mc.Get(c)
  175. defer conn.Close()
  176. item, err := conn.Get(RedPointKey(mid))
  177. if err != nil {
  178. if err == gmc.ErrNotFound {
  179. err = nil
  180. return
  181. }
  182. log.Error("d.RedPointCache(mid:%d) err(%v)", mid, err)
  183. return
  184. }
  185. if err = conn.Scan(item, &nid); err != nil {
  186. log.Error("d.RedPointCache(mid:%d) err(%v)", mid, err)
  187. }
  188. return
  189. }
  190. // SetRedPointCache set red point cache.
  191. func (d *Dao) SetRedPointCache(c context.Context, mid, nid int64) (err error) {
  192. key := RedPointKey(mid)
  193. item := &gmc.Item{Key: key, Object: nid, Expiration: d.pointExpire, Flags: gmc.FlagJSON}
  194. conn := d.mc.Get(c)
  195. defer conn.Close()
  196. if err = conn.Set(item); err != nil {
  197. log.Error("SetRedPointCache(%d %d) err(%v)", mid, nid, err)
  198. }
  199. return
  200. }
  201. // DelRedPointCache delete new medal info red point cache.
  202. func (d *Dao) DelRedPointCache(c context.Context, mid int64) (err error) {
  203. key := RedPointKey(mid)
  204. conn := d.mc.Get(c)
  205. defer conn.Close()
  206. if err = conn.Delete(key); err != nil {
  207. if err == gmc.ErrNotFound {
  208. err = nil
  209. } else {
  210. log.Error("d.DelRedPointCache(%d) error(%v)", mid, err)
  211. }
  212. }
  213. return
  214. }