dao.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. arcGRPC "go-common/app/service/main/archive/api"
  6. "go-common/app/service/main/ugcpay/conf"
  7. "go-common/app/service/main/ugcpay/model"
  8. "go-common/library/cache"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/cache/redis"
  11. xsql "go-common/library/database/sql"
  12. )
  13. // Dao dao
  14. type Dao struct {
  15. c *conf.Config
  16. mc *memcache.Pool
  17. redis *redis.Pool
  18. db *xsql.DB
  19. cache *cache.Cache
  20. archiveAPI arcGRPC.ArchiveClient
  21. }
  22. // New init mysql db
  23. func New(c *conf.Config) (dao *Dao) {
  24. dao = &Dao{
  25. c: c,
  26. mc: memcache.NewPool(c.Memcache),
  27. redis: redis.NewPool(c.Redis),
  28. db: xsql.NewMySQL(c.MySQL),
  29. cache: cache.New(10, 10240),
  30. }
  31. var err error
  32. if dao.archiveAPI, err = arcGRPC.NewClient(nil); err != nil {
  33. panic(err)
  34. }
  35. return
  36. }
  37. // Close close the resource.
  38. func (d *Dao) Close() {
  39. d.mc.Close()
  40. d.redis.Close()
  41. d.db.Close()
  42. }
  43. // Ping dao ping
  44. func (d *Dao) Ping(c context.Context) error {
  45. return d.db.Ping(c)
  46. }
  47. func orderKey(id string) string {
  48. return fmt.Sprintf("up_o_%s", id)
  49. }
  50. func assetKey(oid int64, otype string, currency string) string {
  51. return fmt.Sprintf("up_a_%d_%s_%s", oid, otype, currency)
  52. }
  53. func (d *Dao) cacheSFAsset(oid int64, otype string, currency string) string {
  54. return fmt.Sprintf("up_a_sf_%d_%s_%s", oid, otype, currency)
  55. }
  56. //go:generate $GOPATH/src/go-common/app/tool/cache/mc
  57. type _mc interface {
  58. //mc: -key=orderKey -type=get
  59. CacheOrderUser(c context.Context, id int64) (*model.Order, error)
  60. //mc: -key=orderKey -expire=d.cacheTTL.OrderTTL
  61. AddCacheOrderUser(c context.Context, id int64, value *model.Order) error
  62. //mc: -key=orderKey
  63. DelCacheOrderUser(c context.Context, id int64) error
  64. //mc: -key=assetKey -type=get
  65. CacheAsset(c context.Context, oid int64, otype string, currency string) (*model.Asset, error)
  66. //mc: -key=assetKey -expire=d.cacheTTL.AssetTTL
  67. AddCacheAsset(c context.Context, oid int64, otype string, currency string, value *model.Asset) error
  68. //mc: -key=assetKey
  69. DelCacheAsset(c context.Context, oid int64, otype string, currency string) error
  70. }
  71. //go:generate $GOPATH/src/go-common/app/tool/cache/gen
  72. type _cache interface {
  73. // cache: -nullcache=&model.Order{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  74. OrderUser(c context.Context, id int64) (*model.Order, error)
  75. // cache: -nullcache=&model.Asset{ID:-1} -check_null_code=$!=nil&&$.ID==-1 -singleflight=true
  76. Asset(c context.Context, oid int64, otype string, currency string) (*model.Asset, error)
  77. }