Skip to content

Commit

Permalink
feat(sqlbuilderzero): update cache design
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Nov 17, 2024
1 parent ca35b5b commit 4b127c0
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 206 deletions.
14 changes: 7 additions & 7 deletions .template/go-zero/model/customized.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (m *custom{{.upperStartCamelObject}}Model) BulkInsert(ctx context.Context,
if session != nil {
_, err = session.ExecCtx(ctx, statement, args...)
} else {
{{if .withCache}}_, err = m.ExecNoCacheCtx(ctx, statement, args...){{else}}_, err = m.conn.ExecCtx(ctx, statement, args...){{end}}
_, err = m.conn.ExecCtx(ctx, statement, args...)
}
return err
}
Expand All @@ -26,7 +26,7 @@ func (m *custom{{.upperStartCamelObject}}Model) FindByCondition(ctx context.Cont
if session != nil {
err = session.QueryRowsCtx(ctx, &resp, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx, &resp, statement, args...){{else}}err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...){{end}}
err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...)
}
if err != nil {
return nil, err
Expand All @@ -47,7 +47,7 @@ func (m *custom{{.upperStartCamelObject}}Model) FindOneByCondition(ctx context.C
if session != nil {
err = session.QueryRowCtx(ctx, &resp, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowNoCacheCtx(ctx, &resp, statement, args...){{else}}err = m.conn.QueryRowCtx(ctx, &resp, statement, args...){{end}}
err = m.conn.QueryRowCtx(ctx, &resp, statement, args...)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -77,7 +77,7 @@ func (m *custom{{.upperStartCamelObject}}Model) PageByCondition(ctx context.Cont
if session != nil {
err = session.QueryRowsCtx(ctx, &resp, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowsNoCacheCtx(ctx, &resp, statement, args...){{else}}err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...){{end}}
err = m.conn.QueryRowsCtx(ctx, &resp, statement, args...)
}
if err != nil {
return nil, 0, err
Expand All @@ -88,7 +88,7 @@ func (m *custom{{.upperStartCamelObject}}Model) PageByCondition(ctx context.Cont
if session != nil {
err = session.QueryRowCtx(ctx, &total, statement, args...)
} else {
{{if .withCache}}err = m.QueryRowNoCacheCtx(ctx, &total, statement, args...){{else}}err = m.conn.QueryRowCtx(ctx, &total, statement, args...){{end}}
err = m.conn.QueryRowCtx(ctx, &total, statement, args...)
}
if err != nil {
return nil, 0, err
Expand Down Expand Up @@ -117,7 +117,7 @@ func (m *custom{{.upperStartCamelObject}}Model) UpdateFieldsByCondition(ctx cont
if session != nil {
_, err = session.ExecCtx(ctx, statement, args...)
} else {
{{if .withCache}}_, err = m.ExecNoCacheCtx(ctx, statement, args...){{else}}_, err = m.conn.ExecCtx(ctx, statement, args...){{end}}
_, err = m.conn.ExecCtx(ctx, statement, args...)
}
if err != nil {
return err
Expand All @@ -137,7 +137,7 @@ func (m *custom{{.upperStartCamelObject}}Model) DeleteByCondition(ctx context.Co
if session != nil {
_, err = session.ExecCtx(ctx, statement, args...)
} else {
{{if .withCache}}_, err = m.ExecNoCacheCtx(ctx, statement, args...){{else}}_, err = m.conn.ExecCtx(ctx, statement, args...){{end}}
_, err = m.conn.ExecCtx(ctx, statement, args...)
}
return err
}
42 changes: 23 additions & 19 deletions .template/go-zero/model/delete.tpl
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}})
if err!=nil{
return err
}

{{end}} {{.keys}}
_, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
sb := sqlbuilder.DeleteFrom(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sql, args := sb.Build()
if session != nil {
return session.ExecCtx(ctx, sql, args...)
}
return conn.ExecCtx(ctx, sql, args...)
}, {{.keyValues}}){{else}}sb := sqlbuilder.DeleteFrom(m.table)
sb := sqlbuilder.DeleteFrom(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sql, args := sb.Build()
statement, args := sb.Build()
var err error
if session != nil {
_, err = session.ExecCtx(ctx, sql, args...)
_, err = session.ExecCtx(ctx, statement, args...)
}else{
_, err = m.conn.ExecCtx(ctx, sql, args...)
_, err = m.conn.ExecCtx(ctx, statement, args...)
}
{{end}}
return err
}

func (m *default{{.upperStartCamelObject}}Model) DeleteWithCache(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
{{if .withCache}}{{if .containsIndexCache}}data, err := m.FindOne(ctx, session, {{.lowerStartCamelPrimaryKey}})
if err != nil{
return err
}

{{end}} {{.keys}}
_, err {{if .containsIndexCache}}={{else}}:={{end}} m.cachedConn.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
sb := sqlbuilder.DeleteFrom(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
statement, args := sb.Build()
if session != nil {
return session.ExecCtx(ctx, statement, args...)
}
return conn.ExecCtx(ctx, statement, args...)
}, {{.keyValues}})
return err{{else}}return m.Delete(ctx, session, {{.lowerStartCamelPrimaryKey}}){{end}}
}
55 changes: 29 additions & 26 deletions .template/go-zero/model/find-one-by-field.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error) {
var resp {{.upperStartCamelObject}}
var err error

sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb.Where(sb.EQ(strings.Split(strings.ReplaceAll("{{.originalField}}", " ", ""), "=")[0], {{.lowerStartCamelField}}))
sb.Limit(1)

sql, args := sb.Build()

if session != nil {
err = session.QueryRowCtx(ctx, &resp, sql, args...)
} else {
err = m.conn.QueryRowCtx(ctx, &resp, sql, args...)
}

switch err {
case nil:
return &resp, nil
case sqlx.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}WithCache(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error) {
{{if .withCache}}{{.cacheKey}}
var resp {{.upperStartCamelObject}}
err := m.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
err := m.cachedConn.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb.Where(sb.EQ(strings.Split(strings.ReplaceAll("{{.originalField}}", " ", ""), "=")[0], {{.lowerStartCamelField}}))
sb.Limit(1)
Expand All @@ -25,28 +51,5 @@ func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx co
return nil, ErrNotFound
default:
return nil, err
}
}{{else}}var resp {{.upperStartCamelObject}}
var err error

sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb.Where(sb.EQ(strings.Split(strings.ReplaceAll("{{.originalField}}", " ", ""), "=")[0], {{.lowerStartCamelField}}))
sb.Limit(1)

sql, args := sb.Build()

if session != nil {
err = session.QueryRowCtx(ctx, &resp, sql, args...)
} else {
err = m.conn.QueryRowCtx(ctx, &resp, sql, args...)
}

switch err {
case nil:
return &resp, nil
case sqlx.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}{{end}}
}{{else}}return m.FindOneBy{{.upperField}}(ctx, session, {{ $length := len .upperField }}{{ printf "%.*s" $length .in }}){{end}}
}
44 changes: 24 additions & 20 deletions .template/go-zero/model/find-one.tpl
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
{{if .withCache}}{{.cacheKey}}
var resp {{.upperStartCamelObject}}
err := m.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sql, args := sb.Build()
if session != nil {
return session.QueryRowCtx(ctx, v, sql, args...)
}
return conn.QueryRowCtx(ctx, v, sql, args...)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}{{else}}sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sb.Limit(1)
sql, args := sb.Build()
Expand All @@ -35,5 +17,27 @@ func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, se
return nil, ErrNotFound
default:
return nil, err
}{{end}}
}
}

