dao.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/usersuit/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/sql"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. const (
  12. _msgURL = "/api/notify/send.user.notify.do"
  13. _managersURI = "/x/admin/manager/users"
  14. _managerTotalURI = "/x/admin/manager/users/total"
  15. )
  16. // Dao struct answer history of Dao
  17. type Dao struct {
  18. db *sql.DB
  19. c *conf.Config
  20. // redis
  21. redis *redis.Pool
  22. // http
  23. client *bm.Client
  24. mc *memcache.Pool
  25. mcExpire int32
  26. pointExpire int32
  27. msgURL string
  28. managersURL string
  29. managerTotalURL string
  30. bucket string
  31. key string
  32. secret string
  33. bfs string
  34. }
  35. // New new a Dao and return.
  36. func New(c *conf.Config) (d *Dao) {
  37. d = &Dao{
  38. c: c,
  39. db: sql.NewMySQL(c.DB.Usersuit),
  40. // http client
  41. client: bm.NewClient(c.HTTPClient),
  42. // redis
  43. redis: redis.NewPool(c.Redis),
  44. mc: memcache.NewPool(c.Memcache.Config),
  45. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  46. pointExpire: int32(time.Duration(c.Memcache.PointExpire) / time.Second),
  47. }
  48. d.msgURL = c.Host.Msg + _msgURL
  49. d.managersURL = c.Host.Manager + _managersURI
  50. d.managerTotalURL = c.Host.Manager + _managerTotalURI
  51. d.bucket = c.BFS.Bucket
  52. d.key = c.BFS.Key
  53. d.secret = c.BFS.Secret
  54. d.bfs = c.Host.Bfs
  55. return
  56. }
  57. // BeginTran begin tran.
  58. func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
  59. tx, err = d.db.Begin(c)
  60. return
  61. }
  62. // Close close connections of mc, redis, db.
  63. func (d *Dao) Close() {
  64. if d.db != nil {
  65. d.db.Close()
  66. }
  67. if d.redis != nil {
  68. d.redis.Close()
  69. }
  70. }
  71. // Ping ping health.
  72. func (d *Dao) Ping(c context.Context) (err error) {
  73. return d.db.Ping(c)
  74. }