none_template.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. var _noneTemplate = `
  3. // NAME {{or .Comment "get data from cache if miss will call source method, then add to cache."}}
  4. func (d *Dao) NAME(c context.Context) (res VALUE, err error) {
  5. addCache := true
  6. res, err = CACHEFUNC(c)
  7. if err != nil {
  8. addCache = false
  9. err = nil
  10. }
  11. {{if .EnableNullCache}}
  12. defer func() {
  13. {{if .SimpleValue}} if res == {{.NullCache}} { {{else}} if {{.CheckNullCode}} { {{end}}
  14. res = {{.ZeroValue}}
  15. }
  16. }()
  17. {{end}}
  18. {{if .GoValue}}
  19. if len(res) != 0 {
  20. {{else}}
  21. if res != {{.ZeroValue}} {
  22. {{end}}
  23. prom.CacheHit.Incr("NAME")
  24. return
  25. }
  26. {{if .EnableSingleFlight}}
  27. var rr interface{}
  28. sf := d.cacheSFNAME()
  29. rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) {
  30. prom.CacheMiss.Incr("NAME")
  31. r, e = RAWFUNC(c)
  32. return
  33. })
  34. res = rr.(VALUE)
  35. {{else}}
  36. prom.CacheMiss.Incr("NAME")
  37. res, err = RAWFUNC(c)
  38. {{end}}
  39. if err != nil {
  40. return
  41. }
  42. var miss = res
  43. {{if .EnableNullCache}}
  44. {{if .GoValue}}
  45. if len(miss) == 0 {
  46. {{else}}
  47. if miss == {{.ZeroValue}} {
  48. {{end}}
  49. miss = {{.NullCache}}
  50. }
  51. {{end}}
  52. if !addCache {
  53. return
  54. }
  55. {{if .Sync}}
  56. ADDCACHEFUNC(c, miss)
  57. {{else}}
  58. d.cache.Do(c, func(c context.Context) {
  59. ADDCACHEFUNC(c, miss)
  60. })
  61. {{end}}
  62. return
  63. }
  64. `