dao.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package dao
  2. import (
  3. "time"
  4. "go-common/app/service/main/secure/conf"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/database/hbase.v2"
  10. )
  11. // Dao struct info of Dao.
  12. type Dao struct {
  13. db *sql.DB
  14. ddldb *sql.DB
  15. c *conf.Config
  16. redis *redis.Pool
  17. hbase *hbase.Client
  18. locsExpire int32
  19. expire int64
  20. doubleCheckExpire int64
  21. mc *memcache.Pool
  22. // http
  23. httpClient *bm.Client
  24. }
  25. // New new a Dao and return.
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. c: c,
  29. redis: redis.NewPool(c.Redis.Config),
  30. expire: int64(time.Duration(c.Redis.Expire) / time.Second),
  31. doubleCheckExpire: int64(time.Duration(c.Redis.DoubleCheck) / time.Second),
  32. db: sql.NewMySQL(c.Mysql.Secure),
  33. ddldb: sql.NewMySQL(c.Mysql.DDL),
  34. hbase: hbase.NewClient(c.HBase.Config),
  35. mc: memcache.NewPool(c.Memcache.Config),
  36. locsExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  37. httpClient: bm.NewClient(c.HTTPClient),
  38. }
  39. return
  40. }
  41. // Close close connections of mc, redis, db.
  42. func (d *Dao) Close() {
  43. if d.db != nil {
  44. d.db.Close()
  45. }
  46. }