cache.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package unicom
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/app-wall/model/unicom"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _prefix = "unicoms_user_%v"
  12. _userbindkey = "unicoms_user_bind_%d"
  13. _userbindreceive = "unicom_pack_receives_%d"
  14. _userflowkey = "unicom_user_flow_%v"
  15. _userflowlistkey = "unicom_user_flow_list"
  16. )
  17. func keyUserBind(mid int64) string {
  18. return fmt.Sprintf(_userbindkey, mid)
  19. }
  20. func keyUserBindReceive(mid int64) string {
  21. return fmt.Sprintf(_userbindreceive, mid)
  22. }
  23. func keyUnicom(usermob string) string {
  24. return fmt.Sprintf(_prefix, usermob)
  25. }
  26. func keyUserFlow(key string) string {
  27. return fmt.Sprintf(_userflowkey, key)
  28. }
  29. // UserBindCache user bind cache
  30. func (d *Dao) UserBindCache(c context.Context, mid int64) (ub *unicom.UserBind, err error) {
  31. var (
  32. key = keyUserBind(mid)
  33. conn = d.mc.Get(c)
  34. r *memcache.Item
  35. )
  36. defer conn.Close()
  37. if r, err = conn.Get(key); err != nil {
  38. if err != memcache.ErrNotFound {
  39. log.Error("UserBindCache error(%v) or mid(%v)", err, mid)
  40. }
  41. return
  42. }
  43. if err = conn.Scan(r, &ub); err != nil {
  44. log.Error("r.Scan(%s) error(%v)", r.Value, err)
  45. }
  46. return
  47. }
  48. // AddUserBindCache add user user bind cache
  49. func (d *Dao) AddUserBindCache(c context.Context, mid int64, ub *unicom.UserBind) (err error) {
  50. var (
  51. key = keyUserBind(mid)
  52. conn = d.mc.Get(c)
  53. )
  54. if err = conn.Set(&memcache.Item{Key: key, Object: ub, Flags: memcache.FlagJSON, Expiration: 0}); err != nil {
  55. log.Error("AddUserBindCache d.mc.Set(%s,%v) error(%v)", key, ub, err)
  56. }
  57. conn.Close()
  58. return
  59. }
  60. // DeleteUserBindCache delete user bind cache
  61. func (d *Dao) DeleteUserBindCache(c context.Context, mid int64) (err error) {
  62. var (
  63. key = keyUserBind(mid)
  64. conn = d.mc.Get(c)
  65. )
  66. defer conn.Close()
  67. if err = conn.Delete(key); err != nil {
  68. if err == memcache.ErrNotFound {
  69. err = nil
  70. return
  71. }
  72. log.Error("DeleteUserBindCache MemchDB.Delete(%s) error(%v)", key, err)
  73. return
  74. }
  75. return
  76. }
  77. // UserPackReceiveCache user pack cache
  78. func (d *Dao) UserPackReceiveCache(c context.Context, mid int64) (count int, err error) {
  79. var (
  80. key = keyUserBindReceive(mid)
  81. conn = d.mc.Get(c)
  82. r *memcache.Item
  83. )
  84. defer conn.Close()
  85. if r, err = conn.Get(key); err != nil {
  86. if err == memcache.ErrNotFound {
  87. err = nil
  88. return
  89. }
  90. log.Error("UserBindCache MemchDB.Get(%s) error(%v)", key, err)
  91. return
  92. }
  93. if err = conn.Scan(r, &count); err != nil {
  94. log.Error("r.Scan(%s) error(%v)", r.Value, err)
  95. }
  96. return
  97. }
  98. // AddUserPackReceiveCache add user pack cache
  99. func (d *Dao) AddUserPackReceiveCache(c context.Context, mid int64, count int, now time.Time) (err error) {
  100. var (
  101. key = keyUserBindReceive(mid)
  102. conn = d.mc.Get(c)
  103. currenttimeSec = int32((now.Hour() * 60 * 60) + (now.Minute() * 60) + now.Second())
  104. overtime int32
  105. )
  106. if overtime = d.expire - currenttimeSec; overtime < 1 {
  107. overtime = d.expire
  108. }
  109. log.Info("AddUserPackReceiveCache mid(%d) overtime(%d)", mid, overtime)
  110. if err = conn.Set(&memcache.Item{Key: key, Object: count, Flags: memcache.FlagJSON, Expiration: overtime}); err != nil {
  111. log.Error("AddUserPackReceiveCache d.mc.Set(%s,%v) error(%v)", key, count, err)
  112. }
  113. conn.Close()
  114. return
  115. }
  116. // DeleteUserPackReceiveCache delete user pack cache
  117. func (d *Dao) DeleteUserPackReceiveCache(c context.Context, mid int64) (err error) {
  118. var (
  119. key = keyUserBindReceive(mid)
  120. conn = d.mc.Get(c)
  121. )
  122. defer conn.Close()
  123. if err = conn.Delete(key); err != nil {
  124. if err == memcache.ErrNotFound {
  125. err = nil
  126. return
  127. }
  128. log.Error("DeleteUserPackReceiveCache MemchDB.Delete(%s) error(%v)", key, err)
  129. return
  130. }
  131. return
  132. }
  133. // UnicomCache
  134. func (d *Dao) UnicomCache(c context.Context, usermob string) (u []*unicom.Unicom, err error) {
  135. var (
  136. key = keyUnicom(usermob)
  137. conn = d.mc.Get(c)
  138. r *memcache.Item
  139. )
  140. defer conn.Close()
  141. if r, err = conn.Get(key); err != nil {
  142. log.Error("unicomCache MemchDB.Get(%s) error(%v)", key, err)
  143. return
  144. }
  145. if err = conn.Scan(r, &u); err != nil {
  146. log.Error("r.Scan(%s) error(%v)", r.Value, err)
  147. }
  148. return
  149. }
  150. //UserFlowCache unicom flow cache
  151. func (d *Dao) UserFlowCache(c context.Context, keyStr string) (err error) {
  152. var (
  153. key = keyUserFlow(keyStr)
  154. conn = d.mc.Get(c)
  155. r *memcache.Item
  156. res struct{}
  157. )
  158. defer conn.Close()
  159. if r, err = conn.Get(key); err != nil {
  160. log.Error("UserFlowCache MemchDB.Get(%s) error(%v)", key, err)
  161. return
  162. }
  163. if err = conn.Scan(r, &res); err != nil {
  164. log.Error("r.Scan(%s) error(%v)", r.Value, err)
  165. }
  166. return
  167. }
  168. // AddUserFlowCache add user pack cache
  169. func (d *Dao) AddUserFlowCache(c context.Context, keyStr string) (err error) {
  170. var (
  171. key = keyUserFlow(keyStr)
  172. conn = d.mc.Get(c)
  173. )
  174. if err = conn.Set(&memcache.Item{Key: key, Object: struct{}{}, Flags: memcache.FlagJSON, Expiration: d.flowKeyExpired}); err != nil {
  175. log.Error("AddUserPackCache d.mc.Set(%s) error(%v)", key, err)
  176. }
  177. conn.Close()
  178. return
  179. }
  180. // DeleteUserPackCache delete user pack cache
  181. func (d *Dao) DeleteUserFlowCache(c context.Context, keyStr string) (err error) {
  182. var (
  183. key = keyUserFlow(keyStr)
  184. conn = d.mc.Get(c)
  185. )
  186. defer conn.Close()
  187. if err = conn.Delete(key); err != nil {
  188. if err == memcache.ErrNotFound {
  189. err = nil
  190. return
  191. }
  192. log.Error("DeleteUserFlowCache MemchDB.Delete(%s) error(%v)", key, err)
  193. return
  194. }
  195. return
  196. }
  197. //UserFlowListCache unicom flow cache
  198. func (d *Dao) UserFlowListCache(c context.Context) (res map[string]*unicom.UnicomUserFlow, err error) {
  199. var (
  200. key = _userflowlistkey
  201. conn = d.mc.Get(c)
  202. r *memcache.Item
  203. )
  204. defer conn.Close()
  205. if r, err = conn.Get(key); err != nil {
  206. if err == memcache.ErrNotFound {
  207. err = nil
  208. return
  209. }
  210. log.Error("UserFlowListCache MemchDB.Get(%s) error(%v)", key, err)
  211. return
  212. }
  213. if err = conn.Scan(r, &res); err != nil {
  214. log.Error("r.Scan(%s) error(%v)", r.Value, err)
  215. }
  216. return
  217. }
  218. // AddUserFlowListCache add user pack cache
  219. func (d *Dao) AddUserFlowListCache(c context.Context, list map[string]*unicom.UnicomUserFlow) (err error) {
  220. var (
  221. key = _userflowlistkey
  222. conn = d.mc.Get(c)
  223. )
  224. if err = conn.Set(&memcache.Item{Key: key, Object: list, Flags: memcache.FlagJSON, Expiration: d.flowKeyExpired}); err != nil {
  225. log.Error("AddUserFlowListCache d.mc.Set(%s,%v) error(%v)", key, list, err)
  226. }
  227. conn.Close()
  228. return
  229. }