add dao interface

pull/162/head
小旭 5 years ago
parent 19200b067d
commit d4ac045f8d
  1. 27
      tool/kratos/template.go

@ -189,8 +189,14 @@ import (
xtime "github.com/bilibili/kratos/pkg/time" xtime "github.com/bilibili/kratos/pkg/time"
) )
// Dao dao. // Dao dao interface
type Dao struct { type Dao interface {
Close()
Ping(ctx context.Context) (err error)
}
// dao dao.
type dao struct {
db *sql.DB db *sql.DB
redis *redis.Pool redis *redis.Pool
redisExpire int32 redisExpire int32
@ -205,7 +211,7 @@ func checkErr(err error) {
} }
// New new a dao and return. // New new a dao and return.
func New() (dao *Dao) { func New() (Dao) {
var ( var (
dc struct { dc struct {
Demo *sql.Config Demo *sql.Config
@ -222,7 +228,7 @@ func New() (dao *Dao) {
checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&dc)) checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&dc))
checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rc)) checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rc))
checkErr(paladin.Get("memcache.toml").UnmarshalTOML(&mc)) checkErr(paladin.Get("memcache.toml").UnmarshalTOML(&mc))
dao = &Dao{ return &dao{
// mysql // mysql
db: sql.NewMySQL(dc.Demo), db: sql.NewMySQL(dc.Demo),
// redis // redis
@ -232,18 +238,17 @@ func New() (dao *Dao) {
mc: memcache.New(mc.Demo), mc: memcache.New(mc.Demo),
mcExpire: int32(time.Duration(mc.DemoExpire) / time.Second), mcExpire: int32(time.Duration(mc.DemoExpire) / time.Second),
} }
return
} }
// Close close the resource. // Close close the resource.
func (d *Dao) Close() { func (d *dao) Close() {
d.mc.Close() d.mc.Close()
d.redis.Close() d.redis.Close()
d.db.Close() d.db.Close()
} }
// Ping ping the resource. // Ping ping the resource.
func (d *Dao) Ping(ctx context.Context) (err error) { func (d *dao) Ping(ctx context.Context) (err error) {
if err = d.pingMC(ctx); err != nil { if err = d.pingMC(ctx); err != nil {
return return
} }
@ -253,14 +258,14 @@ func (d *Dao) Ping(ctx context.Context) (err error) {
return d.db.Ping(ctx) return d.db.Ping(ctx)
} }
func (d *Dao) pingMC(ctx context.Context) (err error) { func (d *dao) pingMC(ctx context.Context) (err error) {
if err = d.mc.Set(ctx, &memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil { if err = d.mc.Set(ctx, &memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
log.Error("conn.Set(PING) error(%v)", err) log.Error("conn.Set(PING) error(%v)", err)
} }
return return
} }
func (d *Dao) pingRedis(ctx context.Context) (err error) { func (d *dao) pingRedis(ctx context.Context) (err error) {
conn := d.redis.Get(ctx) conn := d.redis.Get(ctx)
defer conn.Close() defer conn.Close()
if _, err = conn.Do("SET", "ping", "pong"); err != nil { if _, err = conn.Do("SET", "ping", "pong"); err != nil {
@ -286,7 +291,7 @@ import (
// Service service. // Service service.
type Service struct { type Service struct {
ac *paladin.Map ac *paladin.Map
dao *dao.Dao dao dao.Dao
} }
// New new a service and return. // New new a service and return.
@ -329,7 +334,7 @@ import (
// Service service. // Service service.
type Service struct { type Service struct {
ac *paladin.Map ac *paladin.Map
dao *dao.Dao dao dao.Dao
} }
// New new a service and return. // New new a service and return.

Loading…
Cancel
Save