dao.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "time"
  4. "go-common/app/interface/main/answer/conf"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. httpx "go-common/library/net/http/blademaster"
  9. "go-common/library/queue/databus"
  10. )
  11. // Dao struct answer history of Dao
  12. type Dao struct {
  13. c *conf.Config
  14. db *sql.DB
  15. redis *redis.Pool
  16. mc *memcache.Pool
  17. mcExpire int32
  18. redisExpire int32
  19. ansCountExpire int32
  20. ansAddFlagExpire int32
  21. answerBlockExpire int32
  22. dbExtraAnswerRet *databus.Databus
  23. captcha *httpx.Client
  24. }
  25. // New new a Dao and return.
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. c: c,
  29. db: sql.NewMySQL(c.Mysql),
  30. redis: redis.NewPool(c.Redis.Config),
  31. mc: memcache.NewPool(c.Memcache.Config),
  32. redisExpire: int32(time.Duration(c.Redis.Expire) / time.Second),
  33. ansCountExpire: int32(time.Duration(c.Redis.AnsCountExpire) / time.Second),
  34. ansAddFlagExpire: int32(time.Duration(c.Redis.AnsCountExpire) / time.Second),
  35. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  36. answerBlockExpire: int32(time.Duration(c.Memcache.AnswerBolckExpire) / time.Second),
  37. dbExtraAnswerRet: databus.New(c.DataBus.ExtraAnswer),
  38. captcha: httpx.NewClient(c.Captcha),
  39. }
  40. return
  41. }
  42. // Close close connections of mc, redis, db.
  43. func (d *Dao) Close() {
  44. if d.redis != nil {
  45. d.redis.Close()
  46. }
  47. if d.mc != nil {
  48. d.mc.Close()
  49. }
  50. if d.db != nil {
  51. d.db.Close()
  52. }
  53. }