multi_template.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package main
  2. import (
  3. "strings"
  4. )
  5. var _multiGetTemplate = `
  6. // NAME {{or .Comment "get data from mc"}}
  7. func (d *Dao) NAME(c context.Context, ids []KEY {{.ExtraArgsType}}) (res map[KEY]VALUE, err error) {
  8. l := len(ids)
  9. if l == 0 {
  10. return
  11. }
  12. {{if .EnableBatch}}
  13. mutex := sync.Mutex{}
  14. for i:=0;i < l; i+= GROUPSIZE * MAXGROUP {
  15. var subKeys []KEY
  16. {{if .BatchErrBreak}}
  17. group, ctx := errgroup.WithContext(c)
  18. {{else}}
  19. group := &errgroup.Group{}
  20. ctx := c
  21. {{end}}
  22. if (i + GROUPSIZE * MAXGROUP) > l {
  23. subKeys = ids[i:]
  24. } else {
  25. subKeys = ids[i : i+GROUPSIZE * MAXGROUP]
  26. }
  27. subLen := len(subKeys)
  28. for j:=0; j< subLen; j += GROUPSIZE {
  29. var ks []KEY
  30. if (j+GROUPSIZE) > subLen {
  31. ks = subKeys[j:]
  32. } else {
  33. ks = subKeys[j:j+GROUPSIZE]
  34. }
  35. group.Go(func() (err error) {
  36. keysMap := make(map[string]KEY, len(ks))
  37. keys := make([]string, 0, len(ks))
  38. for _, id := range ks {
  39. key := {{.KeyMethod}}(id{{.ExtraArgs}})
  40. keysMap[key] = id
  41. keys = append(keys, key)
  42. }
  43. conn := d.mc.Get(ctx)
  44. defer conn.Close()
  45. replies, err := conn.GetMulti(keys)
  46. if err != nil {
  47. prom.BusinessErrCount.Incr("mc:NAME")
  48. log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("keys", keys))
  49. return
  50. }
  51. for key, reply := range replies {
  52. {{if .GetSimpleValue}}
  53. var v string
  54. err = conn.Scan(reply, &v)
  55. {{else}}
  56. var v VALUE
  57. {{if .GetDirectValue}}
  58. err = conn.Scan(reply, &v)
  59. {{else}}
  60. {{if .InitValue}}
  61. v = &{{.OriginValueType}}{}
  62. err = conn.Scan(reply, res)
  63. {{else}}
  64. v = {{.OriginValueType}}{}
  65. err = conn.Scan(reply, &res)
  66. {{end}}
  67. {{end}}
  68. {{end}}
  69. if err != nil {
  70. prom.BusinessErrCount.Incr("mc:NAME")
  71. log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  72. return
  73. }
  74. {{if .GetSimpleValue}}
  75. r, err := {{.ConvertBytes2Value}}
  76. if err != nil {
  77. prom.BusinessErrCount.Incr("mc:NAME")
  78. log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  79. return res, err
  80. }
  81. mutex.Lock()
  82. if res == nil {
  83. res = make(map[KEY]VALUE, len(keys))
  84. }
  85. res[keysMap[key]] = {{.ValueType}}(r)
  86. mutex.Unlock()
  87. {{else}}
  88. mutex.Lock()
  89. if res == nil {
  90. res = make(map[KEY]VALUE, len(keys))
  91. }
  92. res[keysMap[key]] = v
  93. mutex.Unlock()
  94. {{end}}
  95. }
  96. return
  97. })
  98. }
  99. err1 := group.Wait()
  100. if err1 != nil {
  101. err = err1
  102. {{if .BatchErrBreak}}
  103. break
  104. {{end}}
  105. }
  106. }
  107. {{else}}
  108. keysMap := make(map[string]KEY, l)
  109. keys := make([]string, 0, l)
  110. for _, id := range ids {
  111. key := {{.KeyMethod}}(id{{.ExtraArgs}})
  112. keysMap[key] = id
  113. keys = append(keys, key)
  114. }
  115. conn := d.mc.Get(c)
  116. defer conn.Close()
  117. replies, err := conn.GetMulti(keys)
  118. if err != nil {
  119. prom.BusinessErrCount.Incr("mc:NAME")
  120. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("keys", keys))
  121. return
  122. }
  123. for key, reply := range replies {
  124. {{if .GetSimpleValue}}
  125. var v string
  126. err = conn.Scan(reply, &v)
  127. {{else}}
  128. var v VALUE
  129. {{if .InitValue}}
  130. v = &{{.OriginValueType}}{}
  131. err = conn.Scan(reply, v)
  132. {{else}}
  133. v = {{.OriginValueType}}{}
  134. err = conn.Scan(reply, &v)
  135. {{end}}
  136. {{end}}
  137. if err != nil {
  138. prom.BusinessErrCount.Incr("mc:NAME")
  139. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  140. return
  141. }
  142. {{if .GetSimpleValue}}
  143. r, err := {{.ConvertBytes2Value}}
  144. if err != nil {
  145. prom.BusinessErrCount.Incr("mc:NAME")
  146. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  147. return res, err
  148. }
  149. if res == nil {
  150. res = make(map[KEY]VALUE, len(keys))
  151. }
  152. res[keysMap[key]] = {{.ValueType}}(r)
  153. {{else}}
  154. if res == nil {
  155. res = make(map[KEY]VALUE, len(keys))
  156. }
  157. res[keysMap[key]] = v
  158. {{end}}
  159. }
  160. {{end}}
  161. return
  162. }
  163. `
  164. var _multiSetTemplate = `
  165. // NAME {{or .Comment "Set data to mc"}}
  166. func (d *Dao) NAME(c context.Context, values map[KEY]VALUE {{.ExtraArgsType}}) (err error) {
  167. if len(values) == 0 {
  168. return
  169. }
  170. conn := d.mc.Get(c)
  171. defer conn.Close()
  172. for id, val := range values {
  173. key := {{.KeyMethod}}(id{{.ExtraArgs}})
  174. {{if .SimpleValue}}
  175. bs := {{.ConvertValue2Bytes}}
  176. item := &memcache.Item{Key: key, Value: bs, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}}
  177. {{else}}
  178. item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}}
  179. {{end}}
  180. if err = conn.Set(item); err != nil {
  181. prom.BusinessErrCount.Incr("mc:NAME")
  182. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  183. return
  184. }
  185. }
  186. return
  187. }
  188. `
  189. var _multiAddTemplate = strings.Replace(_multiSetTemplate, "Set", "Add", -1)
  190. var _multiReplaceTemplate = strings.Replace(_multiSetTemplate, "Set", "Replace", -1)
  191. var _multiDelTemplate = `
  192. // NAME {{or .Comment "delete data from mc"}}
  193. func (d *Dao) NAME(c context.Context, ids []KEY {{.ExtraArgsType}}) (err error) {
  194. if len(ids) == 0 {
  195. return
  196. }
  197. conn := d.mc.Get(c)
  198. defer conn.Close()
  199. for _, id := range ids {
  200. key := {{.KeyMethod}}(id{{.ExtraArgs}})
  201. if err = conn.Delete(key); err != nil {
  202. if err == memcache.ErrNotFound {
  203. err = nil
  204. continue
  205. }
  206. prom.BusinessErrCount.Incr("mc:NAME")
  207. log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
  208. return
  209. }
  210. }
  211. return
  212. }
  213. `