@@ -12,6 +12,7 @@ import (
1212 "github.com/zeromicro/go-zero/core/logx"
1313 "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
1414
15+ "github.com/jzero-io/jzero/cmd/jzero/internal/config"
1516 "github.com/jzero-io/jzero/cmd/jzero/internal/embeded"
1617 "github.com/jzero-io/jzero/cmd/jzero/internal/pkg/templatex"
1718)
@@ -21,9 +22,13 @@ type ImportWithAlias struct {
2122 Path string
2223}
2324
24- type NameWithAlias struct {
25- Alias string
26- Name string
25+ type TableInfo struct {
26+ Alias string
27+ Name string
28+ FullName string // 完整表名,如 "log.user"
29+ WithCache bool
30+ HasCacheExpiry bool
31+ HasNotFoundExpiry bool
2732}
2833
2934func (jm * JzeroModel ) GenRegister (tables []string ) error {
@@ -34,9 +39,10 @@ func (jm *JzeroModel) GenRegister(tables []string) error {
3439 var imports []string
3540 var importsWithAlias []ImportWithAlias
3641 var tablePackages []string
42+ var tableInfos []TableInfo
3743
3844 mutiModels := make (map [string ][]string )
39- mutiModelsWithAlias := make (map [string ][]NameWithAlias )
45+ mutiModelsWithAlias := make (map [string ][]TableInfo )
4046
4147 for _ , t := range tables {
4248 var isMutiModel bool
@@ -57,30 +63,91 @@ func (jm *JzeroModel) GenRegister(tables []string) error {
5763 Alias : strings .ReplaceAll (strings .ReplaceAll (t , "." , "_" ), "-" , "_" ),
5864 Path : fmt .Sprintf ("%s/internal/model/%s" , jm .Module , strings .ToLower (filepath .ToSlash (tf ))),
5965 })
60- mutiModels [filepath .Dir (tf )] = append (mutiModels [filepath .Dir (tf )], strings .ToLower (filepath .Base (tf )))
61- mutiModelsWithAlias [filepath .Dir (tf )] = append (mutiModelsWithAlias [filepath .Dir (tf )], NameWithAlias {
62- Alias : strings .ReplaceAll (strings .ReplaceAll (t , "." , "_" ), "-" , "_" ),
63- Name : strings .ToLower (filepath .Base (tf )),
66+ tableName := strings .ToLower (filepath .Base (tf ))
67+ mutiModels [filepath .Dir (tf )] = append (mutiModels [filepath .Dir (tf )], tableName )
68+ // 将路径分隔符替换为点,如 "log/user" -> "log.user"
69+ fullName := strings .ReplaceAll (strings .ToLower (tf ), "/" , "." )
70+ mutiModelsWithAlias [filepath .Dir (tf )] = append (mutiModelsWithAlias [filepath .Dir (tf )], TableInfo {
71+ Alias : strings .ReplaceAll (strings .ReplaceAll (t , "." , "_" ), "-" , "_" ),
72+ Name : tableName ,
73+ FullName : fullName , // 完整表名,如 "log.user"
74+ WithCache : getIsCacheTable (t ),
6475 })
6576 } else {
6677 imports = append (imports , fmt .Sprintf ("%s/internal/model/%s" , jm .Module , strings .ToLower (filepath .ToSlash (tf ))))
6778 importsWithAlias = append (importsWithAlias , ImportWithAlias {
6879 Path : fmt .Sprintf ("%s/internal/model/%s" , jm .Module , strings .ToLower (filepath .ToSlash (tf ))),
6980 })
7081 tablePackages = append (tablePackages , strings .ToLower (tf ))
82+ tableInfos = append (tableInfos , TableInfo {
83+ Name : strings .ToLower (tf ),
84+ WithCache : getIsCacheTable (t ),
85+ })
7186 }
7287 }
7388
7489 logx .Debugf ("get register imports: %v" , imports )
7590 logx .Debugf ("get register table packages: %v" , tablePackages )
7691 logx .Debugf ("get register muti models: %v" , mutiModels )
7792
93+ // Build cache expiry table maps - only when cache is enabled
94+ var modelExpiryTable map [string ]int64
95+ var modelNotFoundExpiryTable map [string ]int64
96+ if config .C .Gen .ModelCache {
97+ modelExpiryTable = make (map [string ]int64 )
98+ modelNotFoundExpiryTable = make (map [string ]int64 )
99+ for _ , v := range config .C .Gen .ModelCacheExpiryTable {
100+ if v .Expiry > 0 {
101+ modelExpiryTable [v .Table ] = v .Expiry
102+ }
103+ if v .NotFoundExpiry > 0 {
104+ modelNotFoundExpiryTable [v .Table ] = v .NotFoundExpiry
105+ }
106+ }
107+ }
108+
109+ // Update tableInfos with cache expiry info
110+ for i := range tableInfos {
111+ if modelExpiryTable != nil {
112+ if _ , ok := modelExpiryTable [tableInfos [i ].Name ]; ok {
113+ tableInfos [i ].HasCacheExpiry = true
114+ }
115+ }
116+ if modelNotFoundExpiryTable != nil {
117+ if _ , ok := modelNotFoundExpiryTable [tableInfos [i ].Name ]; ok {
118+ tableInfos [i ].HasNotFoundExpiry = true
119+ }
120+ }
121+ }
122+
123+ // Update mutiModelsWithAlias with cache expiry info
124+ for k := range mutiModelsWithAlias {
125+ for i := range mutiModelsWithAlias [k ] {
126+ fullName := mutiModelsWithAlias [k ][i ].FullName
127+ if modelExpiryTable != nil {
128+ if _ , ok := modelExpiryTable [fullName ]; ok {
129+ mutiModelsWithAlias [k ][i ].HasCacheExpiry = true
130+ }
131+ }
132+ if modelNotFoundExpiryTable != nil {
133+ if _ , ok := modelNotFoundExpiryTable [fullName ]; ok {
134+ mutiModelsWithAlias [k ][i ].HasNotFoundExpiry = true
135+ }
136+ }
137+ }
138+ }
139+
78140 template , err := templatex .ParseTemplate (filepath .Join ("model" , "model.go.tpl" ), map [string ]any {
79- "Imports" : imports ,
80- "ImportsWithAlias" : importsWithAlias ,
81- "TablePackages" : tablePackages ,
82- "MutiModels" : mutiModels , // 兼容
83- "MutiModelsWithAlias" : mutiModelsWithAlias , // 兼容
141+ "Imports" : imports ,
142+ "ImportsWithAlias" : importsWithAlias ,
143+ "TablePackages" : tablePackages ,
144+ "TableInfos" : tableInfos ,
145+ "MutiModels" : mutiModels , // 兼容
146+ "MutiModelsWithAlias" : mutiModelsWithAlias , // 兼容
147+ "ModelExpiryTable" : modelExpiryTable ,
148+ "ModelNotFoundExpiryTable" : modelNotFoundExpiryTable ,
149+ "ModelCache" : config .C .Gen .ModelCache ,
150+ "ModelNewOriginal" : config .C .Gen .ModelNewOriginal ,
84151 }, lo .If (
85152 // 兼容老版本 model 路径
86153 // TODO: wait to remove
0 commit comments