cache.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package card
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/app-show/model/card"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _hotTenprefix = "%d_hclist_v2"
  11. )
  12. func getHotKey(i int) string {
  13. return fmt.Sprintf(_hotTenprefix, i)
  14. }
  15. // AddPopularCardTenCache add mc list
  16. func (d *Dao) AddPopularCardTenCache(c context.Context, i int, cards []*card.PopularCard) (err error) {
  17. var (
  18. key = getHotKey(i)
  19. conn = d.mc.Get(c)
  20. )
  21. if err = conn.Set(&memcache.Item{Key: key, Object: cards, Flags: memcache.FlagJSON, Expiration: 0}); err != nil {
  22. log.Error("addCards d.mc.Set(%s,%v) error(%v)", key, cards, err)
  23. }
  24. conn.Close()
  25. return
  26. }
  27. // PopularCardTenCache get cards list
  28. func (d *Dao) PopularCardTenCache(c context.Context, i int) (cards []*card.PopularCard, err error) {
  29. var (
  30. key = getHotKey(i)
  31. conn = d.mc.Get(c)
  32. r *memcache.Item
  33. )
  34. defer conn.Close()
  35. if r, err = conn.Get(key); err != nil {
  36. log.Error("cardsCache MemchDB.Get(%s) error(%v)", key, err)
  37. return
  38. }
  39. if err = conn.Scan(r, &cards); err != nil {
  40. log.Error("r.Scan(%s) error(%v)", r.Value, err)
  41. }
  42. return
  43. }