redis.go 1.1 KB

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