func (m *default{{.upperStartCamelObject}}Model) FindOneWithCache(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
{{if .withCache}}{{.cacheKey}}
var resp {{.upperStartCamelObject}}
err := m.cachedConn.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
sb := sqlbuilder.Select({{.lowerStartCamelObject}}Rows).From(m.table)
sb.Where(sb.EQ("{{.originalPrimaryKey}}", {{.lowerStartCamelPrimaryKey}}))
sql, args := sb.Build()
if session != nil {
return session.QueryRowCtx(ctx, v, sql, args...)
}
return conn.QueryRowCtx(ctx, v, sql, args...)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}{{else}}return m.FindOne(ctx, session, {{.lowerStartCamelPrimaryKey}}){{end}}
}
3 changes: 2 additions & 1 deletion .template/go-zero/model/import-no-cache.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"strings"
{{if .time}}"time"{{end}}

{{if .containsPQ}}"github.com/lib/pq"{{end}}
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
"github.com/huandu/go-sqlbuilder"
"github.com/jzero-io/jzero-contrib/condition"
"github.com/jzero-io/jzero-contrib/modelx"
"github.com/eddieowens/opts"

{{.third}}
)
4 changes: 2 additions & 2 deletions .template/go-zero/model/import.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"strings"
{{if .time}}"time"{{end}}

{{if .containsPQ}}"github.com/lib/pq"{{end}}
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
"github.com/huandu/go-sqlbuilder"
"github.com/jzero-io/jzero-contrib/condition"
"github.com/jzero-io/jzero-contrib/modelx"
"github.com/eddieowens/opts"

{{.third}}
)
20 changes: 14 additions & 6 deletions .template/go-zero/model/insert.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, ses
InsertInto(m.table).
Cols({{.lowerStartCamelObject}}RowsExpectAutoSet).
Values({{.expressionValues}}).Build()
if session != nil {
return session.ExecCtx(ctx, statement, args...)
}
return m.conn.ExecCtx(ctx, statement, args...)
}

