dao.go 961 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "go-common/app/service/main/passport-sns/conf"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/database/sql"
  9. )
  10. // Dao dao struct
  11. type Dao struct {
  12. c *conf.Config
  13. db *sql.DB
  14. mc *memcache.Pool
  15. client *http.Client
  16. mcExpire int32
  17. }
  18. // New create new dao
  19. func New(c *conf.Config) (d *Dao) {
  20. d = &Dao{
  21. c: c,
  22. db: sql.NewMySQL(c.MySQL),
  23. mc: memcache.NewPool(c.Memcache.Config),
  24. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  25. client: &http.Client{
  26. Timeout: 600 * time.Millisecond,
  27. },
  28. }
  29. return
  30. }
  31. // Ping check db and mc health.
  32. func (d *Dao) Ping(c context.Context) (err error) {
  33. return
  34. }
  35. // Close close connections of mc, redis, db.
  36. func (d *Dao) Close() {
  37. if d.db != nil {
  38. d.db.Close()
  39. }
  40. }
  41. // BeginTran begin transcation.
  42. func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
  43. return d.db.Begin(c)
  44. }