dao.cache.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
  2. /*
  3. Package dao is a generated cache proxy package.
  4. It is generated from:
  5. type _cache interface {
  6. // cache: -batch=50 -batch_err=break -nullcache=&v1.SysMsg{Id:0} -check_null_code=$==nil||$.Id==0
  7. SysMsg(c context.Context, ids []int64) (map[int64]*v1.SysMsg, error)
  8. }
  9. */
  10. package dao
  11. import (
  12. "context"
  13. "go-common/app/service/bbq/sys-msg/api/v1"
  14. "go-common/library/net/metadata"
  15. "go-common/library/stat/prom"
  16. )
  17. var _ _cache
  18. // SysMsg get data from cache if miss will call source method, then add to cache.
  19. func (d *Dao) SysMsg(c context.Context, keys []int64) (res map[int64]*v1.SysMsg, err error) {
  20. if len(keys) == 0 {
  21. return
  22. }
  23. addCache := true
  24. res, err = d.CacheSysMsg(c, keys)
  25. if err != nil {
  26. addCache = false
  27. res = nil
  28. err = nil
  29. }
  30. var miss []int64
  31. for _, key := range keys {
  32. if (res == nil) || (res[key] == nil) {
  33. miss = append(miss, key)
  34. }
  35. }
  36. prom.CacheHit.Add("SysMsg", int64(len(keys)-len(miss)))
  37. defer func() {
  38. for k, v := range res {
  39. if v == nil || v.Id == 0 {
  40. delete(res, k)
  41. }
  42. }
  43. }()
  44. if len(miss) == 0 {
  45. return
  46. }
  47. var missData map[int64]*v1.SysMsg
  48. prom.CacheMiss.Add("SysMsg", int64(len(miss)))
  49. missData, err = d.RawSysMsg(c, miss)
  50. if res == nil {
  51. res = make(map[int64]*v1.SysMsg)
  52. }
  53. for k, v := range missData {
  54. res[k] = v
  55. }
  56. if err != nil {
  57. return
  58. }
  59. for _, key := range keys {
  60. if res[key] == nil {
  61. if missData == nil {
  62. missData = make(map[int64]*v1.SysMsg, len(keys))
  63. }
  64. missData[key] = &v1.SysMsg{Id: 0}
  65. }
  66. }
  67. if !addCache {
  68. return
  69. }
  70. d.cache.Save(func() {
  71. d.AddCacheSysMsg(metadata.WithContext(c), missData)
  72. })
  73. return
  74. }