redis.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/reply-feed/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. )
  10. const (
  11. // r_<实验组名>_<oid>_<type>
  12. // 用redis ZSet存储热门评论列表,score为热评分数,member为rpID
  13. _replyZSetFormat = "r_%s_%d_%d"
  14. // c_<oid>_<type>
  15. _refreshCheckerFormat = "c_%d_%d"
  16. // h_<oid>_<type>
  17. // 用一个set来存某一个评论区下的应该存在于热门评论列表的评论ID
  18. // 上热评的最低门槛,点赞数大于3,且该评论区根评论数目大于20
  19. _replyListFormat = "h_%d_%d"
  20. // 行为, 小时,slot, 种类(全量或者针对热评)
  21. _uvFormat = "uv_%s_%d_%d_%s"
  22. _uvExp = 3600
  23. )
  24. func keyRefreshChecker(oid int64, tp int) string {
  25. return fmt.Sprintf(_refreshCheckerFormat, oid, tp)
  26. }
  27. func keyReplyZSet(name string, oid int64, tp int) string {
  28. return fmt.Sprintf(_replyZSetFormat, name, oid, tp)
  29. }
  30. func keyReplySet(oid int64, tp int) string {
  31. return fmt.Sprintf(_replyListFormat, oid, tp)
  32. }
  33. // KeyUV ...
  34. func (d *Dao) KeyUV(action string, hour, slot int, kind string) string {
  35. return keyUV(action, hour, slot, kind)
  36. }
  37. func keyUV(action string, hour, slot int, kind string) string {
  38. return fmt.Sprintf(_uvFormat, action, hour, slot, kind)
  39. }
  40. // PingRedis redis health check.
  41. func (d *Dao) PingRedis(ctx context.Context) (err error) {
  42. conn := d.redis.Get(ctx)
  43. defer conn.Close()
  44. _, err = conn.Do("SET", "ping", "pong")
  45. return
  46. }
  47. // AddUV ...
  48. func (d *Dao) AddUV(ctx context.Context, action string, hour, slot int, mid int64, kind string) (err error) {
  49. key := keyUV(action, hour, slot, kind)
  50. conn := d.redis.Get(ctx)
  51. defer conn.Close()
  52. if err = conn.Send("SADD", key, mid); err != nil {
  53. log.Error("redis SADD(%s, %d) error(%v)", key, mid, err)
  54. return
  55. }
  56. if err = conn.Send("EXPIRE", key, _uvExp); err != nil {
  57. log.Error("redis EXPIRE(%s) error(%v)", key, err)
  58. return
  59. }
  60. if err = conn.Flush(); err != nil {
  61. log.Error("redis Flush() error(%v)", err)
  62. return
  63. }
  64. for i := 0; i < 2; i++ {
  65. if _, err = conn.Receive(); err != nil {
  66. log.Error("redis Receive() error(%v)", err)
  67. return
  68. }
  69. }
  70. return
  71. }
  72. // CountUV ...
  73. func (d *Dao) CountUV(ctx context.Context, keys []string) (counts []int64, err error) {
  74. conn := d.redis.Get(ctx)
  75. defer conn.Close()
  76. j := 0
  77. for _, key := range keys {
  78. if err = conn.Send("SCARD", key); err != nil {
  79. log.Error("redis SCARD(%s) error(%v)", key, err)
  80. return
  81. }
  82. j++
  83. }
  84. if err = conn.Flush(); err != nil {
  85. log.Error("redis Flush() error(%v)", err)
  86. return
  87. }
  88. for i := 0; i < j; i++ {
  89. var count int64
  90. if count, err = redis.Int64(conn.Receive()); err != nil && err != redis.ErrNil {
  91. log.Error("redis Receive() error(%v)", err)
  92. return
  93. }
  94. counts = append(counts, count)
  95. }
  96. return
  97. }
  98. // ExpireCheckerRds expire checker.
  99. func (d *Dao) ExpireCheckerRds(ctx context.Context, oid int64, tp int) (ok bool, err error) {
  100. conn := d.redis.Get(ctx)
  101. defer conn.Close()
  102. key := keyRefreshChecker(oid, tp)
  103. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisRefreshExpire)); err != nil {
  104. if err == redis.ErrNil {
  105. err = nil
  106. return
  107. }
  108. log.Error("redis EXPIRE key(%s) error(%v)", key, err)
  109. }
  110. return
  111. }
  112. // ExpireReplyZSetRds expire reply list.
  113. func (d *Dao) ExpireReplyZSetRds(ctx context.Context, name string, oid int64, tp int) (ok bool, err error) {
  114. conn := d.redis.Get(ctx)
  115. defer conn.Close()
  116. key := keyReplyZSet(name, oid, tp)
  117. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisReplyZSetExpire)); err != nil {
  118. if err == redis.ErrNil {
  119. err = nil
  120. return
  121. }
  122. log.Error("redis EXPIRE key(%s) error(%v)", key, err)
  123. }
  124. return
  125. }
  126. // ExpireReplySetRds expire reply set.
  127. func (d *Dao) ExpireReplySetRds(ctx context.Context, oid int64, tp int) (ok bool, err error) {
  128. conn := d.redis.Get(ctx)
  129. defer conn.Close()
  130. key := keyReplySet(oid, tp)
  131. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisReplySetExpire)); err != nil {
  132. if err == redis.ErrNil {
  133. err = nil
  134. return
  135. }
  136. log.Error("redis EXPIRE key(%s) error(%v)", key, err)
  137. }
  138. return
  139. }
  140. // ReplySetRds get reply rpIDs from redis set.
  141. func (d *Dao) ReplySetRds(ctx context.Context, oid int64, tp int) (rpIDs []int64, err error) {
  142. key := keyReplySet(oid, tp)
  143. var startCursor, endCursor int64
  144. endCursor = 1
  145. for endCursor != 0 {
  146. var (
  147. conn = d.redis.Get(ctx)
  148. chunkedRpIDs []int64
  149. values []interface{}
  150. )
  151. if values, err = redis.Values(conn.Do("SSCAN", key, startCursor)); err != nil {
  152. log.Error("redis SSCAN(%s) error(%v)", key, err)
  153. conn.Close()
  154. return
  155. }
  156. if _, err = redis.Scan(values, &endCursor, &chunkedRpIDs); err != nil {
  157. log.Error("redis Scan(%v) error(%v)", values, err)
  158. conn.Close()
  159. return
  160. }
  161. startCursor = endCursor
  162. rpIDs = append(rpIDs, chunkedRpIDs...)
  163. conn.Close()
  164. }
  165. return
  166. }
  167. // SetReplySetRds set reply list batch, call it when back to source.
  168. func (d *Dao) SetReplySetRds(ctx context.Context, oid int64, tp int, rpIDs []int64) (err error) {
  169. if len(rpIDs) < 1 {
  170. return
  171. }
  172. for _, chunkedRpIDs := range split(rpIDs, 5000) {
  173. var (
  174. key = keyReplySet(oid, tp)
  175. args = make([]interface{}, 0, len(chunkedRpIDs)+1)
  176. conn = d.redis.Get(ctx)
  177. )
  178. args = append(args, key)
  179. for _, rpID := range chunkedRpIDs {
  180. args = append(args, rpID)
  181. }
  182. if err = conn.Send("SADD", args...); err != nil {
  183. log.Error("redis SADD(%v) error(%v)", args, err)
  184. conn.Close()
  185. return
  186. }
  187. if err = conn.Send("EXPIRE", key, d.redisReplySetExpire); err != nil {
  188. log.Error("redis EXPIRE(%s) error(%v)", key, err)
  189. conn.Close()
  190. return
  191. }
  192. if err = conn.Flush(); err != nil {
  193. log.Error("redis Flush() error(%v)", err)
  194. conn.Close()
  195. return
  196. }
  197. for i := 0; i < 2; i++ {
  198. if _, err = conn.Receive(); err != nil {
  199. log.Error("redis Receive() error(%v)", err)
  200. conn.Close()
  201. return
  202. }
  203. }
  204. conn.Close()
  205. }
  206. return
  207. }
  208. // RemReplySetRds remove one rp from set.
  209. func (d *Dao) RemReplySetRds(ctx context.Context, oid, rpID int64, tp int) (err error) {
  210. conn := d.redis.Get(ctx)
  211. defer conn.Close()
  212. key := keyReplySet(oid, tp)
  213. if _, err = redis.Int64(conn.Do("SREM", key, rpID)); err != nil {
  214. if err == redis.ErrNil {
  215. err = nil
  216. return
  217. }
  218. log.Error("SREM conn.Do(%s,%d) err(%v)", key, rpID, err)
  219. }
  220. return
  221. }
  222. // DelReplySetRds delete a set key.
  223. func (d *Dao) DelReplySetRds(ctx context.Context, oid int64, tp int) (err error) {
  224. conn := d.redis.Get(ctx)
  225. defer conn.Close()
  226. key := keyReplySet(oid, tp)
  227. if _, err = conn.Do("DEL", key); err != nil {
  228. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  229. }
  230. return
  231. }
  232. // AddReplySetRds add a reply into redis set, make sure expire the key first.
  233. func (d *Dao) AddReplySetRds(ctx context.Context, oid int64, tp int, rpID int64) (err error) {
  234. conn := d.redis.Get(ctx)
  235. defer conn.Close()
  236. key := keyReplySet(oid, tp)
  237. if _, err = conn.Do("SADD", key, rpID); err != nil {
  238. log.Error("redis SADD(%s, %d) error(%v)", key, rpID, err)
  239. }
  240. return
  241. }
  242. // ReplyZSetRds get reply list from redis sorted set.
  243. func (d *Dao) ReplyZSetRds(ctx context.Context, name string, oid int64, tp, start, end int) (rpIDs []int64, err error) {
  244. conn := d.redis.Get(ctx)
  245. defer conn.Close()
  246. key := keyReplyZSet(name, oid, tp)
  247. values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end))
  248. if err != nil {
  249. log.Error("redis ZREVRANGE(%s, %d, %d) error(%v)", key, start, end, err)
  250. return
  251. }
  252. if err = redis.ScanSlice(values, &rpIDs); err != nil {
  253. log.Error("redis ScanSlice(%v) error(%v)", values, err)
  254. }
  255. return
  256. }
  257. // SetReplyZSetRds set reply list batch, call it when back to source.
  258. func (d *Dao) SetReplyZSetRds(ctx context.Context, name string, oid int64, tp int, rs []*model.ReplyScore) (err error) {
  259. if len(rs) < 1 {
  260. return
  261. }
  262. for _, chunkedReplyStats := range splitReplyScore(rs, 5000) {
  263. var (
  264. count = 0
  265. key = keyReplyZSet(name, oid, tp)
  266. conn = d.redis.Get(ctx)
  267. args = make([]interface{}, 0, len(chunkedReplyStats)*2+1)
  268. )
  269. args = append(args, key)
  270. for _, s := range chunkedReplyStats {
  271. args = append(args, s.Score)
  272. args = append(args, s.RpID)
  273. }
  274. if err = conn.Send("ZADD", args...); err != nil {
  275. log.Error("redis ZADD(%s, %v) error(%v)", key, args, err)
  276. conn.Close()
  277. return
  278. }
  279. count++
  280. if err = conn.Send("EXPIRE", key, d.redisReplyZSetExpire); err != nil {
  281. log.Error("redis EXPIRE(%s) error(%v)", key, err)
  282. conn.Close()
  283. return
  284. }
  285. count++
  286. if err = conn.Flush(); err != nil {
  287. log.Error("redis Flush error(%v)", err)
  288. conn.Close()
  289. return
  290. }
  291. for i := 0; i < count; i++ {
  292. if _, err = conn.Receive(); err != nil {
  293. log.Error("redis Receive (key: %s, %f, %d) error(%v)", key, rs[i].Score, rs[i].RpID, err)
  294. conn.Close()
  295. return
  296. }
  297. }
  298. conn.Close()
  299. }
  300. return
  301. }
  302. // RangeReplyZSetRds ...
  303. func (d *Dao) RangeReplyZSetRds(ctx context.Context, name string, oid int64, tp, start, end int) (rpIDs []int64, err error) {
  304. conn := d.redis.Get(ctx)
  305. defer conn.Close()
  306. key := keyReplyZSet(name, oid, tp)
  307. values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end))
  308. if err != nil {
  309. log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
  310. return
  311. }
  312. if len(values) == 0 {
  313. return
  314. }
  315. if err = redis.ScanSlice(values, &rpIDs); err != nil {
  316. log.Error("redis.ScanSlice(%v) error(%v)", values, err)
  317. return
  318. }
  319. return
  320. }
  321. // AddReplyZSetRds add a reply into redis sorted set, make sure expire the key first.
  322. func (d *Dao) AddReplyZSetRds(ctx context.Context, name string, oid int64, tp int, rs *model.ReplyScore) (err error) {
  323. conn := d.redis.Get(ctx)
  324. defer conn.Close()
  325. key := keyReplyZSet(name, oid, tp)
  326. if _, err = conn.Do("ZADD", key, rs.Score, rs.RpID); err != nil {
  327. log.Error("redis ZADD(%s, %f, %d) error(%v)", key, rs.Score, rs.RpID, err)
  328. }
  329. return
  330. }
  331. // RemReplyZSetRds remove one rpID from reply ZSet.
  332. func (d *Dao) RemReplyZSetRds(ctx context.Context, name string, oid int64, tp int, rpID int64) (err error) {
  333. conn := d.redis.Get(ctx)
  334. defer conn.Close()
  335. key := keyReplyZSet(name, oid, tp)
  336. if _, err = conn.Do("ZREM", key, rpID); err != nil {
  337. if err == redis.ErrNil {
  338. err = nil
  339. return
  340. }
  341. log.Error("redis ZREM(%s, %d) error(%v)", key, rpID, err)
  342. }
  343. return
  344. }
  345. // DelReplyZSetRds del a key from reply ZSet.
  346. func (d *Dao) DelReplyZSetRds(ctx context.Context, names []string, oid int64, tp int) (err error) {
  347. if len(names) < 1 {
  348. return
  349. }
  350. conn := d.redis.Get(ctx)
  351. defer conn.Close()
  352. count := 0
  353. for _, name := range names {
  354. key := keyReplyZSet(name, oid, tp)
  355. if err = conn.Send("DEL", key); err != nil {
  356. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  357. return
  358. }
  359. count++
  360. }
  361. if err = conn.Flush(); err != nil {
  362. log.Error("redis Flush error(%v)", err)
  363. return
  364. }
  365. for i := 0; i < count; i++ {
  366. if _, err = conn.Receive(); err != nil {
  367. log.Error("redis Receive error(%v)", err)
  368. return
  369. }
  370. }
  371. return
  372. }
  373. // CheckerTsRds get refresh checker timestamp from redis, if time.Now()-ts > strategy.time, then refresh reply list.
  374. func (d *Dao) CheckerTsRds(ctx context.Context, oid int64, tp int) (ts int64, err error) {
  375. conn := d.redis.Get(ctx)
  376. defer conn.Close()
  377. key := keyRefreshChecker(oid, tp)
  378. if ts, err = redis.Int64(conn.Do("GET", key)); err != nil {
  379. if err == redis.ErrNil {
  380. err = nil
  381. return
  382. }
  383. log.Error("redis GET(%s) error(%v)", key, err)
  384. }
  385. return
  386. }
  387. // SetCheckerTsRds set refresh checker's timestamp as time.Now(), call it when refresh reply list.
  388. func (d *Dao) SetCheckerTsRds(ctx context.Context, oid int64, tp int) (err error) {
  389. conn := d.redis.Get(ctx)
  390. defer conn.Close()
  391. key := keyRefreshChecker(oid, tp)
  392. if err = conn.Send("SETEX", key, d.redisRefreshExpire, time.Now().Unix()); err != nil {
  393. log.Error("redis SETEX(%s) error(%v)", key, err)
  394. }
  395. return
  396. }