dao.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package pendant
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/usersuit/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/sql"
  9. bm "go-common/library/net/http/blademaster"
  10. "github.com/bluele/gcache"
  11. )
  12. const (
  13. _info = "/internal/v1/user/"
  14. )
  15. // Dao struct info of Dao.
  16. type Dao struct {
  17. db *sql.DB
  18. c *conf.Config
  19. client *bm.Client
  20. // redis
  21. redis *redis.Pool
  22. pendantExpire int32
  23. // memcache
  24. mc *memcache.Pool
  25. pointExpire int32
  26. vipInfoURL string
  27. payURL string
  28. notifyURL string
  29. // equipStore
  30. equipStore gcache.Cache
  31. }
  32. // New new a Dao and return.
  33. func New(c *conf.Config) (d *Dao) {
  34. d = &Dao{
  35. c: c,
  36. db: sql.NewMySQL(c.MySQL),
  37. client: bm.NewClient(c.HTTPClient),
  38. // redis
  39. redis: redis.NewPool(c.Redis.Config),
  40. pendantExpire: int32(time.Duration(c.Redis.PendantExpire) / time.Second),
  41. // memcache
  42. mc: memcache.NewPool(c.Memcache.Config),
  43. pointExpire: int32(time.Duration(c.Memcache.PointExpire) / time.Second),
  44. vipInfoURL: c.VipURI + _info,
  45. payURL: c.PayURL,
  46. notifyURL: c.NotifyURL,
  47. equipStore: gcache.New(c.EquipCache.Size).LFU().Build(),
  48. }
  49. return
  50. }
  51. // Ping ping health.
  52. func (d *Dao) Ping(c context.Context) (err error) {
  53. return d.pingRedis(c)
  54. }
  55. // Close close connections of mc, redis, db.
  56. func (d *Dao) Close() {
  57. if d.redis != nil {
  58. d.redis.Close()
  59. }
  60. if d.db != nil {
  61. d.db.Close()
  62. }
  63. }
  64. func (d *Dao) pingRedis(c context.Context) (err error) {
  65. conn := d.redis.Get(c)
  66. _, err = conn.Do("SET", "PING", "PONG")
  67. conn.Close()
  68. return
  69. }