You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.4 KiB
64 lines
1.4 KiB
5 years ago
|
package dao
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
|
||
|
"{{.ModPrefix}}{{.Name}}/internal/model"
|
||
|
"github.com/bilibili/kratos/pkg/cache/memcache"
|
||
|
"github.com/bilibili/kratos/pkg/cache/redis"
|
||
|
"github.com/bilibili/kratos/pkg/conf/paladin"
|
||
|
"github.com/bilibili/kratos/pkg/database/sql"
|
||
|
"github.com/bilibili/kratos/pkg/sync/pipeline/fanout"
|
||
|
xtime "github.com/bilibili/kratos/pkg/time"
|
||
|
)
|
||
|
|
||
|
//go:generate kratos tool genbts
|
||
|
// Dao dao interface
|
||
|
type Dao interface {
|
||
|
Close()
|
||
|
Ping(ctx context.Context) (err error)
|
||
|
// bts: -nullcache=&model.Article{ID:-1} -check_null_code=$!=nil&&$.ID==-1
|
||
|
Article(c context.Context, id int64) (*model.Article, error)
|
||
|
}
|
||
|
|
||
|
// dao dao.
|
||
|
type dao struct {
|
||
|
db *sql.DB
|
||
|
redis *redis.Redis
|
||
|
mc *memcache.Memcache
|
||
|
cache *fanout.Fanout
|
||
|
demoExpire int32
|
||
|
}
|
||
|
|
||
|
// New new a dao and return.
|
||
|
func New(r *redis.Redis, mc *memcache.Memcache, db *sql.DB) (d Dao, err error) {
|
||
|
var cfg struct{
|
||
|
DemoExpire xtime.Duration
|
||
|
}
|
||
|
if err = paladin.Get("application.toml").UnmarshalTOML(&cfg); err != nil {
|
||
|
return
|
||
|
}
|
||
|
d = &dao{
|
||
|
db: db,
|
||
|
redis: r,
|
||
|
mc: mc,
|
||
|
cache: fanout.New("cache"),
|
||
|
demoExpire: int32(time.Duration(cfg.DemoExpire) / time.Second),
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Close close the resource.
|
||
|
func (d *dao) Close() {
|
||
|
d.mc.Close()
|
||
|
d.redis.Close()
|
||
|
d.db.Close()
|
||
|
d.cache.Close()
|
||
|
}
|
||
|
|
||
|
// Ping ping the resource.
|
||
|
func (d *dao) Ping(ctx context.Context) (err error) {
|
||
|
return nil
|
||
|
}
|