func (m *default{{.upperStartCamelObject}}Model) InsertWithCache(ctx context.Context, session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error) {
{{if .withCache}}{{.keys}}
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
statement, args := sqlbuilder.NewInsertBuilder().
InsertInto(m.table).
Cols({{.lowerStartCamelObject}}RowsExpectAutoSet).
Values({{.expressionValues}}).Build()
return m.cachedConn.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
if session != nil {
return session.ExecCtx(ctx, statement, args...)
}
return conn.ExecCtx(ctx, statement, args...)
}, {{.keyValues}}){{else}}if session != nil {
return session.ExecCtx(ctx, statement, args...)
}
return m.conn.ExecCtx(ctx, statement, args...){{end}}
}
}, {{.keyValues}}){{else}}return m.Insert(ctx, session, data){{end}}
}
3 changes: 2 additions & 1 deletion .template/go-zero/model/interface-delete.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Delete(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error
Delete(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error
DeleteWithCache(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error
3 changes: 2 additions & 1 deletion .template/go-zero/model/interface-find-one-by-field.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
FindOneBy{{.upperField}}(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error)
FindOneBy{{.upperField}}(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error)
FindOneBy{{.upperField}}WithCache(ctx context.Context, session sqlx.Session, {{.in}}) (*{{.upperStartCamelObject}}, error)
3 changes: 2 additions & 1 deletion .template/go-zero/model/interface-find-one.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
FindOne(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)
FindOne(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)
FindOneWithCache(ctx context.Context, session sqlx.Session, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)
3 changes: 2 additions & 1 deletion .template/go-zero/model/interface-insert.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Insert(ctx context.Context, session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error)
Insert(ctx context.Context, session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error)
InsertWithCache(ctx context.Context, session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error)
3 changes: 2 additions & 1 deletion .template/go-zero/model/interface-update.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Update(ctx context.Context, session sqlx.Session, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error
Update(ctx context.Context, session sqlx.Session, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error
UpdateWithCache(ctx context.Context, session sqlx.Session, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error
6 changes: 4 additions & 2 deletions .template/go-zero/model/model-new.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf, opts ...cache.Option{{end}}) *default{{.upperStartCamelObject}}Model {
func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn, op ...opts.Opt[modelx.ModelOpts]) *default{{.upperStartCamelObject}}Model {
{{if .withCache}}o := opts.DefaultApply(op...){{end}}
return &default{{.upperStartCamelObject}}Model{
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c, opts...){{else}}conn:conn{{end}},
{{if .withCache}}cachedConn: sqlc.NewConn(conn, o.CacheConf, o.CacheOpts...),{{end}}
conn: conn,
table: {{.table}},
}
}
Expand Down
20 changes: 4 additions & 16 deletions .template/go-zero/model/model.tpl
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package {{.pkg}}
{{if .withCache}}
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/jzero-io/jzero-contrib/modelx"
"github.com/eddieowens/opts"
)
{{else}}

import (
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
{{end}}
var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil)

type (
// {{.upperStartCamelObject}}Model is an interface to be customized, add more methods here,
// and implement the added methods in custom{{.upperStartCamelObject}}Model.
{{.upperStartCamelObject}}Model interface {
{{.lowerStartCamelObject}}Model
{{if not .withCache}}WithSession(session sqlx.Session) {{.upperStartCamelObject}}Model{{end}}
}

custom{{.upperStartCamelObject}}Model struct {
Expand All @@ -26,15 +20,9 @@ type (
)

// New{{.upperStartCamelObject}}Model returns a model for the database table.
func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf, opts ...cache.Option{{end}}) {{.upperStartCamelObject}}Model {
func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn, op ...opts.Opt[modelx.ModelOpts]) {{.upperStartCamelObject}}Model {
return &custom{{.upperStartCamelObject}}Model{
default{{.upperStartCamelObject}}Model: new{{.upperStartCamelObject}}Model(conn{{if .withCache}}, c, opts...{{end}}),
default{{.upperStartCamelObject}}Model: new{{.upperStartCamelObject}}Model(conn, op...),
}
}

{{if not .withCache}}
func (m *custom{{.upperStartCamelObject}}Model) WithSession(session sqlx.Session) {{.upperStartCamelObject}}Model {
return New{{.upperStartCamelObject}}Model(sqlx.NewSqlConnFromSession(session))
}
{{end}}

3 changes: 2 additions & 1 deletion .template/go-zero/model/types.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type (
}

default{{.upperStartCamelObject}}Model struct {
{{if .withCache}}sqlc.CachedConn{{else}}conn sqlx.SqlConn{{end}}
{{if .withCache}}cachedConn sqlc.CachedConn
conn sqlx.SqlConn{{else}}conn sqlx.SqlConn{{end}}
table string
}

Expand Down
Loading

0 comments on commit 4b127c0

Please sign in to comment.