ranking_redis.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/interface/main/web/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _keyRkFmt = "r_v2_%d_%d_%d_%d"
  12. _keyRkIndexFmt = "ri_%d"
  13. _keyRkRegionFmt = "rc_%d_%d_%d"
  14. _keyRkRecommendFmt = "rr_%d"
  15. _keyRkTagFmt = "rt_%d_%d"
  16. _keyRegionCustom = "krc"
  17. _keyRegionCustomBak = _keyBakPrefix + _keyRegionCustom
  18. _keyBakPrefix = "b_"
  19. )
  20. func keyRkList(rid int16, rankType, day, arcType int) string {
  21. return fmt.Sprintf(_keyRkFmt, rid, rankType, day, arcType)
  22. }
  23. func keyRkListBak(rid int16, rankType, day, arcType int) string {
  24. return _keyBakPrefix + keyRkList(rid, rankType, day, arcType)
  25. }
  26. func keyRkIndex(day int) string {
  27. return fmt.Sprintf(_keyRkIndexFmt, day)
  28. }
  29. func keyRkIndexBak(day int) string {
  30. return _keyBakPrefix + keyRkIndex(day)
  31. }
  32. func keyRkRegionList(rid int16, day, original int) string {
  33. return fmt.Sprintf(_keyRkRegionFmt, rid, day, original)
  34. }
  35. func keyRkRegionListBak(rid int16, day, original int) string {
  36. return _keyBakPrefix + keyRkRegionList(rid, day, original)
  37. }
  38. func keyRkRecommendList(rid int16) string {
  39. return fmt.Sprintf(_keyRkRecommendFmt, rid)
  40. }
  41. func keyRkRecommendListBak(rid int16) string {
  42. return _keyBakPrefix + fmt.Sprintf(_keyRkRecommendFmt, rid)
  43. }
  44. func keyRkTagList(rid int16, tagID int64) string {
  45. return fmt.Sprintf(_keyRkTagFmt, rid, tagID)
  46. }
  47. func keyRkTagListBak(rid int16, tagID int64) string {
  48. return _keyBakPrefix + keyRkTagList(rid, tagID)
  49. }
  50. // RankingCache get rank list from cache.
  51. func (d *Dao) RankingCache(c context.Context, rid int16, rankType, day, arcType int) (data *model.RankData, err error) {
  52. key := keyRkList(rid, rankType, day, arcType)
  53. conn := d.redis.Get(c)
  54. defer conn.Close()
  55. data, err = d.rankingCache(conn, key)
  56. return
  57. }
  58. // RankingBakCache get rank list from bak cache.
  59. func (d *Dao) RankingBakCache(c context.Context, rid int16, rankType, day, arcType int) (data *model.RankData, err error) {
  60. d.cacheProm.Incr("ranking_remote_cache")
  61. key := keyRkListBak(rid, rankType, day, arcType)
  62. conn := d.redisBak.Get(c)
  63. defer conn.Close()
  64. data, err = d.rankingCache(conn, key)
  65. if data == nil || len(data.List) == 0 {
  66. log.Error("RankingBakCache(%s) is nil", key)
  67. }
  68. return
  69. }
  70. // RankingIndexCache get rank index from cache.
  71. func (d *Dao) RankingIndexCache(c context.Context, day int) (arcs []*model.IndexArchive, err error) {
  72. key := keyRkIndex(day)
  73. conn := d.redis.Get(c)
  74. defer conn.Close()
  75. arcs, err = d.rankingIndexCache(conn, key)
  76. return
  77. }
  78. // RankingIndexBakCache get rank index from bak cache.
  79. func (d *Dao) RankingIndexBakCache(c context.Context, day int) (arcs []*model.IndexArchive, err error) {
  80. d.cacheProm.Incr("ranking_index_remote_cache")
  81. key := keyRkIndexBak(day)
  82. conn := d.redisBak.Get(c)
  83. defer conn.Close()
  84. arcs, err = d.rankingIndexCache(conn, key)
  85. if len(arcs) == 0 {
  86. log.Error("RankingIndexBakCache(%s) is nil", key)
  87. }
  88. return
  89. }
  90. // RankingRegionCache get rank cate list from cache.
  91. func (d *Dao) RankingRegionCache(c context.Context, rid int16, day, original int) (arcs []*model.RegionArchive, err error) {
  92. key := keyRkRegionList(rid, day, original)
  93. conn := d.redis.Get(c)
  94. defer conn.Close()
  95. arcs, err = d.rankingRegionCache(conn, key)
  96. return
  97. }
  98. // RankingRegionBakCache get rank cate list from bak cache.
  99. func (d *Dao) RankingRegionBakCache(c context.Context, rid int16, day, original int) (arcs []*model.RegionArchive, err error) {
  100. d.cacheProm.Incr("ranking_region_remote_cache")
  101. key := keyRkRegionListBak(rid, day, original)
  102. conn := d.redisBak.Get(c)
  103. defer conn.Close()
  104. arcs, err = d.rankingRegionCache(conn, key)
  105. if len(arcs) == 0 {
  106. log.Error("RankingRegionBakCache(%s) is nil", key)
  107. }
  108. return
  109. }
  110. // RankingRecommendCache get rank recommend list from cache.
  111. func (d *Dao) RankingRecommendCache(c context.Context, rid int16) (arcs []*model.IndexArchive, err error) {
  112. key := keyRkRecommendList(rid)
  113. conn := d.redis.Get(c)
  114. defer conn.Close()
  115. arcs, err = d.rankingIndexCache(conn, key)
  116. return
  117. }
  118. // RankingRecommendBakCache get rank recommend list from bak cache.
  119. func (d *Dao) RankingRecommendBakCache(c context.Context, rid int16) (arcs []*model.IndexArchive, err error) {
  120. d.cacheProm.Incr("ranking_rec_remote_cache")
  121. key := keyRkRecommendListBak(rid)
  122. conn := d.redisBak.Get(c)
  123. defer conn.Close()
  124. arcs, err = d.rankingIndexCache(conn, key)
  125. if len(arcs) == 0 {
  126. log.Error("RankingRecommendBakCache(%s) is nil", key)
  127. }
  128. return
  129. }
  130. // RankingTagCache get ranking tag from cache.
  131. func (d *Dao) RankingTagCache(c context.Context, rid int16, tagID int64) (arcs []*model.TagArchive, err error) {
  132. key := keyRkTagList(rid, tagID)
  133. conn := d.redis.Get(c)
  134. defer conn.Close()
  135. arcs, err = d.rankingTagCache(conn, key)
  136. return
  137. }
  138. // RankingTagBakCache get ranking tag from bak cache.
  139. func (d *Dao) RankingTagBakCache(c context.Context, rid int16, tagID int64) (arcs []*model.TagArchive, err error) {
  140. d.cacheProm.Incr("ranking_tag_remote_cache")
  141. key := keyRkTagListBak(rid, tagID)
  142. conn := d.redisBak.Get(c)
  143. defer conn.Close()
  144. arcs, err = d.rankingTagCache(conn, key)
  145. if len(arcs) == 0 {
  146. log.Error("RankingTagBakCache(%s) is nil", key)
  147. }
  148. return
  149. }
  150. // RegionCustomCache get region custom data from cache
  151. func (d *Dao) RegionCustomCache(c context.Context) (res []*model.Custom, err error) {
  152. key := _keyRegionCustom
  153. conn := d.redis.Get(c)
  154. defer conn.Close()
  155. res, err = regionCustomCache(conn, key)
  156. return
  157. }
  158. // RegionCustomBakCache get region custom data from cache
  159. func (d *Dao) RegionCustomBakCache(c context.Context) (res []*model.Custom, err error) {
  160. key := _keyRegionCustomBak
  161. conn := d.redis.Get(c)
  162. defer conn.Close()
  163. res, err = regionCustomCache(conn, key)
  164. return
  165. }
  166. func regionCustomCache(conn redis.Conn, key string) (res []*model.Custom, err error) {
  167. var value []byte
  168. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  169. if err == redis.ErrNil {
  170. err = nil
  171. } else {
  172. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  173. }
  174. return
  175. }
  176. res = []*model.Custom{}
  177. if err = json.Unmarshal(value, &res); err != nil {
  178. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  179. }
  180. return
  181. }
  182. // SetRegionCustomCache set region custom data cache
  183. func (d *Dao) SetRegionCustomCache(c context.Context, data []*model.Custom) (err error) {
  184. key := _keyRegionCustom
  185. conn := d.redis.Get(c)
  186. defer conn.Close()
  187. if err = d.setRegionCustomCache(conn, key, d.redisRcExpire, data); err != nil {
  188. return
  189. }
  190. key = _keyRegionCustomBak
  191. connBak := d.redisBak.Get(c)
  192. err = d.setRegionCustomCache(connBak, key, d.redisRcBakExpire, data)
  193. connBak.Close()
  194. return
  195. }
  196. func (d *Dao) setRegionCustomCache(conn redis.Conn, key string, expire int32, data []*model.Custom) (err error) {
  197. var bs []byte
  198. if bs, err = json.Marshal(data); err != nil {
  199. log.Error("json.Marshal(%v) error (%v)", data, err)
  200. return
  201. }
  202. if err = conn.Send("SET", key, bs); err != nil {
  203. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  204. return
  205. }
  206. if err = conn.Send("EXPIRE", key, d.redisRkExpire); err != nil {
  207. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisRkExpire, err)
  208. return
  209. }
  210. if err = conn.Flush(); err != nil {
  211. log.Error("conn.Flush error(%v)", err)
  212. return
  213. }
  214. for i := 0; i < 2; i++ {
  215. if _, err = conn.Receive(); err != nil {
  216. log.Error("conn.Receive() error(%v)", err)
  217. return
  218. }
  219. }
  220. return
  221. }
  222. func (d *Dao) rankingCache(conn redis.Conn, key string) (arcs *model.RankData, err error) {
  223. var value []byte
  224. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  225. if err == redis.ErrNil {
  226. err = nil
  227. } else {
  228. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  229. }
  230. return
  231. }
  232. arcs = new(model.RankData)
  233. if err = json.Unmarshal(value, &arcs); err != nil {
  234. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  235. }
  236. return
  237. }
  238. func (d *Dao) rankingIndexCache(conn redis.Conn, key string) (arcs []*model.IndexArchive, err error) {
  239. var value []byte
  240. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  241. if err == redis.ErrNil {
  242. err = nil
  243. } else {
  244. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  245. }
  246. return
  247. }
  248. arcs = []*model.IndexArchive{}
  249. if err = json.Unmarshal(value, &arcs); err != nil {
  250. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  251. }
  252. return
  253. }
  254. func (d *Dao) rankingRegionCache(conn redis.Conn, key string) (arcs []*model.RegionArchive, err error) {
  255. var value []byte
  256. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  257. if err == redis.ErrNil {
  258. err = nil
  259. } else {
  260. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  261. }
  262. return
  263. }
  264. arcs = []*model.RegionArchive{}
  265. if err = json.Unmarshal(value, &arcs); err != nil {
  266. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  267. }
  268. return
  269. }
  270. func (d *Dao) rankingTagCache(conn redis.Conn, key string) (arcs []*model.TagArchive, err error) {
  271. var value []byte
  272. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  273. if err == redis.ErrNil {
  274. err = nil
  275. } else {
  276. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  277. }
  278. return
  279. }
  280. arcs = []*model.TagArchive{}
  281. if err = json.Unmarshal(value, &arcs); err != nil {
  282. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  283. }
  284. return
  285. }
  286. // SetRankingCache set ranking data to cache
  287. func (d *Dao) SetRankingCache(c context.Context, rid int16, rankType, day, arcType int, data *model.RankData) (err error) {
  288. key := keyRkList(rid, rankType, day, arcType)
  289. conn := d.redis.Get(c)
  290. defer conn.Close()
  291. if err = d.setRkCache(c, conn, key, d.redisRkExpire, data); err != nil {
  292. return
  293. }
  294. key = keyRkListBak(rid, rankType, day, arcType)
  295. connBak := d.redisBak.Get(c)
  296. err = d.setRkCache(c, connBak, key, d.redisRkBakExpire, data)
  297. connBak.Close()
  298. return
  299. }
  300. // SetRankingIndexCache set ranking index data to cache
  301. func (d *Dao) SetRankingIndexCache(c context.Context, day int, arcs []*model.IndexArchive) (err error) {
  302. key := keyRkIndex(day)
  303. conn := d.redis.Get(c)
  304. defer conn.Close()
  305. if err = d.setRkIndexCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
  306. return
  307. }
  308. key = keyRkIndexBak(day)
  309. connBak := d.redisBak.Get(c)
  310. err = d.setRkIndexCache(c, connBak, key, d.redisRkBakExpire, arcs)
  311. connBak.Close()
  312. return
  313. }
  314. // SetRankingRegionCache set ranking data to cache
  315. func (d *Dao) SetRankingRegionCache(c context.Context, rid int16, day, original int, arcs []*model.RegionArchive) (err error) {
  316. key := keyRkRegionList(rid, day, original)
  317. conn := d.redis.Get(c)
  318. defer conn.Close()
  319. if err = d.setRkRegionCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
  320. return
  321. }
  322. key = keyRkRegionListBak(rid, day, original)
  323. connBak := d.redisBak.Get(c)
  324. err = d.setRkRegionCache(c, connBak, key, d.redisRkBakExpire, arcs)
  325. connBak.Close()
  326. return
  327. }
  328. // SetRankingRecommendCache set ranking data to bak cache
  329. func (d *Dao) SetRankingRecommendCache(c context.Context, rid int16, arcs []*model.IndexArchive) (err error) {
  330. key := keyRkRecommendList(rid)
  331. conn := d.redis.Get(c)
  332. defer conn.Close()
  333. if err = d.setRkIndexCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
  334. return
  335. }
  336. key = keyRkRecommendListBak(rid)
  337. connBak := d.redisBak.Get(c)
  338. err = d.setRkIndexCache(c, connBak, key, d.redisRkBakExpire, arcs)
  339. connBak.Close()
  340. return
  341. }
  342. // SetRankingTagCache set ranking tag data to cache
  343. func (d *Dao) SetRankingTagCache(c context.Context, rid int16, tagID int64, arcs []*model.TagArchive) (err error) {
  344. key := keyRkTagList(rid, tagID)
  345. conn := d.redis.Get(c)
  346. defer conn.Close()
  347. if err = d.setRkTagCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
  348. return
  349. }
  350. key = keyRkTagListBak(rid, tagID)
  351. connBak := d.redisBak.Get(c)
  352. err = d.setRkTagCache(c, connBak, key, d.redisRkBakExpire, arcs)
  353. connBak.Close()
  354. return
  355. }
  356. func (d *Dao) setRkCache(c context.Context, conn redis.Conn, key string, expire int32, arcs *model.RankData) (err error) {
  357. var bs []byte
  358. if bs, err = json.Marshal(arcs); err != nil {
  359. log.Error("json.Marshal(%v) error (%v)", arcs, err)
  360. return
  361. }
  362. if err = conn.Send("SET", key, bs); err != nil {
  363. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  364. return
  365. }
  366. if err = conn.Send("EXPIRE", key, expire); err != nil {
  367. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
  368. return
  369. }
  370. if err = conn.Flush(); err != nil {
  371. log.Error("conn.Flush error(%v)", err)
  372. return
  373. }
  374. for i := 0; i < 2; i++ {
  375. if _, err = conn.Receive(); err != nil {
  376. log.Error("conn.Receive() error(%v)", err)
  377. return
  378. }
  379. }
  380. return
  381. }
  382. func (d *Dao) setRkIndexCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.IndexArchive) (err error) {
  383. var bs []byte
  384. if bs, err = json.Marshal(arcs); err != nil {
  385. log.Error("json.Marshal(%v) error (%v)", arcs, err)
  386. return
  387. }
  388. if err = conn.Send("SET", key, bs); err != nil {
  389. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  390. return
  391. }
  392. if err = conn.Send("EXPIRE", key, expire); err != nil {
  393. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
  394. return
  395. }
  396. if err = conn.Flush(); err != nil {
  397. log.Error("conn.Flush error(%v)", err)
  398. return
  399. }
  400. for i := 0; i < 2; i++ {
  401. if _, err = conn.Receive(); err != nil {
  402. log.Error("conn.Receive() error(%v)", err)
  403. return
  404. }
  405. }
  406. return
  407. }
  408. func (d *Dao) setRkRegionCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.RegionArchive) (err error) {
  409. var bs []byte
  410. if bs, err = json.Marshal(arcs); err != nil {
  411. log.Error("json.Marshal(%v) error (%v)", arcs, err)
  412. return
  413. }
  414. if err = conn.Send("SET", key, bs); err != nil {
  415. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  416. return
  417. }
  418. if err = conn.Send("EXPIRE", key, expire); err != nil {
  419. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
  420. return
  421. }
  422. if err = conn.Flush(); err != nil {
  423. log.Error("conn.Flush error(%v)", err)
  424. return
  425. }
  426. for i := 0; i < 2; i++ {
  427. if _, err = conn.Receive(); err != nil {
  428. log.Error("conn.Receive() error(%v)", err)
  429. return
  430. }
  431. }
  432. return
  433. }
  434. func (d *Dao) setRkTagCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.TagArchive) (err error) {
  435. var bs []byte
  436. if bs, err = json.Marshal(arcs); err != nil {
  437. log.Error("json.Marshal(%v) error (%v)", arcs, err)
  438. return
  439. }
  440. if err = conn.Send("SET", key, bs); err != nil {
  441. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  442. return
  443. }
  444. if err = conn.Send("EXPIRE", key, expire); err != nil {
  445. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
  446. return
  447. }
  448. if err = conn.Flush(); err != nil {
  449. log.Error("conn.Flush error(%v)", err)
  450. return
  451. }
  452. for i := 0; i < 2; i++ {
  453. if _, err = conn.Receive(); err != nil {
  454. log.Error("conn.Receive() error(%v)", err)
  455. return
  456. }
  457. }
  458. return
  459. }