mc.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package dao
  2. import (
  3. "context"
  4. "math/rand"
  5. "strconv"
  6. "go-common/app/service/main/passport-game/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _keyPrefixInfoPB = "pa2_"
  12. _keyPrefixTokenPB = "pt_"
  13. _keyPrefixOriginMissMatchFlag = "m_"
  14. _keyPrefixOriginToken = "pot_"
  15. _missMatchFlagExpireSeconds = 30 // 30 seconds
  16. )
  17. func keyInfoPB(mid int64) string {
  18. return _keyPrefixInfoPB + strconv.FormatInt(mid, 10)
  19. }
  20. func keyTokenPB(accessToken string) string {
  21. return _keyPrefixTokenPB + accessToken
  22. }
  23. func keyOriginToken(accessToken string) string {
  24. return _keyPrefixOriginToken + accessToken
  25. }
  26. func keyOriginMissMatchFlag(identify string) string {
  27. return _keyPrefixOriginMissMatchFlag + identify
  28. }
  29. // pingMC check connection success.
  30. func (d *Dao) pingMC(c context.Context) (err error) {
  31. item := &memcache.Item{
  32. Key: "ping",
  33. Value: []byte{1},
  34. Expiration: d.mcExpire,
  35. }
  36. conn := d.mc.Get(c)
  37. defer conn.Close()
  38. if err = conn.Set(item); err != nil {
  39. log.Error("conn.Set(%v) error(%v)", item, err)
  40. }
  41. return
  42. }
  43. // SetInfoCache set info into cache.
  44. func (d *Dao) SetInfoCache(c context.Context, info *model.Info) (err error) {
  45. item := &memcache.Item{
  46. Key: keyInfoPB(info.Mid),
  47. Object: info,
  48. Flags: memcache.FlagProtobuf,
  49. Expiration: d.mcExpire,
  50. }
  51. conn := d.mc.Get(c)
  52. defer conn.Close()
  53. if err = conn.Set(item); err != nil {
  54. log.Error("conn.Set(%s) error(%v)", item.Key, err)
  55. }
  56. return
  57. }
  58. // InfoCache get info cache from cache.
  59. func (d *Dao) InfoCache(c context.Context, mid int64) (info *model.Info, err error) {
  60. key := keyInfoPB(mid)
  61. conn := d.mc.Get(c)
  62. defer conn.Close()
  63. var item *memcache.Item
  64. if item, err = conn.Get(key); err != nil {
  65. if err == memcache.ErrNotFound {
  66. err = nil
  67. return
  68. }
  69. log.Error("conn.Get(%s) error(%v)", key, err)
  70. return
  71. }
  72. info = new(model.Info)
  73. if err = conn.Scan(item, info); err != nil {
  74. log.Error("conn.Scan(%s, %s) error(%v)", key, item.Value, err)
  75. }
  76. return
  77. }
  78. // SetTokenCache set token into cache.
  79. func (d *Dao) SetTokenCache(c context.Context, token *model.Perm) (err error) {
  80. item := &memcache.Item{
  81. Key: keyTokenPB(token.AccessToken),
  82. Object: token,
  83. Flags: memcache.FlagProtobuf,
  84. Expiration: d.mcExpire,
  85. }
  86. conn := d.mc.Get(c)
  87. defer conn.Close()
  88. if err = conn.Set(item); err != nil {
  89. log.Error("conn.Set(%s) error(%v)", item.Key, err)
  90. }
  91. return
  92. }
  93. // TokenCache get token from cache.
  94. func (d *Dao) TokenCache(c context.Context, accessToken string) (token *model.Perm, err error) {
  95. key := keyTokenPB(accessToken)
  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. token = new(model.Perm)
  108. if err = conn.Scan(item, token); err != nil {
  109. log.Error("conn.Scan(%s, %s) error(%v)", item.Key, item.Value, err)
  110. }
  111. return
  112. }
  113. // DelTokenCache delete token from cache.
  114. func (d *Dao) DelTokenCache(c context.Context, accessToken string) (err error) {
  115. key := keyTokenPB(accessToken)
  116. conn := d.mc.Get(c)
  117. defer conn.Close()
  118. if err = conn.Delete(key); err != nil {
  119. if err == memcache.ErrNotFound {
  120. err = nil
  121. return
  122. }
  123. log.Error("conn.Delete(%s) error(%v)", key, err)
  124. }
  125. return
  126. }
  127. // SetOriginMissMatchFlagCache set origin miss match flag cache.
  128. func (d *Dao) SetOriginMissMatchFlagCache(c context.Context, identify string, flag []byte) (err error) {
  129. item := &memcache.Item{
  130. Key: keyOriginMissMatchFlag(identify),
  131. Value: flag,
  132. Expiration: _missMatchFlagExpireSeconds,
  133. }
  134. conn := d.mc.Get(c)
  135. defer conn.Close()
  136. if err = conn.Set(item); err != nil {
  137. log.Error("conn.Set(%s) error(%v)", item.Key, err)
  138. }
  139. return
  140. }
  141. // OriginMissMatchFlagCache get origin miss match flag.
  142. func (d *Dao) OriginMissMatchFlagCache(c context.Context, identify string) (res []byte, err error) {
  143. key := keyOriginMissMatchFlag(identify)
  144. conn := d.mc.Get(c)
  145. defer conn.Close()
  146. var item *memcache.Item
  147. if item, err = conn.Get(key); err != nil {
  148. if err == memcache.ErrNotFound {
  149. err = nil
  150. return
  151. }
  152. log.Error("conn.Get(%s) error(%v)", key, err)
  153. return
  154. }
  155. res = item.Value
  156. return
  157. }
  158. // DelOriginMissMatchFlagCache delete origin miss match flag.
  159. func (d *Dao) DelOriginMissMatchFlagCache(c context.Context, identify string) (err error) {
  160. key := keyOriginMissMatchFlag(identify)
  161. conn := d.mc.Get(c)
  162. defer conn.Close()
  163. if err = conn.Delete(key); err != nil {
  164. if err == memcache.ErrNotFound {
  165. err = nil
  166. return
  167. }
  168. log.Error("conn.Delete(%s) error(%v)", key, err)
  169. }
  170. return
  171. }
  172. // SetOriginTokenCache set passport token into cache.
  173. func (d *Dao) SetOriginTokenCache(c context.Context, token *model.Token) (err error) {
  174. item := &memcache.Item{
  175. Key: keyOriginToken(token.AccessToken),
  176. Object: token,
  177. Flags: memcache.FlagJSON,
  178. Expiration: 60 + rand.Int31n(600),
  179. }
  180. conn := d.mc.Get(c)
  181. defer conn.Close()
  182. if err = conn.Set(item); err != nil {
  183. log.Error("conn.Set(%s) error(%v)", item.Key, err)
  184. return
  185. }
  186. return
  187. }
  188. // OriginTokenCache set passport token into cache.
  189. func (d *Dao) OriginTokenCache(c context.Context, accessToken string) (token *model.Token, err error) {
  190. key := keyOriginToken(accessToken)
  191. conn := d.mc.Get(c)
  192. defer conn.Close()
  193. item, err := conn.Get(key)
  194. if err != nil {
  195. if err == memcache.ErrNotFound {
  196. err = nil
  197. return
  198. }
  199. log.Error("conn.Get(%s) error(%v)", key, err)
  200. return
  201. }
  202. token = new(model.Token)
  203. if err = conn.Scan(item, token); err != nil {
  204. log.Error("conn.Scan(%s, %s) error(%v)", item.Key, item.Value, err)
  205. return
  206. }
  207. return
  208. }
  209. // DelOriginTokenCache delete passport token from cache.
  210. func (d *Dao) DelOriginTokenCache(c context.Context, accessToken string) (err error) {
  211. key := keyOriginToken(accessToken)
  212. conn := d.mc.Get(c)
  213. defer conn.Close()
  214. if err = conn.Delete(key); err != nil {
  215. if err == memcache.ErrNotFound {
  216. err = nil
  217. return
  218. }
  219. log.Error("conn.Delete(%s) error(%v)", key, err)
  220. return
  221. }
  222. return
  223. }