dao.go 1.4 KB

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