dao.cache.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_err=break -nullcache=&v1.VideoBase{Svid:-1} -check_null_code=$==nil||$.Svid==-1
  7. VideoBase(c context.Context, svid []int64) (map[int64]*v1.VideoBase, error)
  8. }
  9. */
  10. package dao
  11. import (
  12. "context"
  13. "go-common/app/service/bbq/video/api/grpc/v1"
  14. "go-common/library/stat/prom"
  15. )
  16. var _ _cache
  17. // VideoBase get data from cache if miss will call source method, then add to cache.
  18. func (d *Dao) VideoBase(c context.Context, keys []int64) (res map[int64]*v1.VideoBase, err error) {
  19. if len(keys) == 0 {
  20. return
  21. }
  22. addCache := true
  23. if res, err = d.CacheVideoBase(c, keys); err != nil {
  24. addCache = false
  25. res = nil
  26. err = nil
  27. }
  28. var miss []int64
  29. for _, key := range keys {
  30. if (res == nil) || (res[key] == nil) {
  31. miss = append(miss, key)
  32. }
  33. }
  34. prom.CacheHit.Add("VideoBase", int64(len(keys)-len(miss)))
  35. for k, v := range res {
  36. if v == nil || v.Svid == -1 {
  37. delete(res, k)
  38. }
  39. }
  40. missLen := len(miss)
  41. if missLen == 0 {
  42. return
  43. }
  44. var missData map[int64]*v1.VideoBase
  45. prom.CacheMiss.Add("VideoBase", int64(len(miss)))
  46. missData, err = d.RawVideoBase(c, miss)
  47. if res == nil {
  48. res = make(map[int64]*v1.VideoBase, len(keys))
  49. }
  50. for k, v := range missData {
  51. res[k] = v
  52. }
  53. if err != nil {
  54. return
  55. }
  56. for _, key := range miss {
  57. if res[key] == nil {
  58. missData[key] = &v1.VideoBase{Svid: -1}
  59. }
  60. }
  61. if !addCache {
  62. return
  63. }
  64. d.cache.Do(c, func(c context.Context) {
  65. d.AddCacheVideoBase(c, missData)
  66. })
  67. return
  68. }