dao.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "go-common/app/interface/openplatform/seo/conf"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _pro = "pro"
  13. _item = "item"
  14. )
  15. // Dao dao
  16. type Dao struct {
  17. c *conf.Config
  18. mc *memcache.Pool
  19. }
  20. // New init memcache
  21. func New(c *conf.Config) (dao *Dao) {
  22. dao = &Dao{
  23. c: c,
  24. mc: memcache.NewPool(c.Memcache),
  25. }
  26. return
  27. }
  28. // Close close the resource.
  29. func (d *Dao) Close() {
  30. d.mc.Close()
  31. }
  32. // Ping dao ping
  33. func (d *Dao) Ping(c context.Context) (err error) {
  34. if err = d.pingMC(c); err != nil {
  35. log.Error("pingMC error(%+v)", err)
  36. return
  37. }
  38. return
  39. }
  40. // pingMc ping memcache
  41. func (d *Dao) pingMC(c context.Context) (err error) {
  42. con := d.mc.Get(c)
  43. defer con.Close()
  44. item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 1}
  45. err = con.Set(&item)
  46. return
  47. }
  48. // getUrl get page url
  49. // page: pro, item
  50. func getUrl(id int, name string, bot bool) string {
  51. p := conf.GetPage(name)
  52. if p == nil {
  53. return ""
  54. }
  55. url := p.Bfs
  56. if !bot {
  57. url = p.Url
  58. }
  59. return fmt.Sprintf(url, id)
  60. }
  61. // getKey get page cache key
  62. // name: pro, item
  63. // return key: pro:bot:1001, pro:app:1001
  64. // return key: item:bot:1001, item:app:1001
  65. func getKey(id int, name string, bot bool) string {
  66. ua := "bot"
  67. if !bot {
  68. ua = "app"
  69. }
  70. return fmt.Sprintf("%s:%s:%d", name, ua, id)
  71. }
  72. // GetFile get page from file
  73. func (d *Dao) GetFile(c context.Context, path string) (res []byte, err error) {
  74. log.Info(path)
  75. res, err = ioutil.ReadFile(path)
  76. return
  77. }
  78. // GetUrl get page from url
  79. func (d *Dao) GetUrl(c context.Context, url string) (res []byte, err error) {
  80. log.Info(url)
  81. r, err := http.Get(url)
  82. if err != nil {
  83. return
  84. }
  85. defer r.Body.Close()
  86. if err == nil {
  87. res, err = ioutil.ReadAll(r.Body)
  88. }
  89. return
  90. }
  91. // GetCache get page from cache
  92. func (d *Dao) GetCache(c context.Context, key string) (res []byte, err error) {
  93. log.Info(key)
  94. con := d.mc.Get(c)
  95. defer con.Close()
  96. item, err := con.Get(key)
  97. if err != nil {
  98. return
  99. }
  100. err = con.Scan(item, &res)
  101. return
  102. }
  103. // AddCache add page to cache
  104. func (d *Dao) AddCache(c context.Context, key string, val []byte) (err error) {
  105. log.Info(key)
  106. item := &memcache.Item{
  107. Key: key,
  108. Value: val,
  109. Flags: memcache.FlagRAW,
  110. Expiration: conf.Conf.Seo.Expire,
  111. }
  112. con := d.mc.Get(c)
  113. defer con.Close()
  114. if err = con.Set(item); err != nil {
  115. log.Error("key(%s) error(%v)", key, err)
  116. }
  117. return
  118. }
  119. // DelCache delete page cache
  120. func (d *Dao) DelCache(c context.Context, key string) (err error) {
  121. con := d.mc.Get(c)
  122. defer con.Close()
  123. return con.Delete(key)
  124. }
  125. // ClearCache clear all page cache
  126. func (d *Dao) ClearCache(c context.Context) (err error) {
  127. return
  128. }