memcached.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package like
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/activity/model/like"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _prefixInfo = "m_"
  11. )
  12. func keyInfo(sid int64) string {
  13. return fmt.Sprintf("%s%d", _prefixInfo, sid)
  14. }
  15. // SetInfoCache Dao
  16. func (dao *Dao) SetInfoCache(c context.Context, v *like.Subject, sid int64) (err error) {
  17. if v == nil {
  18. v = &like.Subject{}
  19. }
  20. var (
  21. conn = dao.mc.Get(c)
  22. mckey = keyInfo(sid)
  23. )
  24. defer conn.Close()
  25. if err = conn.Set(&memcache.Item{Key: mckey, Object: v, Flags: memcache.FlagGOB, Expiration: dao.mcLikeExpire}); err != nil {
  26. log.Error("conn.Set error(%v)", err)
  27. return
  28. }
  29. return
  30. }
  31. // InfoCache Dao
  32. func (dao *Dao) InfoCache(c context.Context, sid int64) (v *like.Subject, err error) {
  33. var (
  34. mckey = keyInfo(sid)
  35. conn = dao.mc.Get(c)
  36. item *memcache.Item
  37. )
  38. defer conn.Close()
  39. if item, err = conn.Get(mckey); err != nil {
  40. if err == memcache.ErrNotFound {
  41. err = nil
  42. v = nil
  43. } else {
  44. log.Error("conn.Get error(%v)", err)
  45. }
  46. return
  47. }
  48. if err = conn.Scan(item, &v); err != nil {
  49. log.Error("item.Scan error(%v)", err)
  50. return
  51. }
  52. return
  53. }