dao.go 979 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/kvo/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/database/sql"
  8. )
  9. // Dao kvo data access obj with bfs
  10. type Dao struct {
  11. cache *memcache.Pool
  12. mcExpire int32
  13. // http client for bfs req
  14. db *sql.DB
  15. // sql stmt
  16. getUserConf *sql.Stmt
  17. getDocument *sql.Stmt
  18. }
  19. // New new data access
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. cache: memcache.NewPool(c.Memcache.Kvo),
  23. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  24. db: sql.NewMySQL(c.Mysql),
  25. }
  26. d.getUserConf = d.db.Prepared(_getUserConf)
  27. d.getDocument = d.db.Prepared(_getDocument)
  28. return
  29. }
  30. // Ping check if health
  31. func (d *Dao) Ping(ctx context.Context) (err error) {
  32. if err = d.pingMemcache(ctx); err != nil {
  33. return
  34. }
  35. if err = d.db.Ping(ctx); err != nil {
  36. return
  37. }
  38. return
  39. }
  40. // BeginTx begin trans
  41. func (d *Dao) BeginTx(c context.Context) (*sql.Tx, error) {
  42. return d.db.Begin(c)
  43. }