redis.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package dede
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/service/main/videoup/model/dede"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _prefixPadInfo = "padinfo"
  11. )
  12. // PopPadInfoCache get padinfo from redis
  13. func (d *Dao) PopPadInfoCache(c context.Context) (pad *dede.PadInfo, err error) {
  14. var (
  15. conn = d.redis.Get(c)
  16. bs []byte
  17. )
  18. defer conn.Close()
  19. if bs, err = redis.Bytes(conn.Do("LPOP", _prefixPadInfo)); err != nil {
  20. if err == redis.ErrNil {
  21. err = nil
  22. } else {
  23. log.Error("conn.Do(LPOP, %s) error(%v)", _prefixPadInfo, err)
  24. }
  25. return
  26. }
  27. pad = &dede.PadInfo{}
  28. if err = json.Unmarshal(bs, pad); err != nil {
  29. log.Error("s.padproc json.Unmarshal error(%v)", err)
  30. }
  31. return
  32. }
  33. // PushPadCache add padinfo into redis.
  34. func (d *Dao) PushPadCache(c context.Context, pad *dede.PadInfo) (err error) {
  35. var (
  36. bs []byte
  37. conn = d.redis.Get(c)
  38. )
  39. defer conn.Close()
  40. if bs, err = json.Marshal(pad); err != nil {
  41. log.Error("json.Marshal(%s) error(%v)", bs, err)
  42. return
  43. }
  44. if _, err = conn.Do("RPUSH", _prefixPadInfo, bs); err != nil {
  45. log.Error("conn.Do(RPUSH, %s) error(%v)", bs, err)
  46. }
  47. return
  48. }