dao.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package ugc
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/tv/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/sql"
  9. httpx "go-common/library/net/http/blademaster"
  10. )
  11. // Dao dao.
  12. type Dao struct {
  13. conf *conf.Config
  14. DB *sql.DB
  15. client *httpx.Client
  16. mc *memcache.Pool
  17. mcExpire int32 // expire for ugc cache, same as pgc auth, because it's daily refresh
  18. criCID int64 // critical cid for ugc video sync
  19. redis *redis.Pool
  20. }
  21. // New create a instance of Dao and return.
  22. func New(c *conf.Config) (d *Dao) {
  23. d = &Dao{
  24. conf: c,
  25. DB: sql.NewMySQL(c.Mysql),
  26. client: httpx.NewClient(conf.Conf.HTTPClient),
  27. mc: memcache.NewPool(c.Memcache.Config),
  28. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  29. criCID: c.UgcSync.Cfg.CriticalCid,
  30. redis: redis.NewPool(c.Redis.Config),
  31. }
  32. return
  33. }
  34. // Close close the redis and kafka resource.
  35. func (d *Dao) Close() {
  36. if d.DB != nil {
  37. d.DB.Close()
  38. }
  39. }
  40. // BeginTran begin mysql transaction
  41. func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
  42. return d.DB.Begin(c)
  43. }