dao.go 914 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package dao
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "go-common/app/interface/main/laser/conf"
  6. "go-common/library/cache/memcache"
  7. xsql "go-common/library/database/sql"
  8. "gopkg.in/gomail.v2"
  9. )
  10. type Dao struct {
  11. c *conf.Config
  12. db *xsql.DB
  13. mc *memcache.Pool
  14. mcExpire int32
  15. email *gomail.Dialer
  16. }
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. c: c,
  20. db: xsql.NewMySQL(c.Mysql),
  21. mc: memcache.NewPool(c.Memcache.Laser.Config),
  22. mcExpire: 3600 * 6,
  23. email: gomail.NewDialer(c.Mail.Host, c.Mail.Port, c.Mail.Username, c.Mail.Password),
  24. }
  25. d.email.TLSConfig = &tls.Config{
  26. InsecureSkipVerify: true,
  27. }
  28. return
  29. }
  30. func (d *Dao) Ping(c context.Context) (err error) {
  31. return d.db.Ping(c)
  32. }
  33. func (d *Dao) Close() {
  34. if d.db != nil {
  35. d.db.Close()
  36. }
  37. }
  38. // BeginTran BeginTran.
  39. func (d *Dao) BeginTran(c context.Context) (*xsql.Tx, error) {
  40. return d.db.Begin(c)
  41. }