dao.go 620 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1
package dao
kzkzzzz's avatar
kzkzzzz committed
2 3 4 5 6

import (
	"github.com/go-kratos/kratos/v2/log"
	"github.com/google/wire"
	"gorm.io/gorm"
kzkzzzz's avatar
kzkzzzz committed
7 8 9
	"hilo/app/service/demo/internal/conf"
	"hilo/common/mysql"
	"hilo/common/redis"
kzkzzzz's avatar
kzkzzzz committed
10 11
)

kzkzzzz's avatar
kzkzzzz committed
12 13
// ProviderSet is dao providers.
var ProviderSet = wire.NewSet(NewDao)
kzkzzzz's avatar
kzkzzzz committed
14

kzkzzzz's avatar
kzkzzzz committed
15 16
// Dao .
type Dao struct {
kzkzzzz's avatar
kzkzzzz committed
17 18 19 20
	DB    *gorm.DB
	Redis *redis.WrapClient
}

kzkzzzz's avatar
kzkzzzz committed
21 22
// NewDao .
func NewDao(c *conf.Data) (*Dao, func(), error) {
kzkzzzz's avatar
kzkzzzz committed
23 24 25 26 27 28 29 30 31
	db := mysql.NewDB(c.Mysql)
	rs := redis.NewRedis(c.Redis)
	cleanup := func() {
		d, _ := db.DB()
		d.Close()
		rs.Client.Close()
		log.Debug("close data resources")
	}

kzkzzzz's avatar
kzkzzzz committed
32
	return &Dao{
kzkzzzz's avatar
kzkzzzz committed
33 34 35 36
		DB:    db,
		Redis: rs,
	}, cleanup, nil
}