memcache.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package archive
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/app-player/model/archive"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _prefixArc = "p_"
  12. )
  13. func keyArc(aid int64) string {
  14. return _prefixArc + strconv.FormatInt(aid, 10)
  15. }
  16. func (d *Dao) archiveCache(c context.Context, aid int64) (arcMc *archive.Info, err error) {
  17. conn := d.arcMc.Get(c)
  18. key := keyArc(aid)
  19. defer conn.Close()
  20. r, err := conn.Get(key)
  21. if err != nil {
  22. if err == memcache.ErrNotFound {
  23. err = nil
  24. return
  25. }
  26. err = errors.Wrapf(err, "conn.Get(%s)", key)
  27. return
  28. }
  29. arcMc = &archive.Info{}
  30. if err = conn.Scan(r, arcMc); err != nil {
  31. err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
  32. }
  33. return
  34. }
  35. // addArchiveCache add archive cache.
  36. func (d *Dao) addArchiveCache(c context.Context, aid int64, arc *archive.Info) (err error) {
  37. conn := d.arcMc.Get(c)
  38. key := keyArc(aid)
  39. item := &memcache.Item{Key: key, Object: arc, Flags: memcache.FlagProtobuf}
  40. if err = conn.Set(item); err != nil {
  41. log.Error("conn.Set(%s, %v) error(%v)", key, arc, err)
  42. }
  43. conn.Close()
  44. return
  45. }
  46. func (d *Dao) pingMC(c context.Context) (err error) {
  47. conn := d.arcMc.Get(c)
  48. err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Flags: memcache.FlagRAW, Expiration: 0})
  49. conn.Close()
  50. return
  51. }