match_redis.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. package like
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. match "go-common/app/interface/main/activity/model/like"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. "go-common/library/time"
  10. )
  11. const (
  12. _keyMatch = "mat_%d"
  13. _keyActMatch = "am_%d"
  14. _keyObject = "ob_%d"
  15. _keyObjects = "os_%d"
  16. _keyUserLog = "ugl_%d_%d"
  17. _keyMatchFollow = "mf_%d"
  18. )
  19. func keyMatch(id int64) string {
  20. return fmt.Sprintf(_keyMatch, id)
  21. }
  22. func keyActMatch(sid int64) string {
  23. return fmt.Sprintf(_keyActMatch, sid)
  24. }
  25. func keyObject(id int64) string {
  26. return fmt.Sprintf(_keyObject, id)
  27. }
  28. func keyObjects(sid int64) string {
  29. return fmt.Sprintf(_keyObjects, sid)
  30. }
  31. func keyUserLog(sid, mid int64) string {
  32. return fmt.Sprintf(_keyUserLog, sid, mid)
  33. }
  34. func keyMatchFollow(mid int64) string {
  35. return fmt.Sprintf(_keyMatchFollow, mid)
  36. }
  37. // MatchCache get match from cache.
  38. func (d *Dao) MatchCache(c context.Context, id int64) (mat *match.Match, err error) {
  39. var (
  40. bs []byte
  41. key = keyMatch(id)
  42. conn = d.redis.Get(c)
  43. )
  44. defer conn.Close()
  45. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  46. if err == redis.ErrNil {
  47. err = nil
  48. mat = nil
  49. } else {
  50. log.Error("conn.Do(GET,%s) error(%v)", key, err)
  51. }
  52. return
  53. }
  54. mat = new(match.Match)
  55. if err = json.Unmarshal(bs, mat); err != nil {
  56. log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
  57. }
  58. return
  59. }
  60. // SetMatchCache set match to cache.
  61. func (d *Dao) SetMatchCache(c context.Context, id int64, mat *match.Match) (err error) {
  62. var (
  63. bs []byte
  64. key = keyMatch(id)
  65. conn = d.redis.Get(c)
  66. )
  67. defer conn.Close()
  68. if bs, err = json.Marshal(mat); err != nil {
  69. log.Error("json.Marshal() error(%v)", err)
  70. return
  71. }
  72. if err = conn.Send("SET", key, bs); err != nil {
  73. log.Error("conn.Send(SET,%s,%d) error(%v)", key, id, err)
  74. return
  75. }
  76. if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
  77. log.Error("conn.Send(EXPIRE,%s) error(%v)", key, err)
  78. return
  79. }
  80. if err = conn.Flush(); err != nil {
  81. log.Error("add conn.Flush error(%v)", err)
  82. return
  83. }
  84. for i := 0; i < 2; i++ {
  85. if _, err = conn.Receive(); err != nil {
  86. log.Error("add conn.Receive()%d error(%v)", i+1, err)
  87. return
  88. }
  89. }
  90. return
  91. }
  92. // ActMatchCache get match list from cache.
  93. func (d *Dao) ActMatchCache(c context.Context, sid int64) (res []*match.Match, err error) {
  94. key := keyActMatch(sid)
  95. conn := d.redis.Get(c)
  96. defer conn.Close()
  97. values, err := redis.Values(conn.Do("ZRANGE", key, 0, -1, "WITHSCORES"))
  98. if err != nil {
  99. log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
  100. return
  101. }
  102. if len(values) == 0 {
  103. return
  104. }
  105. var num int64
  106. for len(values) > 0 {
  107. bs := []byte{}
  108. if values, err = redis.Scan(values, &bs, &num); err != nil {
  109. log.Error("redis.Scan(%v) error(%v)", values, err)
  110. return
  111. }
  112. match := &match.Match{}
  113. if err = json.Unmarshal(bs, match); err != nil {
  114. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  115. return
  116. }
  117. res = append(res, match)
  118. }
  119. return
  120. }
  121. // SetActMatchCache set match list cache.
  122. func (d *Dao) SetActMatchCache(c context.Context, sid int64, matchs []*match.Match) (err error) {
  123. key := keyActMatch(sid)
  124. conn := d.redis.Get(c)
  125. defer conn.Close()
  126. count := 0
  127. if err = conn.Send("DEL", key); err != nil {
  128. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  129. return
  130. }
  131. count++
  132. for _, match := range matchs {
  133. bs, _ := json.Marshal(match)
  134. if err = conn.Send("ZADD", key, match.Ctime, bs); err != nil {
  135. log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
  136. return
  137. }
  138. count++
  139. }
  140. if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
  141. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.matchExpire, err)
  142. return
  143. }
  144. count++
  145. if err = conn.Flush(); err != nil {
  146. log.Error("conn.Flush error(%v)", err)
  147. return
  148. }
  149. for i := 0; i < count; i++ {
  150. if _, err = conn.Receive(); err != nil {
  151. log.Error("conn.Receive() error(%v)", err)
  152. return
  153. }
  154. }
  155. return
  156. }
  157. // ObjectCache get object from cache.
  158. func (d *Dao) ObjectCache(c context.Context, id int64) (mat *match.Object, err error) {
  159. var (
  160. bs []byte
  161. key = keyObject(id)
  162. conn = d.redis.Get(c)
  163. )
  164. defer conn.Close()
  165. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  166. if err == redis.ErrNil {
  167. err = nil
  168. mat = nil
  169. } else {
  170. log.Error("conn.Do(GET,%s) error(%v)", key, err)
  171. }
  172. return
  173. }
  174. mat = new(match.Object)
  175. if err = json.Unmarshal(bs, mat); err != nil {
  176. log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
  177. }
  178. return
  179. }
  180. // CacheMatchSubjects .
  181. func (d *Dao) CacheMatchSubjects(c context.Context, ids []int64) (res map[int64]*match.Object, err error) {
  182. var (
  183. key string
  184. args = redis.Args{}
  185. bss [][]byte
  186. )
  187. for _, pid := range ids {
  188. key = keyObject(pid)
  189. args = args.Add(key)
  190. }
  191. conn := d.redis.Get(c)
  192. defer conn.Close()
  193. if bss, err = redis.ByteSlices(conn.Do("MGET", args...)); err != nil {
  194. if err == redis.ErrNil {
  195. err = nil
  196. } else {
  197. log.Error("CacheMatchSubjects conn.Do(MGET,%s) error(%v)", key, err)
  198. }
  199. return
  200. }
  201. res = make(map[int64]*match.Object, len(ids))
  202. for _, bs := range bss {
  203. obj := new(match.Object)
  204. if bs == nil {
  205. continue
  206. }
  207. if err = json.Unmarshal(bs, obj); err != nil {
  208. log.Error("CacheMatchSubjects json.Unmarshal(%s) error(%v)", string(bs), err)
  209. err = nil
  210. continue
  211. }
  212. res[obj.ID] = obj
  213. }
  214. return
  215. }
  216. // SetObjectCache set object to cache.
  217. func (d *Dao) SetObjectCache(c context.Context, id int64, object *match.Object) (err error) {
  218. var (
  219. bs []byte
  220. key = keyObject(id)
  221. conn = d.redis.Get(c)
  222. )
  223. defer conn.Close()
  224. if bs, err = json.Marshal(object); err != nil {
  225. log.Error("json.Marshal() error(%v)", err)
  226. return
  227. }
  228. if err = conn.Send("SET", key, bs); err != nil {
  229. log.Error("conn.Send(HSET,%s,%d) error(%v)", key, id, err)
  230. return
  231. }
  232. if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
  233. log.Error("conn.Send(EXPIRE,%s) error(%v)", key, err)
  234. return
  235. }
  236. if err = conn.Flush(); err != nil {
  237. log.Error("add conn.Flush error(%v)", err)
  238. return
  239. }
  240. for i := 0; i < 2; i++ {
  241. if _, err = conn.Receive(); err != nil {
  242. log.Error("add conn.Receive()%d error(%v)", i+1, err)
  243. return
  244. }
  245. }
  246. return
  247. }
  248. // AddCacheMatchSubjects .
  249. func (d *Dao) AddCacheMatchSubjects(c context.Context, data map[int64]*match.Object) (err error) {
  250. if len(data) == 0 {
  251. return
  252. }
  253. var (
  254. bs []byte
  255. keyID string
  256. keyIDs []string
  257. argsPid = redis.Args{}
  258. )
  259. conn := d.redis.Get(c)
  260. defer conn.Close()
  261. for _, v := range data {
  262. if bs, err = json.Marshal(v); err != nil {
  263. log.Error("json.Marshal err(%v)", err)
  264. continue
  265. }
  266. keyID = keyObject(v.ID)
  267. keyIDs = append(keyIDs, keyID)
  268. argsPid = argsPid.Add(keyID).Add(string(bs))
  269. }
  270. if err = conn.Send("MSET", argsPid...); err != nil {
  271. log.Error("AddCacheMatchSubjects conn.Send(MSET) error(%v)", err)
  272. return
  273. }
  274. count := 1
  275. for _, v := range keyIDs {
  276. count++
  277. if err = conn.Send("EXPIRE", v, d.matchExpire); err != nil {
  278. log.Error("AddCacheMatchSubjects conn.Send(Expire, %s, %d) error(%v)", v, d.matchExpire, err)
  279. return
  280. }
  281. }
  282. if err = conn.Flush(); err != nil {
  283. log.Error("conn.Flush error(%v)", err)
  284. return
  285. }
  286. for i := 0; i < count; i++ {
  287. if _, err = conn.Receive(); err != nil {
  288. log.Error("conn.Receive() error(%v)", err)
  289. return
  290. }
  291. }
  292. return
  293. }
  294. // ObjectsCache get object list from cache.
  295. func (d *Dao) ObjectsCache(c context.Context, sid int64, start, end int) (res []*match.Object, total int, err error) {
  296. key := keyObjects(sid)
  297. conn := d.redis.Get(c)
  298. defer conn.Close()
  299. values, err := redis.Values(conn.Do("ZRANGE", key, start, end, "WITHSCORES"))
  300. if err != nil {
  301. log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
  302. return
  303. }
  304. if len(values) == 0 {
  305. return
  306. }
  307. var num int64
  308. for len(values) > 0 {
  309. bs := []byte{}
  310. if values, err = redis.Scan(values, &bs, &num); err != nil {
  311. log.Error("redis.Scan(%v) error(%v)", values, err)
  312. return
  313. }
  314. object := &match.Object{}
  315. if err = json.Unmarshal(bs, object); err != nil {
  316. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  317. return
  318. }
  319. res = append(res, object)
  320. }
  321. total = from(num)
  322. return
  323. }
  324. // SetObjectsCache set object list cache.
  325. func (d *Dao) SetObjectsCache(c context.Context, sid int64, objects []*match.Object, total int) (err error) {
  326. key := keyObjects(sid)
  327. conn := d.redis.Get(c)
  328. defer conn.Close()
  329. count := 0
  330. if err = conn.Send("DEL", key); err != nil {
  331. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  332. return
  333. }
  334. count++
  335. for _, object := range objects {
  336. bs, _ := json.Marshal(object)
  337. if err = conn.Send("ZADD", key, combine(object.GameStime, total), bs); err != nil {
  338. log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
  339. return
  340. }
  341. count++
  342. }
  343. if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
  344. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.matchExpire, err)
  345. return
  346. }
  347. count++
  348. if err = conn.Flush(); err != nil {
  349. log.Error("conn.Flush error(%v)", err)
  350. return
  351. }
  352. for i := 0; i < count; i++ {
  353. if _, err = conn.Receive(); err != nil {
  354. log.Error("conn.Receive() error(%v)", err)
  355. return
  356. }
  357. }
  358. return
  359. }
  360. // UserLogCache get user log list from cache.
  361. func (d *Dao) UserLogCache(c context.Context, sid, mid int64) (res []*match.UserLog, err error) {
  362. key := keyUserLog(sid, mid)
  363. conn := d.redis.Get(c)
  364. defer conn.Close()
  365. values, err := redis.Values(conn.Do("ZREVRANGE", key, 0, -1, "WITHSCORES"))
  366. if err != nil {
  367. log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
  368. return
  369. }
  370. if len(values) == 0 {
  371. return
  372. }
  373. var num int64
  374. for len(values) > 0 {
  375. bs := []byte{}
  376. if values, err = redis.Scan(values, &bs, &num); err != nil {
  377. log.Error("redis.Scan(%v) error(%v)", values, err)
  378. return
  379. }
  380. userLog := &match.UserLog{}
  381. if err = json.Unmarshal(bs, userLog); err != nil {
  382. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  383. return
  384. }
  385. res = append(res, userLog)
  386. }
  387. return
  388. }
  389. // SetUserLogCache set user log list cache.
  390. func (d *Dao) SetUserLogCache(c context.Context, sid, mid int64, userLogs []*match.UserLog) (err error) {
  391. key := keyUserLog(sid, mid)
  392. conn := d.redis.Get(c)
  393. defer conn.Close()
  394. count := 0
  395. if err = conn.Send("DEL", key); err != nil {
  396. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  397. return
  398. }
  399. count++
  400. for _, userLog := range userLogs {
  401. bs, _ := json.Marshal(userLog)
  402. if err = conn.Send("ZADD", key, userLog.Ctime, bs); err != nil {
  403. log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
  404. return
  405. }
  406. count++
  407. }
  408. if err = conn.Send("EXPIRE", key, d.matchExpire); err != nil {
  409. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.matchExpire, err)
  410. return
  411. }
  412. count++
  413. if err = conn.Flush(); err != nil {
  414. log.Error("conn.Flush error(%v)", err)
  415. return
  416. }
  417. for i := 0; i < count; i++ {
  418. if _, err = conn.Receive(); err != nil {
  419. log.Error("conn.Receive() error(%v)", err)
  420. return
  421. }
  422. }
  423. return
  424. }
  425. // DelUserLogCache delete user log cache.
  426. func (d *Dao) DelUserLogCache(c context.Context, sid, mid int64) (err error) {
  427. key := keyUserLog(sid, mid)
  428. conn := d.redis.Get(c)
  429. defer conn.Close()
  430. _, err = conn.Do("DEL", key)
  431. return
  432. }
  433. // DelActMatchCache del match cache
  434. func (d *Dao) DelActMatchCache(c context.Context, sid, matID int64) (err error) {
  435. matKey := keyMatch(matID)
  436. actMKey := keyActMatch(sid)
  437. conn := d.redis.Get(c)
  438. defer conn.Close()
  439. if err = conn.Send("DEL", matKey); err != nil {
  440. log.Error("conn.Send(DEL, %s) error(%v)", actMKey, err)
  441. return
  442. }
  443. if err = conn.Send("DEL", actMKey); err != nil {
  444. log.Error("conn.Send(DEL, %s) error(%v)", actMKey, err)
  445. return
  446. }
  447. if err = conn.Flush(); err != nil {
  448. log.Error("conn.Flush() error(%v)", err)
  449. return
  450. }
  451. for i := 0; i < 2; i++ {
  452. if _, err = conn.Receive(); err != nil {
  453. log.Error("conn.Receive() error(%v)", err)
  454. return
  455. }
  456. }
  457. return
  458. }
  459. // DelObjectCache del object cache
  460. func (d *Dao) DelObjectCache(c context.Context, objID, sid int64) (err error) {
  461. objKey := keyObject(objID)
  462. objsKey := keyObjects(sid)
  463. conn := d.redis.Get(c)
  464. defer conn.Close()
  465. if err = conn.Send("DEL", objKey); err != nil {
  466. log.Error("conn.Send(DEL, %s) error(%v)", objKey, err)
  467. return
  468. }
  469. if err = conn.Send("DEL", objsKey); err != nil {
  470. log.Error("conn.Send(DEL, %s) error(%v)", objKey, err)
  471. return
  472. }
  473. if err = conn.Flush(); err != nil {
  474. log.Error("conn.Flush() error(%v)", err)
  475. return
  476. }
  477. for i := 0; i < 2; i++ {
  478. if _, err = conn.Receive(); err != nil {
  479. log.Error("conn.Receive() error(%v)", err)
  480. return
  481. }
  482. }
  483. return
  484. }
  485. // AddFollow add follow teams
  486. func (d *Dao) AddFollow(c context.Context, mid int64, teams []string) (err error) {
  487. var (
  488. bs []byte
  489. key = keyMatchFollow(mid)
  490. conn = d.redis.Get(c)
  491. )
  492. defer conn.Close()
  493. if bs, err = json.Marshal(teams); err != nil {
  494. log.Error("json.Marshal() error(%v)", err)
  495. return
  496. }
  497. if err = conn.Send("SET", key, bs); err != nil {
  498. log.Error("conn.Send(SET,%s,%d) error(%v)", key, mid, err)
  499. return
  500. }
  501. if err = conn.Send("EXPIRE", key, d.followExpire); err != nil {
  502. log.Error("conn.Send(EXPIRE,%s) error(%v)", key, err)
  503. return
  504. }
  505. if err = conn.Flush(); err != nil {
  506. log.Error("add conn.Flush error(%v)", err)
  507. return
  508. }
  509. for i := 0; i < 2; i++ {
  510. if _, err = conn.Receive(); err != nil {
  511. log.Error("add conn.Receive()%d error(%v)", i+1, err)
  512. return
  513. }
  514. }
  515. return
  516. }
  517. // Follow get follow teams
  518. func (d *Dao) Follow(c context.Context, mid int64) (res []string, err error) {
  519. var (
  520. bs []byte
  521. key = keyMatchFollow(mid)
  522. conn = d.redis.Get(c)
  523. )
  524. defer conn.Close()
  525. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  526. if err == redis.ErrNil {
  527. err = nil
  528. } else {
  529. log.Error("conn.Do(GET,%s) error(%v)", key, err)
  530. }
  531. return
  532. }
  533. if err = json.Unmarshal(bs, &res); err != nil {
  534. log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
  535. }
  536. return
  537. }
  538. func from(i int64) int {
  539. return int(i & 0xffff)
  540. }
  541. func combine(ctime time.Time, count int) int64 {
  542. return ctime.Time().Unix()<<16 | int64(count)
  543. }