redis.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package offer
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/library/cache/redis"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _failList = "act_list"
  10. )
  11. func keyRetry() string {
  12. return _failList
  13. }
  14. // PushFail rpush fail item to redis
  15. func (d *Dao) PushFail(c context.Context, a interface{}) (err error) {
  16. var bs []byte
  17. conn := d.redis.Get(c)
  18. key := keyRetry()
  19. defer conn.Close()
  20. if bs, err = json.Marshal(a); err != nil {
  21. err = errors.Wrapf(err, "%v", a)
  22. return
  23. }
  24. if _, err = conn.Do("RPUSH", key, bs); err != nil {
  25. err = errors.Wrapf(err, "conn.Do(RPUSH,%s,%s)", key, bs)
  26. }
  27. return
  28. }
  29. // PopFail lpop fail item from redis
  30. func (d *Dao) PopFail(c context.Context) (bs []byte, err error) {
  31. conn := d.redis.Get(c)
  32. key := keyRetry()
  33. if bs, err = redis.Bytes(conn.Do("LPOP", key)); err != nil {
  34. if err == redis.ErrNil {
  35. err = nil
  36. } else {
  37. err = errors.Wrapf(err, "redis.Bytes(conn.Do(LPOP, %s))", key)
  38. }
  39. }
  40. conn.Close()
  41. return
  42. }
  43. func (d *Dao) PingRedis(c context.Context) (err error) {
  44. var conn = d.redis.Get(c)
  45. _, err = conn.Do("SET", "PING", "PONG")
  46. conn.Close()
  47. return
  48. }