redis.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/interface/main/esports/model"
  7. arcmdl "go-common/app/service/main/archive/api"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _keyCont = "c_%d"
  13. _keyVideo = "v_%d"
  14. _keyCale = "c_%s_%s"
  15. _keyFMat = "fm"
  16. _keyFVideo = "fv"
  17. _keySeason = "se"
  18. _keySeasonM = "sp"
  19. _keyC = "co_%d"
  20. _keyCF = "cf_%d"
  21. _keyCAF = "caf_%d"
  22. _keyS = "s_%d"
  23. _keyAct = "act_%d"
  24. _keyModule = "module_%d"
  25. _keyTop = "top_%d_%d"
  26. _keyPoint = "point_%d_%d_%d"
  27. _keyKnock = "knock_%d"
  28. _keyMAct = "ma_%d"
  29. _keyTeam = "team_%d"
  30. _keyCSData = "c_s_data_%d"
  31. _keyCRecent = "c_recent_%d_%d_%d_%d"
  32. )
  33. func keyCale(stime, etime string) string {
  34. return fmt.Sprintf(_keyCale, stime, etime)
  35. }
  36. func keyCont(ps int) string {
  37. return fmt.Sprintf(_keyCont, ps)
  38. }
  39. func keyVideo(ps int) string {
  40. return fmt.Sprintf(_keyVideo, ps)
  41. }
  42. func keyContID(cid int64) string {
  43. return fmt.Sprintf(_keyC, cid)
  44. }
  45. func keyCoFav(mid int64) string {
  46. return fmt.Sprintf(_keyCF, mid)
  47. }
  48. func keyCoAppFav(mid int64) string {
  49. return fmt.Sprintf(_keyCAF, mid)
  50. }
  51. func keySID(sid int64) string {
  52. return fmt.Sprintf(_keyS, sid)
  53. }
  54. func keyTeamID(tid int64) string {
  55. return fmt.Sprintf(_keyTeam, tid)
  56. }
  57. func keyMatchAct(aid int64) string {
  58. return fmt.Sprintf(_keyAct, aid)
  59. }
  60. func keyCSData(cid int64) string {
  61. return fmt.Sprintf(_keyCSData, cid)
  62. }
  63. func keyCRecent(param *model.ParamCDRecent) string {
  64. key := fmt.Sprintf(_keyCRecent, param.CID, param.HomeID, param.AwayID, param.Ps)
  65. return key
  66. }
  67. func keyMatchModule(mmid int64) string {
  68. return fmt.Sprintf(_keyModule, mmid)
  69. }
  70. func keyKnock(mdID int64) string {
  71. return fmt.Sprintf(_keyKnock, mdID)
  72. }
  73. func keyTop(aid, ps int64) string {
  74. return fmt.Sprintf(_keyTop, aid, ps)
  75. }
  76. func keyPoint(aid, mdID, ps int64) string {
  77. return fmt.Sprintf(_keyPoint, aid, mdID, ps)
  78. }
  79. func keyMAct(aid int64) string {
  80. return fmt.Sprintf(_keyMAct, aid)
  81. }
  82. // FMatCache get filter match from cache.
  83. func (d *Dao) FMatCache(c context.Context) (res map[string][]*model.Filter, err error) {
  84. res, err = d.filterCache(c, _keyFMat)
  85. return
  86. }
  87. // FVideoCache get filter video from cache.
  88. func (d *Dao) FVideoCache(c context.Context) (res map[string][]*model.Filter, err error) {
  89. res, err = d.filterCache(c, _keyFVideo)
  90. return
  91. }
  92. func (d *Dao) filterCache(c context.Context, key string) (rs map[string][]*model.Filter, err error) {
  93. conn := d.redis.Get(c)
  94. defer conn.Close()
  95. var values []byte
  96. if values, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  97. if err == redis.ErrNil {
  98. err = nil
  99. log.Error("filterCache (%s) return nil ", key)
  100. } else {
  101. log.Error("conn.Do(GET,%s) error(%v)", key, err)
  102. }
  103. return
  104. }
  105. rs = make(map[string][]*model.Filter)
  106. if err = json.Unmarshal(values, &rs); err != nil {
  107. log.Error("json.Unmarshal(%v) error(%v)", values, err)
  108. }
  109. return
  110. }
  111. // SetFMatCache set filter match to cache.
  112. func (d *Dao) SetFMatCache(c context.Context, fs map[string][]*model.Filter) (err error) {
  113. err = d.setFilterCache(c, _keyFMat, fs)
  114. return
  115. }
  116. // SetFVideoCache set filter match to cache.
  117. func (d *Dao) SetFVideoCache(c context.Context, fs map[string][]*model.Filter) (err error) {
  118. err = d.setFilterCache(c, _keyFVideo, fs)
  119. return
  120. }
  121. func (d *Dao) setFilterCache(c context.Context, key string, fs map[string][]*model.Filter) (err error) {
  122. conn := d.redis.Get(c)
  123. defer conn.Close()
  124. var bs []byte
  125. if bs, err = json.Marshal(fs); err != nil {
  126. log.Error("json.Marshal(%v) error(%v)", fs, err)
  127. return
  128. }
  129. if err = conn.Send("SET", key, bs); err != nil {
  130. log.Error("conn.Send(SET,%s,%s) error(%v)", key, string(bs), err)
  131. return
  132. }
  133. if err = conn.Send("EXPIRE", key, d.filterExpire); err != nil {
  134. log.Error("conn.Send(EXPIRE,%s,%d) error(%v)", key, d.filterExpire, err)
  135. return
  136. }
  137. if err = conn.Flush(); err != nil {
  138. log.Error("conn.Flush error(%v)", err)
  139. return
  140. }
  141. for i := 0; i < 2; i++ {
  142. if _, err = conn.Receive(); err != nil {
  143. log.Error("conn.Recevie(%d) error(%v0", i, err)
  144. }
  145. }
  146. return
  147. }
  148. // ContestCache get all contest from cache.
  149. func (d *Dao) ContestCache(c context.Context, ps int) (res []*model.Contest, total int, err error) {
  150. key := keyCont(ps)
  151. res, total, err = d.cosCache(c, key)
  152. return
  153. }
  154. // FavCoCache get fav contest from cache.
  155. func (d *Dao) FavCoCache(c context.Context, mid int64) (res []*model.Contest, total int, err error) {
  156. key := keyCoFav(mid)
  157. res, total, err = d.cosCache(c, key)
  158. return
  159. }
  160. // FavCoAppCache get fav contest from cache.
  161. func (d *Dao) FavCoAppCache(c context.Context, mid int64) (res []*model.Contest, total int, err error) {
  162. key := keyCoAppFav(mid)
  163. res, total, err = d.cosCache(c, key)
  164. return
  165. }
  166. func (d *Dao) cosCache(c context.Context, key string) (res []*model.Contest, total int, err error) {
  167. conn := d.redis.Get(c)
  168. defer conn.Close()
  169. values, err := redis.Values(conn.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
  170. if err != nil {
  171. log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
  172. return
  173. }
  174. if len(values) == 0 {
  175. return
  176. }
  177. var num int64
  178. for len(values) > 0 {
  179. bs := []byte{}
  180. if values, err = redis.Scan(values, &bs, &num); err != nil {
  181. log.Error("redis.Scan(%v) error(%v)", values, err)
  182. return
  183. }
  184. cont := &model.Contest{}
  185. if err = json.Unmarshal(bs, cont); err != nil {
  186. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  187. return
  188. }
  189. res = append(res, cont)
  190. }
  191. total = from(num)
  192. return
  193. }
  194. // SetContestCache set all contest to cache.
  195. func (d *Dao) SetContestCache(c context.Context, ps int, contests []*model.Contest, total int) (err error) {
  196. key := keyCont(ps)
  197. err = d.setCosCache(c, key, contests, total)
  198. return
  199. }
  200. // SetFavCoCache set fav contest to cache.
  201. func (d *Dao) SetFavCoCache(c context.Context, mid int64, contests []*model.Contest, total int) (err error) {
  202. key := keyCoFav(mid)
  203. err = d.setCosCache(c, key, contests, total)
  204. return
  205. }
  206. // SetAppFavCoCache set fav contest to cache.
  207. func (d *Dao) SetAppFavCoCache(c context.Context, mid int64, contests []*model.Contest, total int) (err error) {
  208. key := keyCoAppFav(mid)
  209. err = d.setCosCache(c, key, contests, total)
  210. return
  211. }
  212. // DelFavCoCache delete fav contests cache.
  213. func (d *Dao) DelFavCoCache(c context.Context, mid int64) (err error) {
  214. key := keyCoFav(mid)
  215. keyApp := keyCoAppFav(mid)
  216. conn := d.redis.Get(c)
  217. defer conn.Close()
  218. if err = conn.Send("DEL", key); err != nil {
  219. log.Error("conn.Send(DEL plaKey(%s) error(%v))", key, err)
  220. return
  221. }
  222. if err = conn.Send("DEL", keyApp); err != nil {
  223. log.Error("conn.Send(DEL pladKey(%s) error(%v))", keyApp, err)
  224. return
  225. }
  226. if err = conn.Flush(); err != nil {
  227. log.Error("conn.Flush() error(%v)", err)
  228. return
  229. }
  230. for i := 0; i < 2; i++ {
  231. if _, err = conn.Receive(); err != nil {
  232. log.Error("conn.Receive() error(%v)", err)
  233. return
  234. }
  235. }
  236. return
  237. }
  238. func (d *Dao) setCosCache(c context.Context, key string, contests []*model.Contest, total int) (err error) {
  239. conn := d.redis.Get(c)
  240. defer conn.Close()
  241. count := 0
  242. if err = conn.Send("DEL", key); err != nil {
  243. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  244. return
  245. }
  246. count++
  247. args := redis.Args{}.Add(key)
  248. for sort, contest := range contests {
  249. bs, _ := json.Marshal(contest)
  250. args = args.Add(combine(int64(sort), total)).Add(bs)
  251. }
  252. if err = conn.Send("ZADD", args...); err != nil {
  253. log.Error("conn.Send(ZADD, %s, %v) error(%v)", key, args, err)
  254. return
  255. }
  256. count++
  257. if err = conn.Send("EXPIRE", key, d.filterExpire); err != nil {
  258. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.filterExpire, err)
  259. return
  260. }
  261. count++
  262. if err = conn.Flush(); err != nil {
  263. log.Error("conn.Flush error(%v)", err)
  264. return
  265. }
  266. for i := 0; i < count; i++ {
  267. if _, err = conn.Receive(); err != nil {
  268. log.Error("conn.Receive() error(%v)", err)
  269. return
  270. }
  271. }
  272. return
  273. }
  274. // CalendarCache get all calendar from cache.
  275. func (d *Dao) CalendarCache(c context.Context, p *model.ParamFilter) (res []*model.Calendar, err error) {
  276. var (
  277. key = keyCale(p.Stime, p.Etime)
  278. conn = d.redis.Get(c)
  279. )
  280. defer conn.Close()
  281. values, err := redis.Values(conn.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
  282. if err != nil {
  283. log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
  284. return
  285. }
  286. if len(values) == 0 {
  287. return
  288. }
  289. var num int64
  290. for len(values) > 0 {
  291. bs := []byte{}
  292. if values, err = redis.Scan(values, &bs, &num); err != nil {
  293. log.Error("redis.Scan(%v) error(%v)", values, err)
  294. return
  295. }
  296. cale := &model.Calendar{}
  297. if err = json.Unmarshal(bs, cale); err != nil {
  298. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  299. return
  300. }
  301. res = append(res, cale)
  302. }
  303. return
  304. }
  305. // SetCalendarCache set all calendar to cache.
  306. func (d *Dao) SetCalendarCache(c context.Context, p *model.ParamFilter, cales []*model.Calendar) (err error) {
  307. var (
  308. key = keyCale(p.Stime, p.Etime)
  309. conn = d.redis.Get(c)
  310. )
  311. defer conn.Close()
  312. count := 0
  313. if err = conn.Send("DEL", key); err != nil {
  314. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  315. return
  316. }
  317. count++
  318. args := redis.Args{}.Add(key)
  319. for sort, cale := range cales {
  320. bs, _ := json.Marshal(cale)
  321. args = args.Add(sort).Add(bs)
  322. }
  323. if err = conn.Send("ZADD", args...); err != nil {
  324. log.Error("conn.Send(ZADD, %s, %v) error(%v)", key, args, err)
  325. return
  326. }
  327. count++
  328. if err = conn.Send("EXPIRE", key, d.filterExpire); err != nil {
  329. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.filterExpire, err)
  330. return
  331. }
  332. count++
  333. if err = conn.Flush(); err != nil {
  334. log.Error("conn.Flush error(%v)", err)
  335. return
  336. }
  337. for i := 0; i < count; i++ {
  338. if _, err = conn.Receive(); err != nil {
  339. log.Error("conn.Receive() error(%v)", err)
  340. return
  341. }
  342. }
  343. return
  344. }
  345. // VideoCache get all video from cache.
  346. func (d *Dao) VideoCache(c context.Context, ps int) (res []*arcmdl.Arc, total int, err error) {
  347. var (
  348. key = keyVideo(ps)
  349. conn = d.redis.Get(c)
  350. )
  351. defer conn.Close()
  352. values, err := redis.Values(conn.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
  353. if err != nil {
  354. log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
  355. return
  356. }
  357. if len(values) == 0 {
  358. return
  359. }
  360. var num int64
  361. for len(values) > 0 {
  362. bs := []byte{}
  363. if values, err = redis.Scan(values, &bs, &num); err != nil {
  364. log.Error("redis.Scan(%v) error(%v)", values, err)
  365. return
  366. }
  367. cont := &arcmdl.Arc{}
  368. if err = json.Unmarshal(bs, cont); err != nil {
  369. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  370. return
  371. }
  372. res = append(res, cont)
  373. }
  374. total = from(num)
  375. return
  376. }
  377. // SetVideoCache set all contest to cache.
  378. func (d *Dao) SetVideoCache(c context.Context, ps int, videos []*arcmdl.Arc, total int) (err error) {
  379. var (
  380. key = keyVideo(ps)
  381. conn = d.redis.Get(c)
  382. )
  383. defer conn.Close()
  384. count := 0
  385. if err = conn.Send("DEL", key); err != nil {
  386. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  387. return
  388. }
  389. count++
  390. args := redis.Args{}.Add(key)
  391. for sort, video := range videos {
  392. bs, _ := json.Marshal(video)
  393. args = args.Add(combine(int64(sort), total)).Add(bs)
  394. }
  395. if err = conn.Send("ZADD", args...); err != nil {
  396. log.Error("conn.Send(ZADD, %s, %v) error(%v)", key, args, err)
  397. return
  398. }
  399. count++
  400. if err = conn.Send("EXPIRE", key, d.filterExpire); err != nil {
  401. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.filterExpire, err)
  402. return
  403. }
  404. count++
  405. if err = conn.Flush(); err != nil {
  406. log.Error("conn.Flush error(%v)", err)
  407. return
  408. }
  409. for i := 0; i < count; i++ {
  410. if _, err = conn.Receive(); err != nil {
  411. log.Error("conn.Receive() error(%v)", err)
  412. return
  413. }
  414. }
  415. return
  416. }
  417. func (d *Dao) seasonsCache(c context.Context, key string, start, end int) (res []*model.Season, total int, err error) {
  418. conn := d.redis.Get(c)
  419. defer conn.Close()
  420. values, err := redis.Values(conn.Do("ZRANGE", key, start, end, "WITHSCORES"))
  421. if err != nil {
  422. log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
  423. return
  424. }
  425. if len(values) == 0 {
  426. return
  427. }
  428. var num int64
  429. for len(values) > 0 {
  430. bs := []byte{}
  431. if values, err = redis.Scan(values, &bs, &num); err != nil {
  432. log.Error("redis.Scan(%v) error(%v)", values, err)
  433. return
  434. }
  435. object := &model.Season{}
  436. if err = json.Unmarshal(bs, object); err != nil {
  437. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  438. return
  439. }
  440. res = append(res, object)
  441. }
  442. total = from(num)
  443. return
  444. }
  445. func (d *Dao) setSeasonsCache(c context.Context, key string, seasons []*model.Season, total int) (err error) {
  446. conn := d.redis.Get(c)
  447. defer conn.Close()
  448. count := 0
  449. if err = conn.Send("DEL", key); err != nil {
  450. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  451. return
  452. }
  453. count++
  454. for sort, season := range seasons {
  455. bs, _ := json.Marshal(season)
  456. if err = conn.Send("ZADD", key, combine(int64(sort), total), bs); err != nil {
  457. log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
  458. return
  459. }
  460. count++
  461. }
  462. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  463. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.listExpire, err)
  464. return
  465. }
  466. count++
  467. if err = conn.Flush(); err != nil {
  468. log.Error("conn.Flush error(%v)", err)
  469. return
  470. }
  471. for i := 0; i < count; i++ {
  472. if _, err = conn.Receive(); err != nil {
  473. log.Error("conn.Receive() error(%v)", err)
  474. return
  475. }
  476. }
  477. return
  478. }
  479. // SeasonCache get season list from cache.
  480. func (d *Dao) SeasonCache(c context.Context, start, end int) (res []*model.Season, total int, err error) {
  481. res, total, err = d.seasonsCache(c, _keySeason, start, end)
  482. return
  483. }
  484. // SetSeasonCache set season list cache.
  485. func (d *Dao) SetSeasonCache(c context.Context, seasons []*model.Season, total int) (err error) {
  486. err = d.setSeasonsCache(c, _keySeason, seasons, total)
  487. return
  488. }
  489. // SeasonMCache get season list from cache.
  490. func (d *Dao) SeasonMCache(c context.Context, start, end int) (res []*model.Season, total int, err error) {
  491. res, total, err = d.seasonsCache(c, _keySeasonM, start, end)
  492. return
  493. }
  494. // SetSeasonMCache set season list cache.
  495. func (d *Dao) SetSeasonMCache(c context.Context, seasons []*model.Season, total int) (err error) {
  496. err = d.setSeasonsCache(c, _keySeasonM, seasons, total)
  497. return
  498. }
  499. func from(i int64) int {
  500. return int(i & 0xffff)
  501. }
  502. func combine(sort int64, count int) int64 {
  503. return sort<<16 | int64(count)
  504. }
  505. // CacheEpContests .
  506. func (d *Dao) CacheEpContests(c context.Context, ids []int64) (res map[int64]*model.Contest, err error) {
  507. var (
  508. key string
  509. args = redis.Args{}
  510. bss [][]byte
  511. )
  512. for _, cid := range ids {
  513. key = keyContID(cid)
  514. args = args.Add(key)
  515. }
  516. conn := d.redis.Get(c)
  517. defer conn.Close()
  518. if bss, err = redis.ByteSlices(conn.Do("MGET", args...)); err != nil {
  519. if err == redis.ErrNil {
  520. err = nil
  521. } else {
  522. log.Error("CacheEpContests conn.Do(MGET,%s) error(%v)", key, err)
  523. }
  524. return
  525. }
  526. res = make(map[int64]*model.Contest, len(ids))
  527. for _, bs := range bss {
  528. con := new(model.Contest)
  529. if bs == nil {
  530. continue
  531. }
  532. if err = json.Unmarshal(bs, con); err != nil {
  533. log.Error("CacheEpContests json.Unmarshal(%s) error(%v)", string(bs), err)
  534. err = nil
  535. continue
  536. }
  537. res[con.ID] = con
  538. }
  539. return
  540. }
  541. // AddCacheEpContests .
  542. func (d *Dao) AddCacheEpContests(c context.Context, data map[int64]*model.Contest) (err error) {
  543. if len(data) == 0 {
  544. return
  545. }
  546. var (
  547. bs []byte
  548. keyID string
  549. keyIDs []string
  550. argsCid = redis.Args{}
  551. )
  552. conn := d.redis.Get(c)
  553. defer conn.Close()
  554. for _, v := range data {
  555. if bs, err = json.Marshal(v); err != nil {
  556. log.Error("json.Marshal err(%v)", err)
  557. continue
  558. }
  559. keyID = keyContID(v.ID)
  560. keyIDs = append(keyIDs, keyID)
  561. argsCid = argsCid.Add(keyID).Add(string(bs))
  562. }
  563. if err = conn.Send("MSET", argsCid...); err != nil {
  564. log.Error("AddCacheMatchSubjects conn.Send(MSET) error(%v)", err)
  565. return
  566. }
  567. count := 1
  568. for _, v := range keyIDs {
  569. count++
  570. if err = conn.Send("EXPIRE", v, d.listExpire); err != nil {
  571. log.Error("AddCacheMatchSubjects conn.Send(Expire, %s, %d) error(%v)", v, d.listExpire, err)
  572. return
  573. }
  574. }
  575. if err = conn.Flush(); err != nil {
  576. log.Error("conn.Flush error(%v)", err)
  577. return
  578. }
  579. for i := 0; i < count; i++ {
  580. if _, err = conn.Receive(); err != nil {
  581. log.Error("conn.Receive() error(%v)", err)
  582. return
  583. }
  584. }
  585. return
  586. }
  587. // CacheEpSeasons .
  588. func (d *Dao) CacheEpSeasons(c context.Context, ids []int64) (res map[int64]*model.Season, err error) {
  589. var (
  590. key string
  591. args = redis.Args{}
  592. bss [][]byte
  593. )
  594. for _, sid := range ids {
  595. key = keySID(sid)
  596. args = args.Add(key)
  597. }
  598. conn := d.redis.Get(c)
  599. defer conn.Close()
  600. if bss, err = redis.ByteSlices(conn.Do("MGET", args...)); err != nil {
  601. if err == redis.ErrNil {
  602. err = nil
  603. } else {
  604. log.Error("CacheEpSeasons conn.Do(MGET,%s) error(%v)", key, err)
  605. }
  606. return
  607. }
  608. res = make(map[int64]*model.Season, len(ids))
  609. for _, bs := range bss {
  610. sea := new(model.Season)
  611. if bs == nil {
  612. continue
  613. }
  614. if err = json.Unmarshal(bs, sea); err != nil {
  615. log.Error("CacheEpSeasons json.Unmarshal(%s) error(%v)", string(bs), err)
  616. err = nil
  617. continue
  618. }
  619. res[sea.ID] = sea
  620. }
  621. return
  622. }
  623. // CacheEpTeams .
  624. func (d *Dao) CacheEpTeams(c context.Context, ids []int64) (res map[int64]*model.Team, err error) {
  625. var (
  626. key string
  627. args = redis.Args{}
  628. bss [][]byte
  629. )
  630. for _, tid := range ids {
  631. key = keyTeamID(tid)
  632. args = args.Add(key)
  633. }
  634. conn := d.redis.Get(c)
  635. defer conn.Close()
  636. if bss, err = redis.ByteSlices(conn.Do("MGET", args...)); err != nil {
  637. if err == redis.ErrNil {
  638. err = nil
  639. } else {
  640. log.Error("CacheEpTeams conn.Do(MGET,%s) error(%v)", key, err)
  641. }
  642. return
  643. }
  644. res = make(map[int64]*model.Team, len(ids))
  645. for _, bs := range bss {
  646. team := new(model.Team)
  647. if bs == nil {
  648. continue
  649. }
  650. if err = json.Unmarshal(bs, team); err != nil {
  651. log.Error("CacheEpTeams json.Unmarshal(%s) error(%v)", string(bs), err)
  652. err = nil
  653. continue
  654. }
  655. res[team.ID] = team
  656. }
  657. return
  658. }
  659. // AddCacheEpTeams .
  660. func (d *Dao) AddCacheEpTeams(c context.Context, data map[int64]*model.Team) (err error) {
  661. if len(data) == 0 {
  662. return
  663. }
  664. var (
  665. bs []byte
  666. keyID string
  667. keyIDs []string
  668. argsCid = redis.Args{}
  669. )
  670. conn := d.redis.Get(c)
  671. defer conn.Close()
  672. for _, v := range data {
  673. if bs, err = json.Marshal(v); err != nil {
  674. log.Error("AddCacheEpTeams.json.Marshal err(%v)", err)
  675. continue
  676. }
  677. keyID = keyTeamID(v.ID)
  678. keyIDs = append(keyIDs, keyID)
  679. argsCid = argsCid.Add(keyID).Add(string(bs))
  680. }
  681. if err = conn.Send("MSET", argsCid...); err != nil {
  682. log.Error("AddCacheEpTeams conn.Send(MSET) error(%v)", err)
  683. return
  684. }
  685. count := 1
  686. for _, v := range keyIDs {
  687. count++
  688. if err = conn.Send("EXPIRE", v, d.listExpire); err != nil {
  689. log.Error("AddCacheEpTeams conn.Send(Expire, %s, %d) error(%v)", v, d.listExpire, err)
  690. return
  691. }
  692. }
  693. if err = conn.Flush(); err != nil {
  694. log.Error("conn.Flush error(%v)", err)
  695. return
  696. }
  697. for i := 0; i < count; i++ {
  698. if _, err = conn.Receive(); err != nil {
  699. log.Error("conn.Receive() error(%v)", err)
  700. return
  701. }
  702. }
  703. return
  704. }
  705. // AddCacheEpSeasons .
  706. func (d *Dao) AddCacheEpSeasons(c context.Context, data map[int64]*model.Season) (err error) {
  707. if len(data) == 0 {
  708. return
  709. }
  710. var (
  711. bs []byte
  712. keyID string
  713. keyIDs []string
  714. argsCid = redis.Args{}
  715. )
  716. conn := d.redis.Get(c)
  717. defer conn.Close()
  718. for _, v := range data {
  719. if bs, err = json.Marshal(v); err != nil {
  720. log.Error("json.Marshal err(%v)", err)
  721. continue
  722. }
  723. keyID = keySID(v.ID)
  724. keyIDs = append(keyIDs, keyID)
  725. argsCid = argsCid.Add(keyID).Add(string(bs))
  726. }
  727. if err = conn.Send("MSET", argsCid...); err != nil {
  728. log.Error("AddCacheEpSeasons conn.Send(MSET) error(%v)", err)
  729. return
  730. }
  731. count := 1
  732. for _, v := range keyIDs {
  733. count++
  734. if err = conn.Send("EXPIRE", v, d.listExpire); err != nil {
  735. log.Error("AddCacheEpSeasons conn.Send(Expire, %s, %d) error(%v)", v, d.listExpire, err)
  736. return
  737. }
  738. }
  739. if err = conn.Flush(); err != nil {
  740. log.Error("conn.Flush error(%v)", err)
  741. return
  742. }
  743. for i := 0; i < count; i++ {
  744. if _, err = conn.Receive(); err != nil {
  745. log.Error("conn.Receive() error(%v)", err)
  746. return
  747. }
  748. }
  749. return
  750. }
  751. // GetActPageCache get act from cache.
  752. func (d *Dao) GetActPageCache(c context.Context, id int64) (act *model.ActivePage, err error) {
  753. var (
  754. bs []byte
  755. key = keyMatchAct(id)
  756. conn = d.redis.Get(c)
  757. )
  758. defer conn.Close()
  759. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  760. if err == redis.ErrNil {
  761. err = nil
  762. act = nil
  763. } else {
  764. log.Error("conn.Do(GET,%s) error(%v)", key, err)
  765. }
  766. return
  767. }
  768. act = new(model.ActivePage)
  769. if err = json.Unmarshal(bs, act); err != nil {
  770. log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
  771. }
  772. return
  773. }
  774. // GetCSingleData get contest single data
  775. func (d *Dao) GetCSingleData(c context.Context, id int64) (data *model.ContestDataPage, err error) {
  776. var (
  777. bs []byte
  778. key = keyCSData(id)
  779. conn = d.redis.Get(c)
  780. )
  781. defer conn.Close()
  782. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  783. if err == redis.ErrNil {
  784. err = nil
  785. data = nil
  786. } else {
  787. log.Error("GetCSingleData conn.Do(GET,%s) error(%v)", key, err)
  788. }
  789. return
  790. }
  791. data = new(model.ContestDataPage)
  792. if err = json.Unmarshal(bs, data); err != nil {
  793. log.Error("GetCSingleData json.Unmarshal(%s) error(%v)", string(bs), err)
  794. }
  795. return
  796. }
  797. // AddCSingleData add act first page value
  798. func (d *Dao) AddCSingleData(c context.Context, id int64, act *model.ContestDataPage) (err error) {
  799. var (
  800. bs []byte
  801. key = keyCSData(id)
  802. conn = d.redis.Get(c)
  803. )
  804. defer conn.Close()
  805. if bs, err = json.Marshal(act); err != nil {
  806. log.Error("AddCSingleData json.Marshal() error(%v)", err)
  807. return
  808. }
  809. if err = conn.Send("SET", key, bs); err != nil {
  810. log.Error("AddCSingleData conn.Send(SET,%s,%d) error(%v)", key, id, err)
  811. return
  812. }
  813. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  814. log.Error("AddCSingleData conn.Send(EXPIRE,%s) error(%v)", key, err)
  815. return
  816. }
  817. if err = conn.Flush(); err != nil {
  818. log.Error("add conn.Flush error(%v)", err)
  819. return
  820. }
  821. for i := 0; i < 2; i++ {
  822. if _, err = conn.Receive(); err != nil {
  823. log.Error("add conn.Receive()%d error(%v)", i+1, err)
  824. return
  825. }
  826. }
  827. return
  828. }
  829. // GetCRecent get contest recent data
  830. func (d *Dao) GetCRecent(c context.Context, param *model.ParamCDRecent) (data []*model.Contest, err error) {
  831. var (
  832. bs []byte
  833. key = keyCRecent(param)
  834. conn = d.redis.Get(c)
  835. )
  836. defer conn.Close()
  837. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  838. if err == redis.ErrNil {
  839. err = nil
  840. data = nil
  841. } else {
  842. log.Error("GetCRecent conn.Do(GET,%s) error(%v)", key, err)
  843. }
  844. return
  845. }
  846. data = make([]*model.Contest, 0)
  847. if err = json.Unmarshal(bs, &data); err != nil {
  848. log.Error("GetCRecent json.Unmarshal(%s) error(%v)", string(bs), err)
  849. }
  850. return
  851. }
  852. // AddCRecent add contest recent data
  853. func (d *Dao) AddCRecent(c context.Context, param *model.ParamCDRecent, data []*model.Contest) (err error) {
  854. var (
  855. bs []byte
  856. key = keyCRecent(param)
  857. conn = d.redis.Get(c)
  858. )
  859. defer conn.Close()
  860. if bs, err = json.Marshal(data); err != nil {
  861. log.Error("AddCRecent json.Marshal() error(%v)", err)
  862. return
  863. }
  864. if err = conn.Send("SET", key, bs); err != nil {
  865. log.Error("AddCRecent conn.Send(SET,%s,%v) error(%v)", key, param, err)
  866. return
  867. }
  868. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  869. log.Error("AddCRecent conn.Send(EXPIRE,%s) error(%v)", key, err)
  870. return
  871. }
  872. if err = conn.Flush(); err != nil {
  873. log.Error("AddCRecent conn.Flush error(%v)", err)
  874. return
  875. }
  876. for i := 0; i < 2; i++ {
  877. if _, err = conn.Receive(); err != nil {
  878. log.Error("AddCRecent conn.Receive()%d error(%v)", i+1, err)
  879. return
  880. }
  881. }
  882. return
  883. }
  884. // AddActPageCache add act first page value
  885. func (d *Dao) AddActPageCache(c context.Context, aid int64, act *model.ActivePage) (err error) {
  886. var (
  887. bs []byte
  888. key = keyMatchAct(aid)
  889. conn = d.redis.Get(c)
  890. )
  891. defer conn.Close()
  892. if bs, err = json.Marshal(act); err != nil {
  893. log.Error("json.Marshal() error(%v)", err)
  894. return
  895. }
  896. if err = conn.Send("SET", key, bs); err != nil {
  897. log.Error("conn.Send(SET,%s,%d) error(%v)", key, aid, err)
  898. return
  899. }
  900. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  901. log.Error("conn.Send(EXPIRE,%s) error(%v)", key, err)
  902. return
  903. }
  904. if err = conn.Flush(); err != nil {
  905. log.Error("add conn.Flush error(%v)", err)
  906. return
  907. }
  908. for i := 0; i < 2; i++ {
  909. if _, err = conn.Receive(); err != nil {
  910. log.Error("add conn.Receive()%d error(%v)", i+1, err)
  911. return
  912. }
  913. }
  914. return
  915. }
  916. // GetActModuleCache get module from cache.
  917. func (d *Dao) GetActModuleCache(c context.Context, mmid int64) (res []*arcmdl.Arc, err error) {
  918. var (
  919. bs []byte
  920. key = keyMatchModule(mmid)
  921. conn = d.redis.Get(c)
  922. )
  923. defer conn.Close()
  924. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  925. if err == redis.ErrNil {
  926. err = nil
  927. res = nil
  928. } else {
  929. log.Error("GetModuleCache conn.Do(GET,%s) error(%v)", key, err)
  930. }
  931. return
  932. }
  933. if err = json.Unmarshal(bs, &res); err != nil {
  934. log.Error("GetModuleCache json.Unmarshal(%s) error(%v)", string(bs), err)
  935. }
  936. return
  937. }
  938. // AddActModuleCache add act first page cache
  939. func (d *Dao) AddActModuleCache(c context.Context, mmid int64, module []*arcmdl.Arc) (err error) {
  940. var (
  941. bs []byte
  942. key = keyMatchModule(mmid)
  943. conn = d.redis.Get(c)
  944. )
  945. defer conn.Close()
  946. if bs, err = json.Marshal(module); err != nil {
  947. log.Error("AddActModuleCache json.Marshal() error(%v)", err)
  948. return
  949. }
  950. if err = conn.Send("SET", key, bs); err != nil {
  951. log.Error("AddActModuleCache conn.Send(SET,%s,%d) error(%v)", key, mmid, err)
  952. return
  953. }
  954. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  955. log.Error("AddActModuleCache conn.Send(EXPIRE,%s) error(%v)", key, err)
  956. return
  957. }
  958. if err = conn.Flush(); err != nil {
  959. log.Error("AddActModuleCache add conn.Flush error(%v)", err)
  960. return
  961. }
  962. for i := 0; i < 2; i++ {
  963. if _, err = conn.Receive(); err != nil {
  964. log.Error("AddActModuleCache add conn.Receive()%d error(%v)", i+1, err)
  965. return
  966. }
  967. }
  968. return
  969. }
  970. // GetActTopCache get act top value cache
  971. func (d *Dao) GetActTopCache(c context.Context, aid, ps int64) (res []*model.Contest, total int, err error) {
  972. key := keyTop(aid, ps)
  973. conn := d.redis.Get(c)
  974. defer conn.Close()
  975. values, err := redis.Values(conn.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
  976. if err != nil {
  977. log.Error("GetActTopCache conn.Do(ZRANGE, %s) error(%v)", key, err)
  978. return
  979. }
  980. if len(values) == 0 {
  981. return
  982. }
  983. var num int64
  984. for len(values) > 0 {
  985. bs := []byte{}
  986. if values, err = redis.Scan(values, &bs, &num); err != nil {
  987. log.Error("redis.Scan(%v) error(%v)", values, err)
  988. return
  989. }
  990. cont := &model.Contest{}
  991. if err = json.Unmarshal(bs, cont); err != nil {
  992. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  993. return
  994. }
  995. res = append(res, cont)
  996. }
  997. total = from(num)
  998. return
  999. }
  1000. // AddActTopCache add act top cache
  1001. func (d *Dao) AddActTopCache(c context.Context, aid, ps int64, tops []*model.Contest, total int) (err error) {
  1002. key := keyTop(aid, ps)
  1003. conn := d.redis.Get(c)
  1004. defer conn.Close()
  1005. count := 0
  1006. if err = conn.Send("DEL", key); err != nil {
  1007. log.Error("AddActTopCache conn.Send(DEL, %s) error(%v)", key, err)
  1008. return
  1009. }
  1010. count++
  1011. args := redis.Args{}.Add(key)
  1012. for sort, contest := range tops {
  1013. bs, _ := json.Marshal(contest)
  1014. args = args.Add(combine(int64(sort), total)).Add(bs)
  1015. }
  1016. if err = conn.Send("ZADD", args...); err != nil {
  1017. log.Error("AddActTopCache conn.Send(ZADD, %s, %v) error(%v)", key, args, err)
  1018. return
  1019. }
  1020. count++
  1021. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  1022. log.Error("AddActTopCache conn.Send(Expire, %s, %d) error(%v)", key, d.listExpire, err)
  1023. return
  1024. }
  1025. count++
  1026. if err = conn.Flush(); err != nil {
  1027. log.Error("AddActTopCache conn.Flush error(%v)", err)
  1028. return
  1029. }
  1030. for i := 0; i < count; i++ {
  1031. if _, err = conn.Receive(); err != nil {
  1032. log.Error("AddActTopCache conn.Receive() error(%v)", err)
  1033. return
  1034. }
  1035. }
  1036. return
  1037. }
  1038. // GetActPointsCache get act point value
  1039. func (d *Dao) GetActPointsCache(c context.Context, aid, mdID, ps int64) (res []*model.Contest, total int, err error) {
  1040. key := keyPoint(aid, mdID, ps)
  1041. conn := d.redis.Get(c)
  1042. defer conn.Close()
  1043. values, err := redis.Values(conn.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
  1044. if err != nil {
  1045. log.Error("GetActTopCache conn.Do(ZRANGE, %s) error(%v)", key, err)
  1046. return
  1047. }
  1048. if len(values) == 0 {
  1049. return
  1050. }
  1051. var num int64
  1052. for len(values) > 0 {
  1053. bs := []byte{}
  1054. if values, err = redis.Scan(values, &bs, &num); err != nil {
  1055. log.Error("redis.Scan(%v) error(%v)", values, err)
  1056. return
  1057. }
  1058. cont := &model.Contest{}
  1059. if err = json.Unmarshal(bs, cont); err != nil {
  1060. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  1061. return
  1062. }
  1063. res = append(res, cont)
  1064. }
  1065. total = from(num)
  1066. return
  1067. }
  1068. // AddActPointsCache add act point data cache
  1069. func (d *Dao) AddActPointsCache(c context.Context, aid, mdID, ps int64, points []*model.Contest, total int) (err error) {
  1070. key := keyPoint(aid, mdID, ps)
  1071. conn := d.redis.Get(c)
  1072. defer conn.Close()
  1073. count := 0
  1074. if err = conn.Send("DEL", key); err != nil {
  1075. log.Error("AddActTopCache conn.Send(DEL, %s) error(%v)", key, err)
  1076. return
  1077. }
  1078. count++
  1079. args := redis.Args{}.Add(key)
  1080. for sort, contest := range points {
  1081. bs, _ := json.Marshal(contest)
  1082. args = args.Add(combine(int64(sort), total)).Add(bs)
  1083. }
  1084. if err = conn.Send("ZADD", args...); err != nil {
  1085. log.Error("AddActTopCache conn.Send(ZADD, %s, %v) error(%v)", key, args, err)
  1086. return
  1087. }
  1088. count++
  1089. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  1090. log.Error("AddActTopCache conn.Send(Expire, %s, %d) error(%v)", key, d.listExpire, err)
  1091. return
  1092. }
  1093. count++
  1094. if err = conn.Flush(); err != nil {
  1095. log.Error("AddActTopCache conn.Flush error(%v)", err)
  1096. return
  1097. }
  1098. for i := 0; i < count; i++ {
  1099. if _, err = conn.Receive(); err != nil {
  1100. log.Error("AddActTopCache conn.Receive() error(%v)", err)
  1101. return
  1102. }
  1103. }
  1104. return
  1105. }
  1106. // GetActKnockoutCache add act knockout cache value
  1107. func (d *Dao) GetActKnockoutCache(c context.Context, mdID int64) (res [][]*model.TreeList, err error) {
  1108. var (
  1109. bs []byte
  1110. key = keyKnock(mdID)
  1111. conn = d.redis.Get(c)
  1112. )
  1113. defer conn.Close()
  1114. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  1115. if err == redis.ErrNil {
  1116. err = nil
  1117. res = nil
  1118. } else {
  1119. log.Error("GetActKnockoutCache conn.Do(GET,%s) error(%v)", key, err)
  1120. }
  1121. return
  1122. }
  1123. if err = json.Unmarshal(bs, &res); err != nil {
  1124. log.Error("GetActKnockoutCache json.Unmarshal(%s) error(%v)", string(bs), err)
  1125. }
  1126. return
  1127. }
  1128. // AddActKnockoutCache add act knockout cache value
  1129. func (d *Dao) AddActKnockoutCache(c context.Context, mdID int64, knock [][]*model.TreeList) (err error) {
  1130. var (
  1131. bs []byte
  1132. key = keyKnock(mdID)
  1133. conn = d.redis.Get(c)
  1134. )
  1135. defer conn.Close()
  1136. if bs, err = json.Marshal(knock); err != nil {
  1137. log.Error("AddActKnockoutCache json.Marshal() error(%v)", err)
  1138. return
  1139. }
  1140. if err = conn.Send("SET", key, bs); err != nil {
  1141. log.Error("AddActKnockoutCache conn.Send(SET,%s,%d) error(%v)", key, mdID, err)
  1142. return
  1143. }
  1144. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  1145. log.Error("AddActKnockoutCache conn.Send(EXPIRE,%s) error(%v)", key, err)
  1146. return
  1147. }
  1148. if err = conn.Flush(); err != nil {
  1149. log.Error("AddActKnockoutCache add conn.Flush error(%v)", err)
  1150. return
  1151. }
  1152. for i := 0; i < 2; i++ {
  1153. if _, err = conn.Receive(); err != nil {
  1154. log.Error("AddActModuleCache add conn.Receive()%d error(%v)", i+1, err)
  1155. return
  1156. }
  1157. }
  1158. return
  1159. }
  1160. // AddActKnockCacheTime add act knockout cache value time
  1161. func (d *Dao) AddActKnockCacheTime(c context.Context, mdID int64) (err error) {
  1162. var (
  1163. key = keyKnock(mdID)
  1164. conn = d.redis.Get(c)
  1165. )
  1166. defer conn.Close()
  1167. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  1168. log.Error("AddActKnockCacheTime conn.Send(EXPIRE,%s) error(%v)", key, err)
  1169. return
  1170. }
  1171. if err = conn.Flush(); err != nil {
  1172. log.Error("AddActKnockCacheTime add conn.Flush error(%v)", err)
  1173. return
  1174. }
  1175. if _, err = conn.Receive(); err != nil {
  1176. log.Error("AddActKnockCacheTime add error(%v)", err)
  1177. return
  1178. }
  1179. return
  1180. }
  1181. // GetMActCache get act cache value
  1182. func (d *Dao) GetMActCache(c context.Context, aid int64) (res *model.Active, err error) {
  1183. var (
  1184. bs []byte
  1185. key = keyMAct(aid)
  1186. conn = d.redis.Get(c)
  1187. )
  1188. defer conn.Close()
  1189. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  1190. if err == redis.ErrNil {
  1191. err = nil
  1192. res = nil
  1193. } else {
  1194. log.Error("GetMActCache conn.Do(GET,%s) error(%v)", key, err)
  1195. }
  1196. return
  1197. }
  1198. if err = json.Unmarshal(bs, &res); err != nil {
  1199. log.Error("GetMActCache json.Unmarshal(%s) error(%v)", string(bs), err)
  1200. }
  1201. return
  1202. }
  1203. // AddMActCache add act cache value
  1204. func (d *Dao) AddMActCache(c context.Context, aid int64, act *model.Active) (err error) {
  1205. var (
  1206. bs []byte
  1207. key = keyMAct(aid)
  1208. conn = d.redis.Get(c)
  1209. )
  1210. defer conn.Close()
  1211. if bs, err = json.Marshal(act); err != nil {
  1212. log.Error("AddMActCache json.Marshal() error(%v)", err)
  1213. return
  1214. }
  1215. if err = conn.Send("SET", key, bs); err != nil {
  1216. log.Error("AddMActCache conn.Send(SET,%s,%d) error(%v)", key, aid, err)
  1217. return
  1218. }
  1219. if err = conn.Send("EXPIRE", key, d.listExpire); err != nil {
  1220. log.Error("AddMActCache conn.Send(EXPIRE,%s) error(%v)", key, err)
  1221. return
  1222. }
  1223. if err = conn.Flush(); err != nil {
  1224. log.Error("AddMActCache add conn.Flush error(%v)", err)
  1225. return
  1226. }
  1227. for i := 0; i < 2; i++ {
  1228. if _, err = conn.Receive(); err != nil {
  1229. log.Error("AddMActCache add conn.Receive()%d error(%v)", i+1, err)
  1230. return
  1231. }
  1232. }
  1233. return
  1234. }