dao.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "github.com/bluele/gcache"
  6. "go-common/app/service/main/relation/conf"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/database/sql"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. const (
  13. _passportDetailURL = "/intranet/acc/detail"
  14. )
  15. // Dao struct info of Dao.
  16. type Dao struct {
  17. // mysql
  18. db *sql.DB
  19. // memcache
  20. mc *memcache.Pool
  21. followerExpire int32
  22. mcExpire int32
  23. // redis
  24. redis *redis.Pool
  25. redisExpire int32
  26. // conf
  27. c *conf.Config
  28. // prompt
  29. period int64
  30. bcount int64
  31. ucount int64
  32. // followers unread duration
  33. UnreadDuration int64
  34. // apis
  35. detailURI string
  36. // client
  37. client *bm.Client
  38. // statCache
  39. statStore gcache.Cache
  40. }
  41. // New new a Dao and return.
  42. func New(c *conf.Config) (d *Dao) {
  43. d = &Dao{
  44. // conf
  45. c: c,
  46. // db
  47. db: sql.NewMySQL(c.Mysql),
  48. // memcache
  49. mc: memcache.NewPool(c.Memcache.Config),
  50. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  51. followerExpire: int32(time.Duration(c.Memcache.FollowerExpire) / time.Second),
  52. // redis
  53. redis: redis.NewPool(c.Redis.Config),
  54. redisExpire: int32(time.Duration(c.Redis.Expire) / time.Second),
  55. // prompt
  56. period: int64(time.Duration(c.Relation.Period) / time.Second),
  57. bcount: c.Relation.Bcount,
  58. ucount: c.Relation.Ucount,
  59. // followers unread
  60. UnreadDuration: int64(time.Duration(c.Relation.FollowersUnread) / time.Second),
  61. // passport api
  62. detailURI: c.Host.Passport + _passportDetailURL,
  63. client: bm.NewClient(c.HTTPClient),
  64. statStore: gcache.New(c.StatCache.Size).LFU().Build(),
  65. }
  66. return
  67. }
  68. // Ping ping health.
  69. func (d *Dao) Ping(c context.Context) (err error) {
  70. if err = d.pingMC(c); err != nil {
  71. return
  72. }
  73. return d.pingRedis(c)
  74. }
  75. // Close close connections of mc, redis, db.
  76. func (d *Dao) Close() {
  77. if d.mc != nil {
  78. d.mc.Close()
  79. }
  80. if d.redis != nil {
  81. d.redis.Close()
  82. }
  83. if d.db != nil {
  84. d.db.Close()
  85. }
  86. }