overview_mc.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package data
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/creative/model/data"
  6. artmdl "go-common/app/interface/openplatform/article/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _basePrefix = "bse_"
  12. _areaPrefix = "are_"
  13. _trendPrefix = "tre_"
  14. _rfdPrefix = "rfd_"
  15. _rfmPrefix = "rfm_"
  16. _actPrefix = "act_"
  17. _incrPrefix = "incr_"
  18. _thirtyDayArcPrefix = "30arc_"
  19. _thirtyDayArtPrefix = "30art_"
  20. )
  21. func keyBase(mid int64, date string) string {
  22. return _basePrefix + date + "_" + strconv.FormatInt(mid, 10)
  23. }
  24. func keyArea(mid int64, date string) string {
  25. return _areaPrefix + date + "_" + strconv.FormatInt(mid, 10)
  26. }
  27. func keyTrend(mid int64, date string) string {
  28. return _trendPrefix + date + "_" + strconv.FormatInt(mid, 10)
  29. }
  30. func keyRfd(mid int64, date string) string {
  31. return _rfdPrefix + date + "_" + strconv.FormatInt(mid, 10)
  32. }
  33. func keyRfm(mid int64, date string) string {
  34. return _rfmPrefix + date + "_" + strconv.FormatInt(mid, 10)
  35. }
  36. func keyAct(mid int64, date string) string {
  37. return _actPrefix + date + "_" + strconv.FormatInt(mid, 10)
  38. }
  39. func keyViewIncr(mid int64, ty, date string) string {
  40. return _incrPrefix + ty + "_" + date + "_" + strconv.FormatInt(mid, 10)
  41. }
  42. func keyThirtyDayArchive(mid int64, ty string) string {
  43. return _thirtyDayArcPrefix + ty + "_" + strconv.FormatInt(mid, 10)
  44. }
  45. func keyThirtyDayArticle(mid int64) string {
  46. return _thirtyDayArtPrefix + "_" + strconv.FormatInt(mid, 10)
  47. }
  48. // ViewerBaseCache add ViewerBaseCache cache.
  49. func (d *Dao) ViewerBaseCache(c context.Context, mid int64, dt string) (res map[string]*data.ViewerBase, err error) {
  50. var (
  51. conn = d.mc.Get(c)
  52. r *memcache.Item
  53. )
  54. defer conn.Close()
  55. // get cache
  56. r, err = conn.Get(keyBase(mid, dt))
  57. if err != nil {
  58. if err == memcache.ErrNotFound {
  59. err = nil
  60. } else {
  61. log.Error("conn.Get(%d) error(%v)", mid, err)
  62. }
  63. return
  64. }
  65. if err = conn.Scan(r, &res); err != nil {
  66. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  67. res = nil
  68. }
  69. return
  70. }
  71. // AddViewerBaseCache add ViewerBaseCache cache update data by week.
  72. func (d *Dao) AddViewerBaseCache(c context.Context, mid int64, dt string, res map[string]*data.ViewerBase) (err error) {
  73. key := keyBase(mid, dt)
  74. conn := d.mc.Get(c)
  75. defer conn.Close()
  76. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  77. log.Error("memcache.Set(%v) error(%v)", key, err)
  78. }
  79. return
  80. }
  81. // ViewerAreaCache add ViewerArea cache.
  82. func (d *Dao) ViewerAreaCache(c context.Context, mid int64, dt string) (res map[string]map[string]int64, err error) {
  83. var (
  84. conn = d.mc.Get(c)
  85. r *memcache.Item
  86. )
  87. defer conn.Close()
  88. // get cache
  89. r, err = conn.Get(keyArea(mid, dt))
  90. if err != nil {
  91. if err == memcache.ErrNotFound {
  92. err = nil
  93. } else {
  94. log.Error("conn.Get(%d) error(%v)", mid, err)
  95. }
  96. return
  97. }
  98. if err = conn.Scan(r, &res); err != nil {
  99. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  100. res = nil
  101. }
  102. return
  103. }
  104. // AddViewerAreaCache add ViewerArea cache update data by week.
  105. func (d *Dao) AddViewerAreaCache(c context.Context, mid int64, dt string, res map[string]map[string]int64) (err error) {
  106. key := keyArea(mid, dt)
  107. conn := d.mc.Get(c)
  108. defer conn.Close()
  109. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  110. log.Error("memcache.Set(%v) error(%v)", key, err)
  111. }
  112. return
  113. }
  114. // TrendCache add trend cache.
  115. func (d *Dao) TrendCache(c context.Context, mid int64, dt string) (res map[string]*data.ViewerTrend, err error) {
  116. var (
  117. conn = d.mc.Get(c)
  118. r *memcache.Item
  119. )
  120. defer conn.Close()
  121. // get cache
  122. r, err = conn.Get(keyTrend(mid, dt))
  123. if err != nil {
  124. if err == memcache.ErrNotFound {
  125. err = nil
  126. } else {
  127. log.Error("conn.Get(%d) error(%v)", mid, err)
  128. }
  129. return
  130. }
  131. if err = conn.Scan(r, &res); err != nil {
  132. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  133. res = nil
  134. }
  135. return
  136. }
  137. // AddTrendCache add trend cache update data by week.
  138. func (d *Dao) AddTrendCache(c context.Context, mid int64, dt string, res map[string]*data.ViewerTrend) (err error) {
  139. key := keyTrend(mid, dt)
  140. conn := d.mc.Get(c)
  141. defer conn.Close()
  142. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  143. log.Error("memcache.Set(%v) error(%v)", key, err)
  144. }
  145. return
  146. }
  147. // RelationFansDayCache add relation day cache.
  148. func (d *Dao) RelationFansDayCache(c context.Context, mid int64, dt string) (res map[string]map[string]int, err error) {
  149. var (
  150. conn = d.mc.Get(c)
  151. r *memcache.Item
  152. )
  153. defer conn.Close()
  154. // get cache
  155. r, err = conn.Get(keyRfd(mid, dt))
  156. if err != nil {
  157. if err == memcache.ErrNotFound {
  158. err = nil
  159. } else {
  160. log.Error("conn.Get(%d) error(%v)", mid, err)
  161. }
  162. return
  163. }
  164. if err = conn.Scan(r, &res); err != nil {
  165. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  166. res = nil
  167. }
  168. return
  169. }
  170. // AddRelationFansDayCache add relation day cache update data by day.
  171. func (d *Dao) AddRelationFansDayCache(c context.Context, mid int64, dt string, res map[string]map[string]int) (err error) {
  172. key := keyRfd(mid, dt)
  173. conn := d.mc.Get(c)
  174. defer conn.Close()
  175. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  176. log.Error("memcache.Set(%v) error(%v)", key, err)
  177. }
  178. return
  179. }
  180. // RelationFansMonthCache add relation month cache.
  181. func (d *Dao) RelationFansMonthCache(c context.Context, mid int64, dt string) (res map[string]map[string]int, err error) {
  182. var (
  183. conn = d.mc.Get(c)
  184. r *memcache.Item
  185. )
  186. defer conn.Close()
  187. r, err = conn.Get(keyRfm(mid, dt))
  188. if err != nil {
  189. if err == memcache.ErrNotFound {
  190. err = nil
  191. } else {
  192. log.Error("conn.Get(%d) error(%v)", mid, err)
  193. }
  194. return
  195. }
  196. if err = conn.Scan(r, &res); err != nil {
  197. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  198. res = nil
  199. }
  200. return
  201. }
  202. // AddRelationFansMonthCache add relation month cache update data by day.
  203. func (d *Dao) AddRelationFansMonthCache(c context.Context, mid int64, dt string, res map[string]map[string]int) (err error) {
  204. key := keyRfm(mid, dt)
  205. conn := d.mc.Get(c)
  206. defer conn.Close()
  207. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  208. log.Error("memcache.Set(%v) error(%v)", key, err)
  209. }
  210. return
  211. }
  212. // ViewerActionHourCache add ActionHour cache.
  213. func (d *Dao) ViewerActionHourCache(c context.Context, mid int64, dt string) (res map[string]*data.ViewerActionHour, err error) {
  214. var (
  215. conn = d.mc.Get(c)
  216. r *memcache.Item
  217. )
  218. defer conn.Close()
  219. r, err = conn.Get(keyAct(mid, dt))
  220. if err != nil {
  221. if err == memcache.ErrNotFound {
  222. err = nil
  223. } else {
  224. log.Error("conn.Get(%d) error(%v)", mid, err)
  225. }
  226. return
  227. }
  228. if err = conn.Scan(r, &res); err != nil {
  229. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  230. res = nil
  231. }
  232. return
  233. }
  234. // AddViewerActionHourCache add ActionHour cache update data by week.
  235. func (d *Dao) AddViewerActionHourCache(c context.Context, mid int64, dt string, res map[string]*data.ViewerActionHour) (err error) {
  236. key := keyAct(mid, dt)
  237. conn := d.mc.Get(c)
  238. defer conn.Close()
  239. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  240. log.Error("memcache.Set(%v) error(%v)", key, err)
  241. }
  242. return
  243. }
  244. // ViewerIncrCache get ViewerIncr cache.
  245. func (d *Dao) ViewerIncrCache(c context.Context, mid int64, ty, dt string) (res *data.ViewerIncr, err error) {
  246. var (
  247. conn = d.mc.Get(c)
  248. r *memcache.Item
  249. )
  250. defer conn.Close()
  251. // get cache
  252. r, err = conn.Get(keyViewIncr(mid, ty, dt))
  253. if err != nil {
  254. if err == memcache.ErrNotFound {
  255. err = nil
  256. } else {
  257. log.Error("conn.Get(%d) error(%v)", mid, err)
  258. }
  259. return
  260. }
  261. if err = conn.Scan(r, &res); err != nil {
  262. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  263. res = nil
  264. }
  265. return
  266. }
  267. // AddViewerIncrCache add ViewerIncr cache update data by day.
  268. func (d *Dao) AddViewerIncrCache(c context.Context, mid int64, ty, dt string, res *data.ViewerIncr) (err error) {
  269. key := keyViewIncr(mid, ty, dt)
  270. conn := d.mc.Get(c)
  271. defer conn.Close()
  272. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  273. log.Error("memcache.Set(%v) error(%v)", key, err)
  274. }
  275. return
  276. }
  277. // ThirtyDayArchiveCache get archive 30 days cache.
  278. func (d *Dao) ThirtyDayArchiveCache(c context.Context, mid int64, ty string) (res []*data.ThirtyDay, err error) {
  279. var (
  280. conn = d.mc.Get(c)
  281. r *memcache.Item
  282. )
  283. defer conn.Close()
  284. // get cache
  285. r, err = conn.Get(keyThirtyDayArchive(mid, ty))
  286. if err != nil {
  287. if err == memcache.ErrNotFound {
  288. err = nil
  289. } else {
  290. log.Error("conn.Get(%d) error(%v)", mid, err)
  291. }
  292. return
  293. }
  294. if err = conn.Scan(r, &res); err != nil {
  295. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  296. res = nil
  297. }
  298. return
  299. }
  300. // AddThirtyDayArchiveCache add archive 30 days cache update data by day.
  301. func (d *Dao) AddThirtyDayArchiveCache(c context.Context, mid int64, ty string, res []*data.ThirtyDay) (err error) {
  302. key := keyThirtyDayArchive(mid, ty)
  303. conn := d.mc.Get(c)
  304. defer conn.Close()
  305. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  306. log.Error("memcache.Set(%v) error(%v)", key, err)
  307. }
  308. return
  309. }
  310. // ThirtyDayArticleCache get article 30 days cache.
  311. func (d *Dao) ThirtyDayArticleCache(c context.Context, mid int64) (res []*artmdl.ThirtyDayArticle, err error) {
  312. var (
  313. conn = d.mc.Get(c)
  314. r *memcache.Item
  315. )
  316. defer conn.Close()
  317. // get cache
  318. r, err = conn.Get(keyThirtyDayArticle(mid))
  319. if err != nil {
  320. if err == memcache.ErrNotFound {
  321. err = nil
  322. } else {
  323. log.Error("conn.Get(%d) error(%v)", mid, err)
  324. }
  325. return
  326. }
  327. if err = conn.Scan(r, &res); err != nil {
  328. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  329. res = nil
  330. }
  331. return
  332. }
  333. // AddThirtyDayArticleCache add article 30 days cache update data by day.
  334. func (d *Dao) AddThirtyDayArticleCache(c context.Context, mid int64, res []*artmdl.ThirtyDayArticle) (err error) {
  335. key := keyThirtyDayArticle(mid)
  336. conn := d.mc.Get(c)
  337. defer conn.Close()
  338. if err = conn.Set(&memcache.Item{Key: key, Object: res, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  339. log.Error("memcache.Set(%v) error(%v)", key, err)
  340. }
  341. return
  342. }