replace cache key with colon (#746)

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
master
anqiansong 4 years ago committed by GitHub
parent 75a330184d
commit 453f949638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,7 +11,7 @@ import (
"github.com/tal-tech/go-zero/core/stores/mongoc" "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{ type {{.Type}}Model interface{
Insert(ctx context.Context,data *{{.Type}}) error Insert(ctx context.Context,data *{{.Type}}) error

@ -13,9 +13,9 @@ import (
type Key struct { type Key struct {
// VarLeft describes the variable of cache key expression which likes cacheUserIdPrefix // VarLeft describes the variable of cache key expression which likes cacheUserIdPrefix
VarLeft string 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 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 VarExpression string
// KeyLeft describes the variable of key definition expression which likes userKey // KeyLeft describes the variable of key definition expression which likes userKey
KeyLeft string KeyLeft string
@ -76,12 +76,12 @@ func genCacheKey(table stringx.String, in []*parser.Field) Key {
keyLeftJoin = append(keyLeftJoin, "key") keyLeftJoin = append(keyLeftJoin, "key")
varLeft = varLeftJoin.Camel().With("").Untitle() 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) varExpression = fmt.Sprintf(`%s = %s`, varLeft, varRight)
keyLeft = keyLeftJoin.Camel().With("").Untitle() keyLeft = keyLeftJoin.Camel().With("").Untitle()
keyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With("").Source(), varLeft, keyRightJoin.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()) dataKeyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With(":").Source(), varLeft, dataRightJoin.With(", ").Source())
keyExpression = fmt.Sprintf("%s := %s", keyLeft, keyRight) keyExpression = fmt.Sprintf("%s := %s", keyLeft, keyRight)
dataKeyExpression = fmt.Sprintf("%s := %s", keyLeft, dataKeyRight) dataKeyExpression = fmt.Sprintf("%s := %s", keyLeft, dataKeyRight)

@ -78,8 +78,8 @@ func TestGenCacheKeys(t *testing.T) {
assert.Equal(t, true, func() bool { assert.Equal(t, true, func() bool {
return cacheKeyEqual(primariCacheKey, Key{ return cacheKeyEqual(primariCacheKey, Key{
VarLeft: "cacheUserIdPrefix", VarLeft: "cacheUserIdPrefix",
VarRight: `"cache#user#id#"`, VarRight: `"cache:user:id:"`,
VarExpression: `cacheUserIdPrefix = "cache#user#id#"`, VarExpression: `cacheUserIdPrefix = "cache:user:id:"`,
KeyLeft: "userIdKey", KeyLeft: "userIdKey",
KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`, KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`, DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
@ -95,19 +95,19 @@ func TestGenCacheKeys(t *testing.T) {
expected := []Key{ expected := []Key{
{ {
VarLeft: "cacheUserClassNamePrefix", VarLeft: "cacheUserClassNamePrefix",
VarRight: `"cache#user#class#name#"`, VarRight: `"cache:user:class:name:"`,
VarExpression: `cacheUserClassNamePrefix = "cache#user#class#name#"`, VarExpression: `cacheUserClassNamePrefix = "cache:user:class:name:"`,
KeyLeft: "userClassNameKey", KeyLeft: "userClassNameKey",
KeyRight: `fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, class, name)`, KeyRight: `fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, class, name)`,
DataKeyRight: `fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, data.Class, data.Name)`, DataKeyRight: `fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
KeyExpression: `userClassNameKey := fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, class, name)`, KeyExpression: `userClassNameKey := fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, class, name)`,
DataKeyExpression: `userClassNameKey := fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, data.Class, data.Name)`, DataKeyExpression: `userClassNameKey := fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
FieldNameJoin: []string{"class", "name"}, FieldNameJoin: []string{"class", "name"},
}, },
{ {
VarLeft: "cacheUserMobilePrefix", VarLeft: "cacheUserMobilePrefix",
VarRight: `"cache#user#mobile#"`, VarRight: `"cache:user:mobile:"`,
VarExpression: `cacheUserMobilePrefix = "cache#user#mobile#"`, VarExpression: `cacheUserMobilePrefix = "cache:user:mobile:"`,
KeyLeft: "userMobileKey", KeyLeft: "userMobileKey",
KeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)`, KeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)`, DataKeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)`,

Loading…
Cancel
Save