memcache.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/answer/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _answerTimeKey = "v3_at_%d" // key of user's answer limit time
  11. _answerHistoryKey = "v3_ah_%d" // key of user's answer history
  12. _answerQidListKey = "v3_aqbi_%d"
  13. _answerExtraQidListKey = "v3_aql_%d_%d"
  14. _answerBlockKey = "v3_ablk_%d" // key of user's answer block flag
  15. _answerHistory = "hid_%d" // ah_hid
  16. )
  17. var (
  18. _blockFlag = []byte("1")
  19. )
  20. func answerQidListKey(mid int64, t int8) (key string) {
  21. switch t {
  22. case model.BaseExtraPassQ:
  23. key = fmt.Sprintf(_answerExtraQidListKey, t, mid)
  24. case model.BaseExtraNoPassQ:
  25. key = fmt.Sprintf(_answerExtraQidListKey, t, mid)
  26. default:
  27. key = fmt.Sprintf(_answerQidListKey, mid)
  28. }
  29. return
  30. }
  31. // pingMC ping memcache.
  32. func (d *Dao) pingMC(c context.Context) (err error) {
  33. conn := d.mc.Get(c)
  34. defer conn.Close()
  35. if err = conn.Set(&memcache.Item{
  36. Key: "ping",
  37. Value: []byte{1},
  38. Expiration: d.mcExpire,
  39. }); err != nil {
  40. log.Error("conn.Set(ping, 1) error(%v)", err)
  41. }
  42. return
  43. }
  44. // ExpireCache get user's answer stime and base answer error times cache.
  45. func (d *Dao) ExpireCache(c context.Context, mid int64) (at *model.AnswerTime, err error) {
  46. key := fmt.Sprintf(_answerTimeKey, mid)
  47. conn := d.mc.Get(c)
  48. defer conn.Close()
  49. item, err := conn.Get(key)
  50. if err != nil {
  51. if err == memcache.ErrNotFound {
  52. err = nil
  53. return
  54. }
  55. log.Error("conn.Get(%s) error(%v)", key, err)
  56. return
  57. }
  58. at = &model.AnswerTime{}
  59. if err = conn.Scan(item, at); err != nil {
  60. log.Error("conn.Scan(%s) error(%v)", string(item.Value), err)
  61. }
  62. return
  63. }
  64. // SetExpireCache set user's answer stime and base answer error times cache.
  65. func (d *Dao) SetExpireCache(c context.Context, mid int64, at *model.AnswerTime) (err error) {
  66. key := fmt.Sprintf(_answerTimeKey, mid)
  67. conn := d.mc.Get(c)
  68. defer conn.Close()
  69. if err = conn.Set(&memcache.Item{
  70. Key: key,
  71. Object: at,
  72. Flags: memcache.FlagJSON,
  73. Expiration: d.mcExpire,
  74. }); err != nil {
  75. log.Error("conn.Set(%s, %v) error(%v)", key, at, err)
  76. }
  77. return
  78. }
  79. // DelExpireCache delete user's answer stime and base answer error times cache.
  80. func (d *Dao) DelExpireCache(c context.Context, mid int64) (err error) {
  81. key := fmt.Sprintf(_answerTimeKey, mid)
  82. conn := d.mc.Get(c)
  83. defer conn.Close()
  84. if err = conn.Delete(key); err != nil {
  85. if err == memcache.ErrNotFound {
  86. err = nil
  87. return
  88. }
  89. log.Error("conn.Delete(%s) error(%v)", key, err)
  90. }
  91. return
  92. }
  93. // HistoryCache get user's answer history cache
  94. func (d *Dao) HistoryCache(c context.Context, mid int64) (ah *model.AnswerHistory, err error) {
  95. key := fmt.Sprintf(_answerHistoryKey, mid)
  96. conn := d.mc.Get(c)
  97. defer conn.Close()
  98. item, err := conn.Get(key)
  99. if err != nil {
  100. if err == memcache.ErrNotFound {
  101. err = nil
  102. return
  103. }
  104. log.Error("conn.Get(%s) error(%v)", key, err)
  105. return
  106. }
  107. ah = &model.AnswerHistory{}
  108. if err = conn.Scan(item, ah); err != nil {
  109. log.Error("conn.Scan(%s) error(%v)", string(item.Value), err)
  110. }
  111. return
  112. }
  113. // SetHistoryCache set user's answer history cache
  114. func (d *Dao) SetHistoryCache(c context.Context, mid int64, ah *model.AnswerHistory) (err error) {
  115. key := fmt.Sprintf(_answerHistoryKey, mid)
  116. conn := d.mc.Get(c)
  117. defer conn.Close()
  118. if err = conn.Set(&memcache.Item{
  119. Key: key,
  120. Object: ah,
  121. Flags: memcache.FlagJSON,
  122. Expiration: d.mcExpire,
  123. }); err != nil {
  124. log.Error("conn.Set(%s, %v) error(%v)", key, ah, err)
  125. }
  126. return
  127. }
  128. // DelHistoryCache delete user's answer history cache
  129. func (d *Dao) DelHistoryCache(c context.Context, mid int64) (err error) {
  130. key := fmt.Sprintf(_answerHistoryKey, mid)
  131. conn := d.mc.Get(c)
  132. defer conn.Close()
  133. if err = conn.Delete(key); err != nil {
  134. if err == memcache.ErrNotFound {
  135. err = nil
  136. return
  137. }
  138. log.Error("DelHistoryCache(%d),err:%+v", mid, err)
  139. }
  140. return
  141. }
  142. // IdsCache get user's base question ids
  143. func (d *Dao) IdsCache(c context.Context, mid int64, t int8) (ids []int64, err error) {
  144. key := answerQidListKey(mid, t)
  145. conn := d.mc.Get(c)
  146. defer conn.Close()
  147. item, err := conn.Get(key)
  148. if err != nil {
  149. if err == memcache.ErrNotFound {
  150. err = nil
  151. return
  152. }
  153. log.Error("d.IdsCache(%d,%d) error(%v)", mid, t, err)
  154. return
  155. }
  156. if err = conn.Scan(item, &ids); err != nil {
  157. log.Error("conn.Scan(%s) error(%v)", string(item.Value), err)
  158. }
  159. return
  160. }
  161. // SetIdsCache set user's base question ids
  162. func (d *Dao) SetIdsCache(c context.Context, mid int64, ids []int64, t int8) (err error) {
  163. key := answerQidListKey(mid, t)
  164. conn := d.mc.Get(c)
  165. defer conn.Close()
  166. if err = conn.Set(&memcache.Item{
  167. Key: key,
  168. Object: ids,
  169. Flags: memcache.FlagJSON,
  170. Expiration: d.mcExpire,
  171. }); err != nil {
  172. log.Error("conn.Set(%s, %v) error(%v)", key, ids, err)
  173. }
  174. return
  175. }
  176. // DelIdsCache delete user's base question ids
  177. func (d *Dao) DelIdsCache(c context.Context, mid int64, t int8) (err error) {
  178. key := answerQidListKey(mid, t)
  179. conn := d.mc.Get(c)
  180. defer conn.Close()
  181. if err = conn.Delete(key); err != nil {
  182. if err == memcache.ErrNotFound {
  183. err = nil
  184. return
  185. }
  186. log.Error("DelIdsCache(%d,%d),err:%+v", mid, t, err)
  187. }
  188. return
  189. }
  190. // SetBlockCache set user's block.
  191. func (d *Dao) SetBlockCache(c context.Context, mid int64) (err error) {
  192. key := fmt.Sprintf(_answerBlockKey, mid)
  193. conn := d.mc.Get(c)
  194. defer conn.Close()
  195. if err = conn.Set(&memcache.Item{
  196. Key: key,
  197. Value: _blockFlag,
  198. Expiration: d.answerBlockExpire,
  199. }); err != nil {
  200. log.Error("conn.Store(%s, %v) error(%v)", key, string(_blockFlag), err)
  201. }
  202. return
  203. }
  204. // CheckBlockCache check user's block.
  205. func (d *Dao) CheckBlockCache(c context.Context, mid int64) (exist bool, err error) {
  206. key := fmt.Sprintf(_answerBlockKey, mid)
  207. conn := d.mc.Get(c)
  208. defer conn.Close()
  209. _, err = conn.Get(key)
  210. if err != nil {
  211. if err == memcache.ErrNotFound {
  212. err = nil
  213. return
  214. }
  215. log.Error("conn.Get(%s) error(%v)", key, err)
  216. return
  217. }
  218. exist = true
  219. return
  220. }
  221. // HidCache get user's answer history cache
  222. func (d *Dao) HidCache(c context.Context, hid int64) (ah *model.AnswerHistory, err error) {
  223. key := fmt.Sprintf(_answerHistory, hid)
  224. conn := d.mc.Get(c)
  225. defer conn.Close()
  226. item, err := conn.Get(key)
  227. if err != nil {
  228. if err == memcache.ErrNotFound {
  229. err = nil
  230. return
  231. }
  232. log.Error("conn.Get(%s) error(%v)", key, err)
  233. return
  234. }
  235. ah = &model.AnswerHistory{}
  236. if err = conn.Scan(item, ah); err != nil {
  237. log.Error("conn.Scan(%s) error(%v)", string(item.Value), err)
  238. }
  239. return
  240. }
  241. // SetHidCache set user's answer history cache
  242. func (d *Dao) SetHidCache(c context.Context, ah *model.AnswerHistory) (err error) {
  243. key := fmt.Sprintf(_answerHistory, ah.Hid)
  244. conn := d.mc.Get(c)
  245. defer conn.Close()
  246. if err = conn.Set(&memcache.Item{
  247. Key: key,
  248. Object: ah,
  249. Flags: memcache.FlagJSON,
  250. Expiration: d.mcExpire,
  251. }); err != nil {
  252. log.Error("conn.Set(%s, %v) error(%v)", key, ah, err)
  253. }
  254. return
  255. }