dao.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package thirdp
  2. import (
  3. "runtime"
  4. "time"
  5. "go-common/app/interface/main/tv/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/stat/prom"
  11. )
  12. // Dao .
  13. type Dao struct {
  14. db *sql.DB
  15. conf *conf.Config
  16. redis *redis.Pool
  17. mc *memcache.Pool
  18. dbeiExpire int64
  19. cntExpire int32
  20. mCh chan func()
  21. }
  22. // New .
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. db: sql.NewMySQL(c.Mysql),
  26. conf: c,
  27. redis: redis.NewPool(c.Redis.Config),
  28. dbeiExpire: int64(time.Duration(c.Cfg.Dangbei.Expire) / time.Second),
  29. mc: memcache.NewPool(c.Memcache.Config),
  30. cntExpire: int32(time.Duration(c.Memcache.MangoExpire) / time.Second),
  31. mCh: make(chan func(), 10240),
  32. }
  33. for i := 0; i < runtime.NumCPU()*2; i++ {
  34. go d.cacheproc()
  35. }
  36. return
  37. }
  38. var (
  39. cachedCount = prom.CacheHit
  40. missedCount = prom.CacheMiss
  41. )
  42. // addCache add archive to mc or redis
  43. func (d *Dao) addCache(f func()) {
  44. select {
  45. case d.mCh <- f:
  46. default:
  47. log.Warn("cacheproc chan full")
  48. }
  49. }
  50. // cacheproc write memcache and stat redis use goroutine
  51. func (d *Dao) cacheproc() {
  52. for {
  53. f := <-d.mCh
  54. f()
  55. }
  56. }