mc_special.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/dm2/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _fmtSpecialDm = "s_special_%d_%d"
  11. )
  12. func (d *Dao) specialDmKey(oid int64, tp int32) string {
  13. return fmt.Sprintf(_fmtSpecialDm, oid, tp)
  14. }
  15. // DelSpecialDmCache .
  16. func (d *Dao) DelSpecialDmCache(c context.Context, oid int64, tp int32) (err error) {
  17. var (
  18. key = d.specialDmKey(oid, tp)
  19. conn = d.dmSegMC.Get(c)
  20. )
  21. defer conn.Close()
  22. if err = conn.Delete(key); err != nil {
  23. if err == memcache.ErrNotFound {
  24. err = nil
  25. } else {
  26. log.Error("memcache.Delete(%s) error(%v)", key, err)
  27. }
  28. }
  29. return
  30. }
  31. // AddSpecialDmCache add special content to memcache.
  32. func (d *Dao) AddSpecialDmCache(c context.Context, ds *model.DmSpecial) (err error) {
  33. conn := d.dmSegMC.Get(c)
  34. key := d.specialDmKey(ds.Oid, ds.Type)
  35. defer conn.Close()
  36. item := &memcache.Item{
  37. Key: key,
  38. Object: ds,
  39. Flags: memcache.FlagJSON,
  40. Expiration: d.dmSegMCExpire,
  41. }
  42. if err = conn.Set(item); err != nil {
  43. log.Error("conn.Set(%s) error(%v)", key, err)
  44. }
  45. return
  46. }