浏览代码

optimize mongo generation without cache (fix #881) (#882)

* fix #881

* fix #881

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
anqiansong 3 年之前
父节点
当前提交
a74aaf1823
共有 1 个文件被更改,包括 9 次插入9 次删除
  1. 9 9
      tools/goctl/model/mongo/template/template.go

+ 9 - 9
tools/goctl/model/mongo/template/template.go

@@ -7,8 +7,8 @@ import (
     "context"
     "context"
 
 
     "github.com/globalsign/mgo/bson"
     "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}}
 {{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.Type}}:"{{end}}
@@ -21,12 +21,12 @@ type {{.Type}}Model interface{
 }
 }
 
 
 type default{{.Type}}Model struct {
 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{
 	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
     {{if .Cache}}key := prefix{{.Type}}CacheKey + id
     err = m.GetCollection(session).FindOneId(&data, key, bson.ObjectIdHex(id))
     err = m.GetCollection(session).FindOneId(&data, key, bson.ObjectIdHex(id))
 	{{- else}}
 	{{- else}}
-	err = m.GetCollection(session).FindOneIdNoCache(&data, bson.ObjectIdHex(id))
+	err = m.GetCollection(session).FindId(bson.ObjectIdHex(id)).One(&data)
 	{{- end}}
 	{{- end}}
     switch err {
     switch err {
     case nil:
     case nil:
         return &data,nil
         return &data,nil
-    case mongoc.ErrNotFound:
+    case {{if .Cache}}mongoc.ErrNotFound{{else}}mongo.ErrNotFound{{end}}:
         return nil,ErrNotFound
         return nil,ErrNotFound
     default:
     default:
         return nil,err
         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()
 	{{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex()
     return m.GetCollection(session).UpdateId(data.ID, data, key)
     return m.GetCollection(session).UpdateId(data.ID, data, key)
 	{{- else}}
 	{{- else}}
-	return m.GetCollection(session).UpdateIdNoCache(data.ID, data)
+	return m.GetCollection(session).UpdateId(data.ID, data)
 	{{- end}}
 	{{- end}}
 }
 }
 
 
@@ -96,7 +96,7 @@ func (m *default{{.Type}}Model) Delete(ctx context.Context, id string) error {
     {{if .Cache}}key := prefix{{.Type}}CacheKey + id
     {{if .Cache}}key := prefix{{.Type}}CacheKey + id
     return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id), key)
     return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id), key)
 	{{- else}}
 	{{- else}}
-	return m.GetCollection(session).RemoveIdNoCache(bson.ObjectIdHex(id))
+	return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id))
 	{{- end}}
 	{{- end}}
 }
 }
 `
 `