memcache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/job/main/dm/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _prefixSub = "s_"
  12. _keyDuration = "d_" // video duration
  13. )
  14. func keySubject(tp int32, oid int64) string {
  15. return _prefixSub + fmt.Sprintf("%d_%d", tp, oid)
  16. }
  17. // keyDuration return video duration key.
  18. func keyDuration(oid int64) string {
  19. return _keyDuration + strconv.FormatInt(oid, 10)
  20. }
  21. // SubjectCache get subject from memcache.
  22. func (d *Dao) SubjectCache(c context.Context, tp int32, oid int64) (sub *model.Subject, err error) {
  23. var (
  24. conn = d.mc.Get(c)
  25. key = keySubject(tp, oid)
  26. rp *memcache.Item
  27. )
  28. defer conn.Close()
  29. if rp, err = conn.Get(key); err != nil {
  30. if err == memcache.ErrNotFound {
  31. err = nil
  32. } else {
  33. log.Error("mc.Get(%s) error(%v)", key, err)
  34. }
  35. return
  36. }
  37. sub = &model.Subject{}
  38. if err = conn.Scan(rp, &sub); err != nil {
  39. log.Error("conn.Scan() error(%v)", err)
  40. }
  41. return
  42. }
  43. // SetSubjectCache add subject cache.
  44. func (d *Dao) SetSubjectCache(c context.Context, sub *model.Subject) (err error) {
  45. var (
  46. conn = d.mc.Get(c)
  47. key = keySubject(sub.Type, sub.Oid)
  48. )
  49. defer conn.Close()
  50. item := &memcache.Item{
  51. Key: key,
  52. Object: sub,
  53. Flags: memcache.FlagJSON,
  54. Expiration: d.mcExpire,
  55. }
  56. if err = conn.Set(item); err != nil {
  57. log.Error("conn.Set(%v) error(%v)", item, err)
  58. }
  59. return
  60. }
  61. // DurationCache return duration of video.
  62. func (d *Dao) DurationCache(c context.Context, oid int64) (duration int64, err error) {
  63. var (
  64. key = keyDuration(oid)
  65. conn = d.mc.Get(c)
  66. item *memcache.Item
  67. )
  68. defer conn.Close()
  69. if item, err = conn.Get(key); err != nil {
  70. if err == memcache.ErrNotFound {
  71. duration = model.NotFound
  72. err = nil
  73. } else {
  74. log.Error("conn.Get(%s) error(%v)", key, err)
  75. }
  76. return
  77. }
  78. if duration, err = strconv.ParseInt(string(item.Value), 10, 64); err != nil {
  79. log.Error("strconv.ParseInt(%s) error(%v)", item.Value, err)
  80. }
  81. return
  82. }
  83. // SetDurationCache set video duration to redis.
  84. func (d *Dao) SetDurationCache(c context.Context, oid, duration int64) (err error) {
  85. key := keyDuration(oid)
  86. conn := d.mc.Get(c)
  87. item := memcache.Item{
  88. Key: key,
  89. Value: []byte(fmt.Sprint(duration)),
  90. Expiration: d.mcExpire,
  91. Flags: memcache.FlagRAW,
  92. }
  93. if err = conn.Set(&item); err != nil {
  94. log.Error("mc.Set(%v) error(%v)", item, err)
  95. }
  96. conn.Close()
  97. return
  98. }