dao.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package cms
  2. import (
  3. "runtime"
  4. "time"
  5. "go-common/app/interface/main/tv/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/stat/prom"
  10. )
  11. // Dao is account dao.
  12. type Dao struct {
  13. mc *memcache.Pool
  14. conf *conf.Config
  15. db *sql.DB
  16. mCh chan func()
  17. expireCMS int32
  18. }
  19. // New account dao.
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. conf: c,
  23. mc: memcache.NewPool(c.Memcache.Config),
  24. db: sql.NewMySQL(c.Mysql),
  25. mCh: make(chan func(), 10240),
  26. expireCMS: int32(time.Duration(c.Memcache.CmsExpire) / time.Second),
  27. }
  28. // video db
  29. for i := 0; i < runtime.NumCPU()*2; i++ {
  30. go d.cacheproc()
  31. }
  32. return
  33. }
  34. // Prom
  35. var (
  36. errorsCount = prom.BusinessErrCount
  37. infosCount = prom.BusinessInfoCount
  38. cachedCount = prom.CacheHit
  39. missedCount = prom.CacheMiss
  40. )
  41. // PromError prom error
  42. func PromError(name string) {
  43. errorsCount.Incr(name)
  44. }
  45. // PromInfo add prom info
  46. func PromInfo(name string) {
  47. infosCount.Incr(name)
  48. }
  49. // addCache add archive to mc or redis
  50. func (d *Dao) addCache(f func()) {
  51. select {
  52. case d.mCh <- f:
  53. default:
  54. log.Warn("cacheproc chan full")
  55. }
  56. }
  57. // cacheproc write memcache and stat redis use goroutine
  58. func (d *Dao) cacheproc() {
  59. for {
  60. f := <-d.mCh
  61. f()
  62. }
  63. }