act.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package like
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _redDotKeyFmt = "rd_%d"
  10. )
  11. func keyRedDot(mid int64) string {
  12. return fmt.Sprintf(_redDotKeyFmt, mid)
  13. }
  14. // CacheRedDotTs .
  15. func (d *Dao) CacheRedDotTs(c context.Context, mid int64) (ts int64, err error) {
  16. key := keyRedDot(mid)
  17. conn := d.redis.Get(c)
  18. defer conn.Close()
  19. if ts, err = redis.Int64(conn.Do("GET", key)); err != nil {
  20. if err == redis.ErrNil {
  21. err = nil
  22. } else {
  23. err = errors.Wrapf(err, "conn.Do(GET, %s)", key)
  24. }
  25. }
  26. return
  27. }
  28. // AddCacheRedDotTs .
  29. func (d *Dao) AddCacheRedDotTs(c context.Context, mid, ts int64) (err error) {
  30. key := keyRedDot(mid)
  31. conn := d.redis.Get(c)
  32. defer conn.Close()
  33. if err = conn.Send("SET", key, ts); err != nil {
  34. err = errors.Wrapf(err, "conn.Send(SET, %s, %d)", key, ts)
  35. return
  36. }
  37. if err = conn.Send("EXPIRE", key, d.hotDotExpire); err != nil {
  38. err = errors.Wrapf(err, "conn.Send(EXPIRE, %s, %d)", key, ts)
  39. return
  40. }
  41. if err = conn.Flush(); err != nil {
  42. err = errors.Wrap(err, "AddCacheHotDotTs conn.Flush")
  43. return
  44. }
  45. for i := 0; i < 2; i++ {
  46. if _, err = conn.Receive(); err != nil {
  47. err = errors.Wrapf(err, "add conn.Receive(%d)", i+1)
  48. return
  49. }
  50. }
  51. return
  52. }