redis.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package archive
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/interface/main/videoup/model/archive"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _upFavTpsPrefix = "up_fav_tps_"
  12. )
  13. func keyUpFavTpsPrefix(mid int64) string {
  14. return _upFavTpsPrefix + strconv.FormatInt(mid, 10)
  15. }
  16. // FilenameExpires get filename expire time.
  17. func (d *Dao) FilenameExpires(c context.Context, vs []*archive.VideoParam) (ves []*archive.VideoExpire, err error) {
  18. var conn = d.redis.Get(c)
  19. defer conn.Close()
  20. for _, v := range vs {
  21. conn.Send("GET", v.Filename)
  22. }
  23. if err = conn.Flush(); err != nil {
  24. log.Error("conn.Flush() error(%v) | vs(%#v)", err, vs)
  25. return
  26. }
  27. for _, v := range vs {
  28. var exp int64
  29. if exp, err = redis.Int64(conn.Receive()); err != nil && err != redis.ErrNil {
  30. log.Error("conn.Receive error(%+v) | filename(%s)", err, v.Filename)
  31. return
  32. }
  33. err = nil // NOTE: maybe err==redis.ErrNil
  34. ves = append(ves, &archive.VideoExpire{
  35. Filename: v.Filename,
  36. Expire: exp,
  37. })
  38. }
  39. return
  40. }
  41. // FreshFavTypes fn
  42. func (d *Dao) FreshFavTypes(c context.Context, mid int64, tp int) (err error) {
  43. var (
  44. conn = d.redis.Get(c)
  45. score = time.Now().Unix()
  46. )
  47. defer conn.Close()
  48. if err = conn.Send("ZADD", keyUpFavTpsPrefix(mid), score, strconv.Itoa(tp)); err != nil {
  49. log.Error("conn.Send(ZADD, %s, %d) error(%v)", _upFavTpsPrefix, tp, err)
  50. return
  51. }
  52. if err = conn.Flush(); err != nil {
  53. log.Error("conn.Flush error(%v)", err)
  54. return
  55. }
  56. if _, err = conn.Receive(); err != nil {
  57. log.Error("conn.Receive() error(%v)", err)
  58. }
  59. return
  60. }
  61. func (d *Dao) pingRedis(c context.Context) (err error) {
  62. conn := d.redis.Get(c)
  63. _, err = conn.Do("SET", "PING", "PONG")
  64. conn.Close()
  65. return
  66. }