mc.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/memcache"
  6. "github.com/pkg/errors"
  7. )
  8. func userKey(mid int64) (key string) {
  9. key = fmt.Sprintf("u_%d", mid)
  10. return
  11. }
  12. func syncBlockTypeID() (key string) {
  13. return "sync_bt_cur_id"
  14. }
  15. func (d *Dao) SetSyncBlockTypeID(c context.Context, id int64) (err error) {
  16. var (
  17. key = syncBlockTypeID()
  18. conn = d.mc.Get(c)
  19. )
  20. defer conn.Close()
  21. if err = conn.Set(&memcache.Item{Key: key, Object: id, Expiration: 3600 * 24, Flags: memcache.FlagJSON}); err != nil {
  22. err = errors.WithStack(err)
  23. return
  24. }
  25. return
  26. }
  27. func (d *Dao) SyncBlockTypeID(c context.Context) (id int64, err error) {
  28. var (
  29. key = syncBlockTypeID()
  30. conn = d.mc.Get(c)
  31. item *memcache.Item
  32. )
  33. defer conn.Close()
  34. if item, err = conn.Get(key); err != nil {
  35. if err == memcache.ErrNotFound {
  36. err = nil
  37. return
  38. }
  39. err = errors.WithStack(err)
  40. return
  41. }
  42. if err = conn.Scan(item, &id); err != nil {
  43. err = errors.WithStack(err)
  44. return
  45. }
  46. return
  47. }
  48. func (d *Dao) DeleteUserCache(c context.Context, mid int64) (err error) {
  49. var (
  50. key = userKey(mid)
  51. conn = d.mc.Get(c)
  52. )
  53. defer conn.Close()
  54. if err = conn.Delete(key); err != nil {
  55. err = errors.WithStack(err)
  56. return
  57. }
  58. return
  59. }