dao.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package dao
  2. import (
  3. "context"
  4. "go-common/library/cache/memcache"
  5. "go-common/library/cache/redis"
  6. "go-common/library/database/sql"
  7. )
  8. // Dao dao.
  9. type Dao struct {
  10. db *sql.DB
  11. redis *redis.Pool
  12. redisExpire int32
  13. mc *memcache.Pool
  14. mcExpire int32
  15. }
  16. func checkErr(err error) {
  17. if err != nil {
  18. panic(err)
  19. }
  20. }
  21. // New new a dao and return.
  22. func New() (dao *Dao) {
  23. var (
  24. // dc struct {
  25. // Demo *sql.Config
  26. // }
  27. // rc struct {
  28. // Demo *redis.Config
  29. // DemoExpire xtime.Duration
  30. // }
  31. // mc struct {
  32. // Demo *memcache.Config
  33. // DemoExpire xtime.Duration
  34. // }
  35. )
  36. // checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&dc))
  37. // checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rc))
  38. // checkErr(paladin.Get("memcache.toml").UnmarshalTOML(&mc))
  39. dao = &Dao{
  40. // // mysql
  41. // db: sql.NewMySQL(dc.Demo),
  42. // // redis
  43. // redis: redis.NewPool(rc.Demo),
  44. // redisExpire: int32(time.Duration(rc.DemoExpire) / time.Second),
  45. // // memcache
  46. // mc: memcache.NewPool(mc.Demo),
  47. // mcExpire: int32(time.Duration(mc.DemoExpire) / time.Second),
  48. }
  49. return
  50. }
  51. // Close close the resource.
  52. func (d *Dao) Close() {
  53. // d.mc.Close()
  54. // d.redis.Close()
  55. // d.db.Close()
  56. }
  57. // Ping ping the resource.
  58. func (d *Dao) Ping(ctx context.Context) (err error) {
  59. // if err = d.pingMC(ctx); err != nil {
  60. // return
  61. // }
  62. // if err = d.pingRedis(ctx); err != nil {
  63. // return
  64. // }
  65. // return d.db.Ping(ctx)
  66. return nil
  67. }
  68. // func (d *Dao) pingMC(ctx context.Context) (err error) {
  69. // conn := d.mc.Get(ctx)
  70. // defer conn.Close()
  71. // if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
  72. // log.Error("conn.Set(PING) error(%v)", err)
  73. // }
  74. // return
  75. // }
  76. // func (d *Dao) pingRedis(ctx context.Context) (err error) {
  77. // conn := d.redis.Get(ctx)
  78. // defer conn.Close()
  79. // if _, err = conn.Do("SET", "ping", "pong"); err != nil {
  80. // log.Error("conn.Set(PING) error(%v)", err)
  81. // }
  82. // return
  83. // }