cache.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package datadao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/interface/main/mcn/conf"
  7. "go-common/app/interface/main/mcn/model/mcnmodel"
  8. "go-common/app/interface/main/mcn/tool/cache"
  9. "time"
  10. )
  11. var (
  12. dateFmt = "20060102"
  13. )
  14. //CacheBaseLoader base loader
  15. type CacheBaseLoader struct {
  16. SignID int64
  17. Date time.Time
  18. Val interface{}
  19. desc string
  20. }
  21. func descHelper(d cache.DataLoader) (key string) {
  22. return d.Desc()
  23. }
  24. func newCacheBaseLoader(signID int64, date time.Time, val interface{}, desc string) CacheBaseLoader {
  25. return CacheBaseLoader{SignID: signID, Date: date, Val: val, desc: desc}
  26. }
  27. //Key cache's key
  28. func (s *CacheBaseLoader) Key() (key string) {
  29. return fmt.Sprintf("%s_%d_%s", descHelper(s), s.SignID, s.Date.Format(dateFmt))
  30. }
  31. //Value cache's value
  32. func (s *CacheBaseLoader) Value() (value interface{}) {
  33. return s.Val
  34. }
  35. //LoadValue need load value
  36. func (s *CacheBaseLoader) LoadValue(c context.Context) (value interface{}, err error) {
  37. panic("implement me")
  38. }
  39. //Expire expiration
  40. func (s *CacheBaseLoader) Expire() time.Duration {
  41. return time.Duration(conf.Conf.Memcache.McnDataCacheExpire)
  42. }
  43. //Desc key desc
  44. func (s *CacheBaseLoader) Desc() string {
  45. return s.desc
  46. }
  47. // -----------------------------------------
  48. //LoadFuncWithTp sign with type
  49. type LoadFuncWithTp func(c context.Context, signID int64, date time.Time, tp string) (res interface{}, err error)
  50. //LoadFuncOnlySign only sign
  51. type LoadFuncOnlySign func(c context.Context, signID int64, date time.Time) (res interface{}, err error)
  52. //NewCacheMcnDataWithTp reply
  53. func NewCacheMcnDataWithTp(signID int64, date time.Time, tp string, val interface{}, desc string, loadFunc LoadFuncWithTp) *cacheMcnDataWithTp {
  54. return &cacheMcnDataWithTp{
  55. CacheBaseLoader: newCacheBaseLoader(signID, date, val, desc),
  56. Tp: tp,
  57. LoadFunc: loadFunc,
  58. }
  59. }
  60. //cacheMcnDataWithTp cache
  61. type cacheMcnDataWithTp struct {
  62. CacheBaseLoader
  63. Tp string
  64. LoadFunc LoadFuncWithTp
  65. }
  66. //Key key
  67. func (s *cacheMcnDataWithTp) Key() (key string) {
  68. return fmt.Sprintf("%s_%d_%s_%s", s.Desc(), s.SignID, s.Date.Format(dateFmt), s.Tp)
  69. }
  70. //LoadValue load
  71. func (s *cacheMcnDataWithTp) LoadValue(c context.Context) (value interface{}, err error) {
  72. value, err = s.LoadFunc(c, s.SignID, s.Date, s.Tp)
  73. if err != nil {
  74. s.Val = nil
  75. return
  76. }
  77. if sorter, ok := value.(mcnmodel.Sorter); ok {
  78. sorter.Sort()
  79. }
  80. // 如果s.Val存在,则将结果populate到s.Val上,因为外部会直接使用原始传入的s.Val值
  81. if s.Val != nil {
  82. var b, _ = json.Marshal(value)
  83. json.Unmarshal(b, s.Val)
  84. }
  85. return
  86. }
  87. // NewCacheMcnDataSignID 请求只有sign id的情况
  88. func NewCacheMcnDataSignID(signID int64, date time.Time, val interface{}, desc string, loadFunc LoadFuncOnlySign) *cacheMcnDataSignID {
  89. return &cacheMcnDataSignID{
  90. CacheBaseLoader: newCacheBaseLoader(signID, date, val, desc),
  91. LoadFunc: loadFunc,
  92. }
  93. }
  94. type cacheMcnDataSignID struct {
  95. CacheBaseLoader
  96. LoadFunc LoadFuncOnlySign
  97. }
  98. //Key key
  99. func (s *cacheMcnDataSignID) Key() (key string) {
  100. return fmt.Sprintf("%s_%d_%s", s.Desc(), s.SignID, s.Date.Format(dateFmt))
  101. }
  102. //LoadValue load
  103. func (s *cacheMcnDataSignID) LoadValue(c context.Context) (value interface{}, err error) {
  104. value, err = s.LoadFunc(c, s.SignID, s.Date)
  105. if err != nil {
  106. s.Val = nil
  107. return
  108. }
  109. if sorter, ok := value.(mcnmodel.Sorter); ok {
  110. sorter.Sort()
  111. }
  112. // 如果s.Val存在,则将结果populate到s.Val上,因为外部会直接使用原始传入的s.Val值
  113. if s.Val != nil {
  114. var b, _ = json.Marshal(value)
  115. json.Unmarshal(b, s.Val)
  116. }
  117. return
  118. }