memcahe.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package mcndao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/mcn/model/mcnmodel"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. )
  10. //go:generate $GOPATH/src/go-common/app/tool/cache/gen
  11. type _cache interface {
  12. //cache: -nullcache=&mcnmodel.McnSign{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  13. McnSign(c context.Context, mcnmid int64) (up *mcnmodel.McnSign, err error)
  14. //cache: -nullcache=&mcnmodel.McnGetDataSummaryReply{IsNull:true} -check_null_code=$!=nil&&$.IsNull
  15. McnDataSummary(c context.Context, mcnmid int64, generateDate time.Time) (res *mcnmodel.McnGetDataSummaryReply, err error)
  16. //cache: -nullcache=&mcnmodel.UpPermissionCache{IsNull:true} -check_null_code=$!=nil&&$.IsNull
  17. UpPermission(c context.Context, signID int64, mid int64) (data *mcnmodel.UpPermissionCache, err error)
  18. }
  19. //go:generate $GOPATH/src/go-common/app/tool/cache/mc
  20. type _mc interface {
  21. //mc: -key=mcnSignCacheKey -expire=d.mcnSignExpire -encode=json
  22. AddCacheMcnSign(c context.Context, mcnmid int64, up *mcnmodel.McnSign) (err error)
  23. //mc: -key=mcnSignCacheKey
  24. CacheMcnSign(c context.Context, mcnmid int64) (up *mcnmodel.McnSign, err error)
  25. //mc: -key=mcnSignCacheKey
  26. DelCacheMcnSign(c context.Context, mcnmid int64) (err error)
  27. //mc: -key=mcnDataCacheKey -expire=d.mcnDataExpire -encode=json
  28. AddCacheMcnDataSummary(c context.Context, mcnmid int64, data *mcnmodel.McnGetDataSummaryReply, generateDate time.Time) (err error)
  29. //mc: -key=mcnDataCacheKey
  30. CacheMcnDataSummary(c context.Context, mcnmid int64, generateDate time.Time) (data *mcnmodel.McnGetDataSummaryReply, err error)
  31. //mc: -key=mcnDataCacheKey
  32. DelMcnDataSummary(c context.Context, mcnmid int64, generateDate time.Time) (err error)
  33. //mc: -key=mcnPublicationPriceKey -expire=0 -encode=json
  34. AddCachePublicationPrice(c context.Context, signID int64, data *mcnmodel.PublicationPriceCache, mid int64) (err error)
  35. //mc: -key=mcnPublicationPriceKey
  36. CachePublicationPrice(c context.Context, signID int64, mid int64) (data *mcnmodel.PublicationPriceCache, err error)
  37. //mc: -key=mcnPublicationPriceKey
  38. DelPublicationPrice(c context.Context, signID int64, mid int64) (err error)
  39. //mc: -key=mcnUpPermissionKey -expire=d.mcnSignExpire -encode=json
  40. AddCacheUpPermission(c context.Context, signID int64, data *mcnmodel.UpPermissionCache, mid int64) (err error)
  41. //mc: -key=mcnUpPermissionKey
  42. CacheUpPermission(c context.Context, signID int64, mid int64) (data *mcnmodel.UpPermissionCache, err error)
  43. //mc: -key=mcnUpPermissionKey
  44. DelUpPermission(c context.Context, signID int64, mid int64) (err error)
  45. }
  46. func mcnSignCacheKey(mcnmid int64) string {
  47. return fmt.Sprintf("mcn_s_%d", mcnmid)
  48. }
  49. //RawMcnSign raw db .
  50. func (d *Dao) RawMcnSign(c context.Context, mcnmid int64) (up *mcnmodel.McnSign, err error) {
  51. up, _, err = d.GetMcnSignState("*", mcnmid)
  52. if err != nil {
  53. if err == gorm.ErrRecordNotFound {
  54. err = nil
  55. return
  56. }
  57. log.Error("error get state, err=%s", err)
  58. return
  59. }
  60. return
  61. }
  62. //AsyncDelCacheMcnSign delete in async way
  63. func (d *Dao) AsyncDelCacheMcnSign(id int64) (err error) {
  64. return d.cache.Do(context.Background(), func(c context.Context) {
  65. d.DelCacheMcnSign(c, id)
  66. })
  67. }
  68. func mcnDataCacheKey(signID int64, generateDate time.Time) string {
  69. var key = fmt.Sprintf("mcn_data_%d_%s", signID, generateDate.Format(dateFmt))
  70. return key
  71. }
  72. //RawMcnDataSummary raw get
  73. func (d *Dao) RawMcnDataSummary(c context.Context, signID int64, generateDate time.Time) (res *mcnmodel.McnGetDataSummaryReply, err error) {
  74. return d.GetMcnDataSummaryWithDiff(signID, mcnmodel.McnDataTypeDay, generateDate)
  75. }
  76. func mcnPublicationPriceKey(signID int64, mid int64) string {
  77. return fmt.Sprintf("mcn_pubprice_%d_%d", signID, mid)
  78. }
  79. func mcnUpPermissionKey(signID int64, mid int64) string {
  80. var s = fmt.Sprintf("mcn_upperm_%d_%d", signID, mid)
  81. log.Info("key=%s", s)
  82. return s
  83. }
  84. //RawUpPermission get permissino from db
  85. func (d *Dao) RawUpPermission(c context.Context, signID int64, mid int64) (res *mcnmodel.UpPermissionCache, err error) {
  86. upList, err := d.GetUpBind("up_mid=? and sign_id=? and state in (?)", mid, signID, UpSignedStates)
  87. if err != nil {
  88. log.Error("get up from db fail, err=%v, signid=%d, mid=%d", err, signID, mid)
  89. return
  90. }
  91. if len(upList) == 0 {
  92. log.Warn("up not found, sign_id=%d, mid=%d", signID, mid)
  93. return
  94. }
  95. res = new(mcnmodel.UpPermissionCache)
  96. res.Permission = upList[0].Permission
  97. return
  98. }