dao.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/aegis/conf"
  5. relrpc "go-common/app/service/main/relation/rpc/client"
  6. uprpc "go-common/app/service/main/up/api/v1"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/database/orm"
  10. xsql "go-common/library/database/sql"
  11. bm "go-common/library/net/http/blademaster"
  12. "github.com/jinzhu/gorm"
  13. )
  14. // Dao dao
  15. type Dao struct {
  16. c *conf.Config
  17. mc *memcache.Pool
  18. redis *redis.Pool
  19. slowdb *xsql.DB
  20. fastdb *xsql.DB
  21. orm *gorm.DB
  22. //gorpc
  23. relRPC *relrpc.Service
  24. //grpc
  25. upRPC uprpc.UpClient
  26. httpFast *bm.Client
  27. }
  28. // New init mysql db
  29. func New(c *conf.Config) (dao *Dao) {
  30. dao = &Dao{
  31. c: c,
  32. mc: memcache.NewPool(c.Memcache),
  33. redis: redis.NewPool(c.Redis),
  34. fastdb: xsql.NewMySQL(c.MySQL.Fast),
  35. slowdb: xsql.NewMySQL(c.MySQL.Slow),
  36. orm: orm.NewMySQL(c.ORM),
  37. httpFast: bm.NewClient(c.HTTP.Fast),
  38. }
  39. // rpc
  40. if !c.Debug {
  41. dao.relRPC = relrpc.New(c.RPC.Rel)
  42. var err error
  43. if dao.upRPC, err = uprpc.NewClient(c.GRPC.Up); err != nil {
  44. panic(err)
  45. }
  46. }
  47. dao.orm.LogMode(true)
  48. return
  49. }
  50. // Close close the resource.
  51. func (d *Dao) Close() {
  52. d.mc.Close()
  53. d.redis.Close()
  54. d.slowdb.Close()
  55. d.fastdb.Close()
  56. d.orm.Close()
  57. }
  58. // Ping dao ping
  59. func (d *Dao) Ping(c context.Context) error {
  60. return d.fastdb.Ping(c)
  61. }