memcache.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. model "go-common/app/interface/main/credit/model"
  7. gmc "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _noticeInfo = "notice" // key of creditInfo
  12. _reasonList = "reason" // key of creditInfo
  13. _opinion = "op_v2_" // user opinion prefix.
  14. _questionids = "question" // key of creditInfo
  15. _labourIsAnswer = "labour_%d" // key of labourIsAnswer
  16. _juryInfo = "jy_" // key of jury info
  17. )
  18. func noticeKey() string {
  19. return _noticeInfo
  20. }
  21. func reasonKey() string {
  22. return _reasonList
  23. }
  24. // user opinion key.
  25. func opinionKey(opid int64) string {
  26. return _opinion + strconv.FormatInt(opid, 10)
  27. }
  28. func questionKey(mid int64) string {
  29. return _questionids + strconv.FormatInt(mid, 10)
  30. }
  31. func labourKey(mid int64) string {
  32. return fmt.Sprintf(_labourIsAnswer, mid)
  33. }
  34. // user jury info key.
  35. func juryInfoKey(mid int64) string {
  36. return _juryInfo + strconv.FormatInt(mid, 10)
  37. }
  38. func (d *Dao) pingMC(c context.Context) (err error) {
  39. conn := d.mc.Get(c)
  40. if err = conn.Set(&gmc.Item{Key: "ping", Value: []byte{1}, Expiration: d.commonExpire}); err != nil {
  41. log.Error("conn.Store(set, ping, 1) error(%v)", err)
  42. }
  43. conn.Close()
  44. return
  45. }
  46. // NoticeInfoCache get notice info cache.
  47. func (d *Dao) NoticeInfoCache(c context.Context) (dt *model.Notice, err error) {
  48. var conn = d.mc.Get(c)
  49. defer conn.Close()
  50. item, err := conn.Get(noticeKey())
  51. if err != nil {
  52. if err == gmc.ErrNotFound {
  53. err = nil
  54. }
  55. return
  56. }
  57. dt = &model.Notice{}
  58. err = conn.Scan(item, &dt)
  59. return
  60. }
  61. // SetReasonListCache get notice info cache.
  62. func (d *Dao) SetReasonListCache(c context.Context, dt []*model.Reason) (err error) {
  63. var (
  64. item = &gmc.Item{Key: reasonKey(), Object: dt, Expiration: d.minCommonExpire, Flags: gmc.FlagJSON}
  65. conn = d.mc.Get(c)
  66. )
  67. defer conn.Close()
  68. err = conn.Set(item)
  69. return
  70. }
  71. // SetNoticeInfoCache get Notice info cache.
  72. func (d *Dao) SetNoticeInfoCache(c context.Context, n *model.Notice) (err error) {
  73. var (
  74. item = &gmc.Item{Key: noticeKey(), Object: n, Expiration: d.minCommonExpire, Flags: gmc.FlagJSON}
  75. conn = d.mc.Get(c)
  76. )
  77. defer conn.Close()
  78. err = conn.Set(item)
  79. return
  80. }
  81. // DelNoticeInfoCache delete notice info cache.
  82. func (d *Dao) DelNoticeInfoCache(c context.Context, mid int64) (err error) {
  83. conn := d.mc.Get(c)
  84. defer conn.Close()
  85. if err = conn.Delete(noticeKey()); err != nil {
  86. if err == gmc.ErrNotFound {
  87. err = nil
  88. }
  89. }
  90. return
  91. }
  92. // ReasonListCache get Reason info cache.
  93. func (d *Dao) ReasonListCache(c context.Context) (dt []*model.Reason, err error) {
  94. conn := d.mc.Get(c)
  95. defer conn.Close()
  96. item, err := conn.Get(reasonKey())
  97. if err != nil {
  98. if err == gmc.ErrNotFound {
  99. err = nil
  100. }
  101. return
  102. }
  103. dt = make([]*model.Reason, 0)
  104. err = conn.Scan(item, &dt)
  105. return
  106. }
  107. // DelReasonListCache delete reason info cache.
  108. func (d *Dao) DelReasonListCache(c context.Context) (err error) {
  109. conn := d.mc.Get(c)
  110. defer conn.Close()
  111. if err = conn.Delete(reasonKey()); err != nil {
  112. if err == gmc.ErrNotFound {
  113. err = nil
  114. }
  115. }
  116. return
  117. }
  118. // AddOpinionCache add opinion cache.
  119. func (d *Dao) AddOpinionCache(c context.Context, op *model.Opinion) (err error) {
  120. var (
  121. item = &gmc.Item{Key: opinionKey(op.OpID), Object: op, Expiration: d.commonExpire, Flags: gmc.FlagJSON}
  122. conn = d.mc.Get(c)
  123. )
  124. defer conn.Close()
  125. err = conn.Set(item)
  126. return
  127. }
  128. // OpinionsCache get opids opinion detail.
  129. func (d *Dao) OpinionsCache(c context.Context, opids []int64) (ops map[int64]*model.Opinion, miss []int64, err error) {
  130. var (
  131. keys []string
  132. conn = d.mc.Get(c)
  133. )
  134. for _, id := range opids {
  135. keys = append(keys, opinionKey(id))
  136. }
  137. defer conn.Close()
  138. res, err := conn.GetMulti(keys)
  139. if err != nil {
  140. return
  141. }
  142. ops = make(map[int64]*model.Opinion, len(res))
  143. for _, id := range opids {
  144. if r, ok := res[opinionKey(id)]; ok {
  145. op := &model.Opinion{}
  146. if err = conn.Scan(r, &op); err != nil {
  147. return
  148. }
  149. ops[op.OpID] = op
  150. } else {
  151. miss = append(miss, id)
  152. }
  153. }
  154. return
  155. }
  156. // SetQsCache set labour questions cache.
  157. func (d *Dao) SetQsCache(c context.Context, mid int64, qsid *model.QsCache) (err error) {
  158. var (
  159. item = &gmc.Item{Key: questionKey(mid), Object: qsid, Expiration: d.commonExpire, Flags: gmc.FlagJSON}
  160. conn = d.mc.Get(c)
  161. )
  162. defer conn.Close()
  163. err = conn.Set(item)
  164. return
  165. }
  166. // GetQsCache get labour qestions info cache.
  167. func (d *Dao) GetQsCache(c context.Context, mid int64) (qsid *model.QsCache, err error) {
  168. var (
  169. reply *gmc.Item
  170. conn = d.mc.Get(c)
  171. )
  172. defer conn.Close()
  173. reply, err = conn.Get(questionKey(mid))
  174. if err != nil || reply == nil {
  175. if err == gmc.ErrNotFound {
  176. err = nil
  177. }
  178. return
  179. }
  180. qsid = &model.QsCache{}
  181. err = conn.Scan(reply, &qsid)
  182. return
  183. }
  184. // DelQsCache delete labour questions cache.
  185. func (d *Dao) DelQsCache(c context.Context, mid int64) (err error) {
  186. conn := d.mc.Get(c)
  187. defer conn.Close()
  188. if err = conn.Delete(questionKey(mid)); err != nil {
  189. if err == gmc.ErrNotFound {
  190. err = nil
  191. }
  192. }
  193. return
  194. }
  195. // SetAnswerStateCache set labour answer state cache.
  196. func (d *Dao) SetAnswerStateCache(c context.Context, mid int64, state int8) (err error) {
  197. var (
  198. conn = d.mc.Get(c)
  199. item = &gmc.Item{Key: labourKey(mid), Object: state, Expiration: d.userExpire, Flags: gmc.FlagJSON}
  200. )
  201. defer conn.Close()
  202. err = conn.Set(item)
  203. return
  204. }
  205. // GetAnswerStateCache get labour answer state cache.
  206. func (d *Dao) GetAnswerStateCache(c context.Context, mid int64) (state int8, found bool, err error) {
  207. var (
  208. reply *gmc.Item
  209. conn = d.mc.Get(c)
  210. )
  211. defer conn.Close()
  212. if reply, err = conn.Get(labourKey(mid)); err != nil {
  213. if err == gmc.ErrNotFound {
  214. err = nil
  215. }
  216. return
  217. }
  218. if err = conn.Scan(reply, &state); err != nil {
  219. return
  220. }
  221. found = true
  222. return
  223. }
  224. // JuryInfoCache .
  225. func (d *Dao) JuryInfoCache(c context.Context, mid int64) (bj *model.BlockedJury, err error) {
  226. conn := d.mc.Get(c)
  227. defer conn.Close()
  228. item, err := conn.Get(juryInfoKey(mid))
  229. if err != nil {
  230. if err == gmc.ErrNotFound {
  231. err = nil
  232. }
  233. return
  234. }
  235. bj = &model.BlockedJury{}
  236. err = conn.Scan(item, &bj)
  237. return
  238. }
  239. // SetJuryInfoCache .
  240. func (d *Dao) SetJuryInfoCache(c context.Context, mid int64, bj *model.BlockedJury) (err error) {
  241. var (
  242. conn = d.mc.Get(c)
  243. item = &gmc.Item{Key: juryInfoKey(mid), Object: bj, Expiration: d.userExpire, Flags: gmc.FlagJSON}
  244. )
  245. defer conn.Close()
  246. err = conn.Set(item)
  247. return
  248. }
  249. // DelJuryInfoCache del jury cache info.
  250. func (d *Dao) DelJuryInfoCache(c context.Context, mid int64) (err error) {
  251. conn := d.mc.Get(c)
  252. defer conn.Close()
  253. if err = conn.Delete(juryInfoKey(mid)); err != nil {
  254. if err == gmc.ErrNotFound {
  255. err = nil
  256. }
  257. }
  258. return
  259. }