single_template.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "strings"
  4. )
  5. var _singleGetTemplate = `
  6. // NAME {{or .Comment "get data from mc"}}
  7. func (d *Dao) NAME(c context.Context, id KEY {{.ExtraArgsType}}) (res VALUE, err error) {
  8. conn := d.mc.Get(c)
  9. defer conn.Close()
  10. key := {{.KeyMethod}}(id{{.ExtraArgs}})
  11. reply, err := conn.Get(key)
  12. if err != nil {
  13. if err == memcache.ErrNotFound {
  14. err = nil
  15. return
  16. }
  17. prom.BusinessErrCount.Incr("mc:NAME")
  18. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  19. return
  20. }
  21. {{if .GetSimpleValue}}
  22. var v string
  23. err = conn.Scan(reply, &v)
  24. {{else}}
  25. {{if .GetDirectValue}}
  26. err = conn.Scan(reply, &res)
  27. {{else}}
  28. {{if .InitValue}}
  29. res = &{{.OriginValueType}}{}
  30. err = conn.Scan(reply, res)
  31. {{else}}
  32. res = {{.OriginValueType}}{}
  33. err = conn.Scan(reply, &res)
  34. {{end}}
  35. {{end}}
  36. {{end}}
  37. if err != nil {
  38. prom.BusinessErrCount.Incr("mc:NAME")
  39. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  40. return
  41. }
  42. {{if .GetSimpleValue}}
  43. r, err := {{.ConvertBytes2Value}}
  44. if err != nil {
  45. prom.BusinessErrCount.Incr("mc:NAME")
  46. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  47. return
  48. }
  49. res = {{.ValueType}}(r)
  50. {{end}}
  51. return
  52. }
  53. `
  54. var _singleSetTemplate = `
  55. // NAME {{or .Comment "Set data to mc"}}
  56. func (d *Dao) NAME(c context.Context, id KEY, val VALUE {{.ExtraArgsType}}) (err error) {
  57. {{if .PointType}}
  58. if val == nil {
  59. return
  60. }
  61. {{end}}
  62. {{if .LenType}}
  63. if len(val) == 0 {
  64. return
  65. }
  66. {{end}}
  67. conn := d.mc.Get(c)
  68. defer conn.Close()
  69. key := {{.KeyMethod}}(id{{.ExtraArgs}})
  70. {{if .SimpleValue}}
  71. bs := {{.ConvertValue2Bytes}}
  72. item := &memcache.Item{Key: key, Value: bs, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}}
  73. {{else}}
  74. item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}}
  75. {{end}}
  76. if err = conn.Set(item); err != nil {
  77. prom.BusinessErrCount.Incr("mc:NAME")
  78. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  79. return
  80. }
  81. return
  82. }
  83. `
  84. var _singleAddTemplate = strings.Replace(_singleSetTemplate, "Set", "Add", -1)
  85. var _singleReplaceTemplate = strings.Replace(_singleSetTemplate, "Set", "Replace", -1)
  86. var _singleDelTemplate = `
  87. // NAME {{or .Comment "delete data from mc"}}
  88. func (d *Dao) NAME(c context.Context, id KEY {{.ExtraArgsType}}) (err error) {
  89. conn := d.mc.Get(c)
  90. defer conn.Close()
  91. key := {{.KeyMethod}}(id{{.ExtraArgs}})
  92. if err = conn.Delete(key); err != nil {
  93. if err == memcache.ErrNotFound {
  94. err = nil
  95. return
  96. }
  97. prom.BusinessErrCount.Incr("mc:NAME")
  98. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  99. return
  100. }
  101. return
  102. }
  103. `