dao.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/main/identify-game/conf"
  5. "go-common/library/cache/memcache"
  6. httpx "go-common/library/net/http/blademaster"
  7. "go-common/library/stat/prom"
  8. )
  9. var (
  10. errorsCount = prom.BusinessErrCount
  11. cachedCount = prom.CacheHit
  12. missedCount = prom.CacheMiss
  13. )
  14. // PromError prom error
  15. func PromError(name string) {
  16. errorsCount.Incr(name)
  17. }
  18. // Dao struct info of Dao
  19. type Dao struct {
  20. c *conf.Config
  21. mc *memcache.Pool
  22. client *httpx.Client
  23. }
  24. // New new a Dao and return.
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. c: c,
  28. mc: memcache.NewPool(c.Memcache),
  29. client: httpx.NewClient(c.HTTPClient),
  30. }
  31. return
  32. }
  33. // Close close connections of mc, redis, db.
  34. func (d *Dao) Close() error {
  35. return d.mc.Close()
  36. }
  37. // Ping ping health.
  38. func (d *Dao) Ping(c context.Context) (err error) {
  39. if err = d.pingMC(c); err != nil {
  40. PromError("mc:Ping")
  41. }
  42. return
  43. }
  44. // pingMc ping memcache
  45. func (d *Dao) pingMC(c context.Context) (err error) {
  46. conn := d.mc.Get(c)
  47. defer conn.Close()
  48. item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 100}
  49. err = conn.Set(&item)
  50. return
  51. }