Skip to content

Commit

Permalink
replace cache key with colon (zeromicro#746)
Browse files Browse the repository at this point in the history
Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
  • Loading branch information
kesonan and anqiansong authored Jun 2, 2021
1 parent 75a3301 commit 453f949
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tools/goctl/model/mongo/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/tal-tech/go-zero/core/stores/mongoc"
)
{{if .Cache}}var prefix{{.Type}}CacheKey = "cache#{{.Type}}#"{{end}}
{{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.Type}}:"{{end}}
type {{.Type}}Model interface{
Insert(ctx context.Context,data *{{.Type}}) error
Expand Down
10 changes: 5 additions & 5 deletions tools/goctl/model/sql/gen/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
type Key struct {
// VarLeft describes the variable of cache key expression which likes cacheUserIdPrefix
VarLeft string
// VarRight describes the value of cache key expression which likes "cache#user#id#"
// VarRight describes the value of cache key expression which likes "cache:user:id:"
VarRight string
// VarExpression describes the cache key expression which likes cacheUserIdPrefix = "cache#user#id#"
// VarExpression describes the cache key expression which likes cacheUserIdPrefix = "cache:user:id:"
VarExpression string
// KeyLeft describes the variable of key definition expression which likes userKey
KeyLeft string
Expand Down Expand Up @@ -76,12 +76,12 @@ func genCacheKey(table stringx.String, in []*parser.Field) Key {
keyLeftJoin = append(keyLeftJoin, "key")

varLeft = varLeftJoin.Camel().With("").Untitle()
varRight = fmt.Sprintf(`"%s"`, varRightJon.Camel().Untitle().With("#").Source()+"#")
varRight = fmt.Sprintf(`"%s"`, varRightJon.Camel().Untitle().With(":").Source()+":")
varExpression = fmt.Sprintf(`%s = %s`, varLeft, varRight)

keyLeft = keyLeftJoin.Camel().With("").Untitle()
keyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With("").Source(), varLeft, keyRightJoin.With(", ").Source())
dataKeyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With("").Source(), varLeft, dataRightJoin.With(", ").Source())
keyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With(":").Source(), varLeft, keyRightJoin.With(", ").Source())
dataKeyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With(":").Source(), varLeft, dataRightJoin.With(", ").Source())
keyExpression = fmt.Sprintf("%s := %s", keyLeft, keyRight)
dataKeyExpression = fmt.Sprintf("%s := %s", keyLeft, dataKeyRight)

Expand Down
20 changes: 10 additions & 10 deletions tools/goctl/model/sql/gen/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func TestGenCacheKeys(t *testing.T) {
assert.Equal(t, true, func() bool {
return cacheKeyEqual(primariCacheKey, Key{
VarLeft: "cacheUserIdPrefix",
VarRight: `"cache#user#id#"`,
VarExpression: `cacheUserIdPrefix = "cache#user#id#"`,
VarRight: `"cache:user:id:"`,
VarExpression: `cacheUserIdPrefix = "cache:user:id:"`,
KeyLeft: "userIdKey",
KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
Expand All @@ -95,19 +95,19 @@ func TestGenCacheKeys(t *testing.T) {
expected := []Key{
{
VarLeft: "cacheUserClassNamePrefix",
VarRight: `"cache#user#class#name#"`,
VarExpression: `cacheUserClassNamePrefix = "cache#user#class#name#"`,
VarRight: `"cache:user:class:name:"`,
VarExpression: `cacheUserClassNamePrefix = "cache:user:class:name:"`,
KeyLeft: "userClassNameKey",
KeyRight: `fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, class, name)`,
DataKeyRight: `fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
KeyExpression: `userClassNameKey := fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, class, name)`,
DataKeyExpression: `userClassNameKey := fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
KeyRight: `fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, class, name)`,
DataKeyRight: `fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
KeyExpression: `userClassNameKey := fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, class, name)`,
DataKeyExpression: `userClassNameKey := fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
FieldNameJoin: []string{"class", "name"},
},
{
VarLeft: "cacheUserMobilePrefix",
VarRight: `"cache#user#mobile#"`,
VarExpression: `cacheUserMobilePrefix = "cache#user#mobile#"`,
VarRight: `"cache:user:mobile:"`,
VarExpression: `cacheUserMobilePrefix = "cache:user:mobile:"`,
KeyLeft: "userMobileKey",
KeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)`,
Expand Down

0 comments on commit 453f949

Please sign in to comment.