From a74aaf18230c1637578992c143a8dd8cacb6f91a Mon Sep 17 00:00:00 2001 From: anqiansong Date: Mon, 9 Aug 2021 11:33:01 +0800 Subject: [PATCH] optimize mongo generation without cache (fix #881) (#882) * fix #881 * fix #881 Co-authored-by: anqiansong --- tools/goctl/model/mongo/template/template.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/goctl/model/mongo/template/template.go b/tools/goctl/model/mongo/template/template.go index 5348e3ff..936caa6c 100644 --- a/tools/goctl/model/mongo/template/template.go +++ b/tools/goctl/model/mongo/template/template.go @@ -7,8 +7,8 @@ import ( "context" "github.com/globalsign/mgo/bson" - cachec "github.com/tal-tech/go-zero/core/stores/cache" - "github.com/tal-tech/go-zero/core/stores/mongoc" + {{if .Cache}}cachec "github.com/tal-tech/go-zero/core/stores/cache" + "github.com/tal-tech/go-zero/core/stores/mongoc"{{else}}"github.com/tal-tech/go-zero/core/stores/mongo"{{end}} ) {{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.Type}}:"{{end}} @@ -21,12 +21,12 @@ type {{.Type}}Model interface{ } type default{{.Type}}Model struct { - *mongoc.Model + {{if .Cache}}*mongoc.Model{{else}}*mongo.Model{{end}} } -func New{{.Type}}Model(url, collection string, c cachec.CacheConf) {{.Type}}Model { +func New{{.Type}}Model(url, collection string{{if .Cache}}, c cachec.CacheConf{{end}}) {{.Type}}Model { return &default{{.Type}}Model{ - Model: mongoc.MustNewModel(url, collection, c), + Model: {{if .Cache}}mongoc.MustNewModel(url, collection, c){{else}}mongo.MustNewModel(url, collection){{end}}, } } @@ -60,12 +60,12 @@ func (m *default{{.Type}}Model) FindOne(ctx context.Context, id string) (*{{.Typ {{if .Cache}}key := prefix{{.Type}}CacheKey + id err = m.GetCollection(session).FindOneId(&data, key, bson.ObjectIdHex(id)) {{- else}} - err = m.GetCollection(session).FindOneIdNoCache(&data, bson.ObjectIdHex(id)) + err = m.GetCollection(session).FindId(bson.ObjectIdHex(id)).One(&data) {{- end}} switch err { case nil: return &data,nil - case mongoc.ErrNotFound: + case {{if .Cache}}mongoc.ErrNotFound{{else}}mongo.ErrNotFound{{end}}: return nil,ErrNotFound default: return nil,err @@ -82,7 +82,7 @@ func (m *default{{.Type}}Model) Update(ctx context.Context, data *{{.Type}}) err {{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex() return m.GetCollection(session).UpdateId(data.ID, data, key) {{- else}} - return m.GetCollection(session).UpdateIdNoCache(data.ID, data) + return m.GetCollection(session).UpdateId(data.ID, data) {{- end}} } @@ -96,7 +96,7 @@ func (m *default{{.Type}}Model) Delete(ctx context.Context, id string) error { {{if .Cache}}key := prefix{{.Type}}CacheKey + id return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id), key) {{- else}} - return m.GetCollection(session).RemoveIdNoCache(bson.ObjectIdHex(id)) + return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id)) {{- end}} } `