Skip to content

Commit

Permalink
chore: optimzie_repo_template (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
qloog authored Dec 6, 2024
1 parent 7ed3b22 commit 09179bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions cmd/eagle/internal/repo/add/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ type {{.LcName}}Repo struct {
tracer trace.Tracer
{{- if eq .WithCache true }}
cache cache.{{.Name}}Cache
{{- end }}
localCache localCache.Cache
sg singleflight.Group
{{- end }}
}
{{- if .WithCache }}
// New{{.Name}} new a repository and return
func New{{.Name}}(db *dal.DBClient, cache cache.{{.Name}}Cache) {{.Name}}Repo {
// New{{.Name}}Repo new a repository and return
func New{{.Name}}Repo(db *dal.DBClient, cache cache.{{.Name}}Cache) {{.Name}}Repo {
return &{{.LcName}}Repo{
db: db,
tracer: otel.Tracer("{{.LcName}}"),
Expand All @@ -62,8 +62,8 @@ func New{{.Name}}(db *dal.DBClient, cache cache.{{.Name}}Cache) {{.Name}}Repo {
}
}
{{- else }}
// New{{.Name}} new a repository and return
func New{{.Name}}(db *dal.DBClient) {{.Name}}Repo {
// New{{.Name}}Repo new a repository and return
func New{{.Name}}Repo(db *dal.DBClient) {{.Name}}Repo {
return &{{.LcName}}Repo{
db: db,
tracer: otel.Tracer("{{.LcName}}Repo"),
Expand Down Expand Up @@ -135,7 +135,7 @@ func (r *{{.LcName}}Repo) Get{{.Name}}(ctx context.Context, id int64) (ret *mode
// 避免缓存击穿(瞬间有大量请求过来)
val, err, _ := r.sg.Do("sg:{{.LcName}}:"+cast.ToString(id), func() (interface{}, error) {
// read db or rpc
data, err := dao.{{.Name}}Model.WithContext(ctx).Where(dao.{{.Name}}Model.ID.Eq(id)).First()
data, err := dao.{{.Name}}Model.WithContext(ctx).Where(dao.{{.Name}}Model.ID.Eq(id)).Last()
if err != nil {
// cache not found and set empty cache to avoid 缓存穿透
// Note: 如果缓存空数据太多,会大大降低缓存命中率,可以改为使用布隆过滤器
Expand Down Expand Up @@ -168,7 +168,7 @@ func (r *{{.LcName}}Repo) Get{{.Name}}(ctx context.Context, id int64) (ret *mode
return val.(*model.{{.Name}}Model), nil
{{- else }}
// read db
ret, err = dao.{{.Name}}Model.WithContext(ctx).Where(dao.{{.Name}}Model.ID.In(ids...)).Find()
ret, err = dao.{{.Name}}Model.WithContext(ctx).Where(dao.{{.Name}}Model.ID.Eq(id)).Last()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -212,8 +212,8 @@ func (r *{{.LcName}}Repo) BatchGet{{.Name}}(ctx context.Context, ids []int64) (r
return ret, nil
{{- else }}
// read db
items := make([]*model.{{.Name}}Model, 0)
items, err := dao.{{.Name}}Model.WithContext(ctx).Where(dao.{{.Name}}Model.ID.In(ids...)).Find()
items := make([]*model.{{.Name}}Model, len(ids))
items, err = dao.{{.Name}}Model.WithContext(ctx).Where(dao.{{.Name}}Model.ID.In(ids...)).Find()
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/eagle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var (
// Version is the version of the compiled software.
Version = "v1.0.1"
Version = "v1.0.2"

rootCmd = &cobra.Command{
Use: "eagle",
Expand Down

0 comments on commit 09179bc

Please sign in to comment.