memcached.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package sports
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _preQq = "q_"
  11. )
  12. func keyQq(tp int64) string {
  13. return fmt.Sprintf("%s%d", _preQq, tp)
  14. }
  15. // QqCache get qq from cache
  16. func (d *Dao) QqCache(c context.Context, tp int64) (rs *json.RawMessage, err error) {
  17. var (
  18. mckey = keyQq(tp)
  19. conn = d.mc.Get(c)
  20. item *memcache.Item
  21. )
  22. defer conn.Close()
  23. if item, err = conn.Get(mckey); err != nil {
  24. if err == memcache.ErrNotFound {
  25. err = nil
  26. } else {
  27. log.Error("conn.Get error(%v)", err)
  28. }
  29. return
  30. }
  31. if err = conn.Scan(item, &rs); err != nil {
  32. log.Error("item.Scan error(%v)", err)
  33. }
  34. return
  35. }
  36. // SetQqCache set qq to cache
  37. func (d *Dao) SetQqCache(c context.Context, v *json.RawMessage, tp int64) (err error) {
  38. var (
  39. conn = d.mc.Get(c)
  40. mckey = keyQq(tp)
  41. )
  42. defer conn.Close()
  43. if err = conn.Set(&memcache.Item{Key: mckey, Object: v, Flags: memcache.FlagJSON, Expiration: d.mcQqExpire}); err != nil {
  44. log.Error("conn.Set error(%v)", err)
  45. return
  46. }
  47. return
  48. }