cache.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package card
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _channelCardKey = "ccmd5_buvid_%v_%d"
  11. )
  12. func channelCardKey(buvid string, channelID int64) string {
  13. return fmt.Sprintf(_channelCardKey, buvid, channelID)
  14. }
  15. // AddChannelCardCache add user buvid and cardID cache
  16. func (d *Dao) AddChannelCardCache(c context.Context, buvid, md5 string, channelID int64, now time.Time) (err error) {
  17. var (
  18. key = channelCardKey(buvid, channelID)
  19. conn = d.mc.Get(c)
  20. currenttimeSec = int32((now.Hour() * 60 * 60) + (now.Minute() * 60) + now.Second())
  21. overtime int32
  22. )
  23. if overtime = d.expire - currenttimeSec; overtime < 1 {
  24. overtime = d.expire
  25. }
  26. if err = conn.Set(&memcache.Item{Key: key, Object: md5, Flags: memcache.FlagJSON, Expiration: overtime}); err != nil {
  27. log.Error("AddChannelCardCache d.mc.Set(%s,%v) error(%v)", key, channelID, err)
  28. }
  29. conn.Close()
  30. return
  31. }
  32. // ChannelCardCache user buvid channel card
  33. func (d *Dao) ChannelCardCache(c context.Context, buvid string, channelID int64) (md5 string, err error) {
  34. var (
  35. key = channelCardKey(buvid, channelID)
  36. conn = d.mc.Get(c)
  37. r *memcache.Item
  38. )
  39. defer conn.Close()
  40. if r, err = conn.Get(key); err != nil {
  41. return
  42. }
  43. if err = conn.Scan(r, &md5); err != nil {
  44. log.Error("r.Scan(%s) error(%v)", r.Value, err)
  45. }
  46. return
  47. }