dao.cache.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
  2. /*
  3. Package archive is a generated cache proxy package.
  4. It is generated from:
  5. type _cache interface {
  6. // cache: -singleflight=true -nullcache=[]*archive.Staff{{ID:-1}} -check_null_code=len($)==1&&$[0].ID==-1
  7. StaffData(c context.Context, aid int64) ([]*archive.Staff, error)
  8. }
  9. */
  10. package archive
  11. import (
  12. "context"
  13. "go-common/app/service/main/videoup/model/archive"
  14. "go-common/library/net/metadata"
  15. "go-common/library/stat/prom"
  16. "golang.org/x/sync/singleflight"
  17. )
  18. var _ _cache
  19. var cacheSingleFlights = [1]*singleflight.Group{{}}
  20. // StaffData get data from cache if miss will call source method, then add to cache.
  21. func (d *Dao) StaffData(c context.Context, id int64) (res []*archive.Staff, err error) {
  22. addCache := true
  23. res, err = d.CacheStaffData(c, id)
  24. if err != nil {
  25. addCache = false
  26. err = nil
  27. }
  28. defer func() {
  29. if len(res) == 1 && res[0].ID == -1 {
  30. res = nil
  31. }
  32. }()
  33. if len(res) != 0 {
  34. prom.CacheHit.Incr("StaffData")
  35. return
  36. }
  37. var rr interface{}
  38. sf := d.cacheSFStaffData(id)
  39. rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
  40. prom.CacheMiss.Incr("StaffData")
  41. r, e = d.Staffs(c, id)
  42. return
  43. })
  44. res = rr.([]*archive.Staff)
  45. if err != nil {
  46. return
  47. }
  48. miss := res
  49. if len(miss) == 0 {
  50. miss = []*archive.Staff{{ID: -1}}
  51. }
  52. if !addCache {
  53. return
  54. }
  55. d.cache.Do(c, func(ctx context.Context) {
  56. d.AddCacheStaffData(metadata.WithContext(c), id, miss)
  57. })
  58. return
  59. }