Skip to content

Commit c42d4a8

Browse files
authored
feat(jzero): support set model cache expiry (#397)
* feat(jzero): support set model cache expiry feat(jzero): support set model cache expiry feat(jzero): support set model cache expiry feat(jzero): support set model cache expiry feat(jzero): support set model cache expiry * feat(jzero): update frame with features model/cache/redis
1 parent 1c20fa1 commit c42d4a8

11 files changed

Lines changed: 307 additions & 66 deletions

File tree

cmd/jzero/.template/frame/api/app/internal/config/config.go.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"github.com/zeromicro/go-zero/rest"
66
{{ if has "model" .Features }}"github.com/zeromicro/go-zero/core/stores/sqlx"{{ end }}
77
{{ if has "redis" .Features }}"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
8+
{{ if has "cache" .Features }}"github.com/zeromicro/go-zero/core/stores/cache"
9+
"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
810
)
911

1012
type Config struct {
1113
Rest RestConf
1214
Log LogConf
1315
{{ if has "model" .Features }}Sqlx SqlxConf{{ end }}
1416
{{ if has "redis" .Features }}Redis RedisConf{{ end }}
17+
{{ if has "cache" .Features }}Cache CacheConf{{ end }}
1518
Banner BannerConf
1619
}
1720

@@ -29,6 +32,11 @@ type LogConf struct {
2932
{{ if has "redis" .Features }}type RedisConf struct {
3033
redis.RedisConf
3134
}{{ end }}
35+
{{ if has "cache" .Features }}type CacheConf struct {
36+
Expiry int64 `json:",default=300000"` // 默认 300s
37+
NotFoundExpiry int64 `json:",default=60000"` // 默认 60s
38+
Redis cache.CacheConf
39+
}{{ end }}
3240

3341
type BannerConf struct {
3442
Text string `json:",default=JZERO"`

cmd/jzero/.template/frame/api/app/internal/svc/e3tGb3JtYXRTdHlsZSAuU3R5bGUgInNlcnZpY2VfY29udGV4dCJ9fS5nby50cGw=

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package svc
22

33
import (
4+
"errors"
5+
"time"
6+
47
"github.com/jzero-io/jzero/core/configcenter"
58
{{ if has "model" .Features }}"github.com/jzero-io/jzero/core/stores/modelx"{{ end }}
6-
{{ if has "redis" .Features }}"github.com/jzero-io/jzero/core/stores/cache"
7-
"github.com/zeromicro/go-zero/core/stores/redis"
8-
"github.com/pkg/errors"{{ end }}
9+
{{ if has "redis" .Features }}"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
10+
{{ if has "cache" .Features }}"github.com/zeromicro/go-zero/core/stores/cache"
11+
"github.com/zeromicro/go-zero/core/syncx"{{ end }}
912

1013
"{{ .Module }}/internal/config"
1114
"{{ .Module }}/internal/middleware"
@@ -17,8 +20,8 @@ type ServiceContext struct {
1720
Middleware
1821
{{ if has "model" .Features }}SqlxConn sqlx.SqlConn
1922
Model model.Model{{ end }}
20-
{{ if has "redis" .Features }}RedisConn *redis.Redis
21-
Cache cache.Cache{{ end }}
23+
{{ if has "redis" .Features }}RedisConn *redis.Redis{{ end }}
24+
{{ if has "cache" .Features }}Cache cache.Cache{{ end }}
2225
}
2326

2427
func NewServiceContext(cc configcenter.ConfigCenter[config.Config]) *ServiceContext {
@@ -28,8 +31,21 @@ func NewServiceContext(cc configcenter.ConfigCenter[config.Config]) *ServiceCont
2831

2932
{{ if has "model" .Features }}svcCtx.SqlxConn = modelx.MustNewConn(cc.MustGetConfig().Sqlx.SqlConf){{ end }}
3033
{{ if has "redis" .Features }}svcCtx.RedisConn = redis.MustNewRedis(cc.MustGetConfig().Redis.RedisConf){{ end }}
31-
{{ if has "redis" .Features }}svcCtx.Cache = cache.NewRedisNode(svcCtx.RedisConn, errors.New("cache not found")){{ end }}
32-
{{ if and (has "model" .Features) (has "redis" .Features) }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn, modelx.WithCachedConn(modelx.NewConnWithCache(svcCtx.SqlxConn, svcCtx.Cache))){{else if has "model" .Features }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn){{ end }}
34+
{{ if has "cache" .Features }}svcCtx.Cache = cache.New(cc.MustGetConfig().Cache.Redis,
35+
syncx.NewSingleFlight(),
36+
cache.NewStat("redis-cache"),
37+
errors.New("cache not found"),
38+
cache.WithExpiry(time.Duration(cc.MustGetConfig().Cache.Expiry)*time.Millisecond),
39+
cache.WithNotFoundExpiry(time.Duration(cc.MustGetConfig().Cache.NotFoundExpiry)*time.Millisecond),
40+
){{ end }}
41+
{{ if and (has "model" .Features) (has "cache" .Features) }}svcCtx.Model = model.NewModel(
42+
svcCtx.SqlxConn,
43+
modelx.WithCacheConf(cc.MustGetConfig().Cache.Redis),
44+
modelx.WithCacheOpts(
45+
cache.WithExpiry(time.Duration(cc.MustGetConfig().Cache.Expiry)*time.Millisecond),
46+
cache.WithNotFoundExpiry(time.Duration(cc.MustGetConfig().Cache.NotFoundExpiry)*time.Millisecond),
47+
),
48+
){{else if has "model" .Features }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn){{ end }}
3349
svcCtx.SetConfigListener()
3450
svcCtx.Middleware = svcCtx.NewMiddleware()
3551
return svcCtx

cmd/jzero/.template/frame/gateway/app/internal/config/config.go.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/zeromicro/go-zero/zrpc"
77
{{ if has "model" .Features }}"github.com/zeromicro/go-zero/core/stores/sqlx"{{ end }}
88
{{ if has "redis" .Features }}"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
9+
{{ if has "cache" .Features }}"github.com/zeromicro/go-zero/core/stores/cache"
10+
"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
911
)
1012

1113
type Config struct {
@@ -14,6 +16,7 @@ type Config struct {
1416
Log LogConf
1517
{{ if has "model" .Features }}Sqlx SqlxConf{{ end }}
1618
{{ if has "redis" .Features }}Redis RedisConf{{ end }}
19+
{{ if has "cache" .Features }}Cache CacheConf{{ end }}
1720
Banner BannerConf
1821
}
1922

@@ -35,6 +38,11 @@ type LogConf struct {
3538
{{ if has "redis" .Features }}type RedisConf struct {
3639
redis.RedisConf
3740
}{{ end }}
41+
{{ if has "cache" .Features }}type CacheConf struct {
42+
Expiry int64 `json:",default=300000"` // 默认 300s
43+
NotFoundExpiry int64 `json:",default=60000"` // 默认 60s
44+
Redis cache.CacheConf
45+
}{{ end }}
3846

3947
type BannerConf struct {
4048
Text string `json:",default=JZERO"`
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package svc
22

33
import (
4-
"github.com/jzero-io/jzero/core/configcenter"
4+
"errors"
5+
"time"
6+
7+
"github.com/jzero-io/jzero/core/configcenter"
58
{{ if has "model" .Features }}"github.com/jzero-io/jzero/core/stores/modelx"{{ end }}
6-
{{ if has "redis" .Features }}"github.com/jzero-io/jzero/core/stores/cache"
7-
"github.com/zeromicro/go-zero/core/stores/redis"
8-
"github.com/pkg/errors"{{ end }}
9+
{{ if has "redis" .Features }}"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
10+
{{ if has "cache" .Features }}"github.com/zeromicro/go-zero/core/stores/cache"
11+
"github.com/zeromicro/go-zero/core/syncx"{{ end }}
912

1013
"{{ .Module }}/internal/config"
1114
{{ if has "model" .Features }}"{{ .Module }}/internal/model"{{ end }}
@@ -15,8 +18,8 @@ type ServiceContext struct {
1518
ConfigCenter configcenter.ConfigCenter[config.Config]
1619
{{ if has "model" .Features }}SqlxConn sqlx.SqlConn
1720
Model model.Model{{ end }}
18-
{{ if has "redis" .Features }}RedisConn *redis.Redis
19-
Cache cache.Cache{{ end }}
21+
{{ if has "redis" .Features }}RedisConn *redis.Redis{{ end }}
22+
{{ if has "cache" .Features }}Cache cache.Cache{{ end }}
2023
}
2124

2225
func NewServiceContext(cc configcenter.ConfigCenter[config.Config]) *ServiceContext {
@@ -26,8 +29,21 @@ func NewServiceContext(cc configcenter.ConfigCenter[config.Config]) *ServiceCont
2629

2730
{{ if has "model" .Features }}svcCtx.SqlxConn = modelx.MustNewConn(cc.MustGetConfig().Sqlx.SqlConf){{ end }}
2831
{{ if has "redis" .Features }}svcCtx.RedisConn = redis.MustNewRedis(cc.MustGetConfig().Redis.RedisConf){{ end }}
29-
{{ if has "redis" .Features }}svcCtx.Cache = cache.NewRedisNode(svcCtx.RedisConn, errors.New("cache not found")){{ end }}
30-
{{ if and (has "model" .Features) (has "redis" .Features) }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn, modelx.WithCachedConn(modelx.NewConnWithCache(svcCtx.SqlxConn, svcCtx.Cache))){{else if has "model" .Features }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn){{ end }}
32+
{{ if has "cache" .Features }}svcCtx.Cache = cache.New(cc.MustGetConfig().Cache.Redis,
33+
syncx.NewSingleFlight(),
34+
cache.NewStat("redis-cache"),
35+
errors.New("cache not found"),
36+
cache.WithExpiry(time.Duration(cc.MustGetConfig().Cache.Expiry)*time.Millisecond),
37+
cache.WithNotFoundExpiry(time.Duration(cc.MustGetConfig().Cache.NotFoundExpiry)*time.Millisecond),
38+
){{ end }}
39+
{{ if and (has "model" .Features) (has "cache" .Features) }}svcCtx.Model = model.NewModel(
40+
svcCtx.SqlxConn,
41+
modelx.WithCacheConf(cc.MustGetConfig().Cache.Redis),
42+
modelx.WithCacheOpts(
43+
cache.WithExpiry(time.Duration(cc.MustGetConfig().Cache.Expiry)*time.Millisecond),
44+
cache.WithNotFoundExpiry(time.Duration(cc.MustGetConfig().Cache.NotFoundExpiry)*time.Millisecond),
45+
),
46+
){{else if has "model" .Features }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn){{ end }}
3147
svcCtx.SetConfigListener()
3248
return svcCtx
3349
}

cmd/jzero/.template/frame/rpc/app/internal/config/config.go.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"github.com/zeromicro/go-zero/zrpc"
66
{{ if has "model" .Features }}"github.com/zeromicro/go-zero/core/stores/sqlx"{{ end }}
77
{{ if has "redis" .Features }}"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
8+
{{ if has "cache" .Features }}"github.com/zeromicro/go-zero/core/stores/cache"
9+
"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
810
)
911

1012
type Config struct {
1113
Zrpc ZrpcConf
1214
Log LogConf
1315
{{ if has "model" .Features }}Sqlx SqlxConf{{ end }}
1416
{{ if has "redis" .Features }}Redis RedisConf{{ end }}
17+
{{ if has "cache" .Features }}Cache CacheConf{{ end }}
1518
Banner BannerConf
1619
}
1720

@@ -29,6 +32,11 @@ type LogConf struct {
2932
{{ if has "redis" .Features }}type RedisConf struct {
3033
redis.RedisConf
3134
}{{ end }}
35+
{{ if has "cache" .Features }}type CacheConf struct {
36+
Expiry int64 `json:",default=300000"` // 默认 300s
37+
NotFoundExpiry int64 `json:",default=60000"` // 默认 60s
38+
Redis cache.CacheConf
39+
}{{ end }}
3240

3341
type BannerConf struct {
3442
Text string `json:",default=JZERO"`
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package svc
22

33
import (
4+
"errors"
5+
"time"
6+
47
"github.com/jzero-io/jzero/core/configcenter"
58
{{ if has "model" .Features }}"github.com/jzero-io/jzero/core/stores/modelx"{{ end }}
6-
{{ if has "redis" .Features }}"github.com/jzero-io/jzero/core/stores/cache"
7-
"github.com/zeromicro/go-zero/core/stores/redis"
8-
"github.com/pkg/errors"{{ end }}
9+
{{ if has "redis" .Features }}"github.com/zeromicro/go-zero/core/stores/redis"{{ end }}
10+
{{ if has "cache" .Features }}"github.com/zeromicro/go-zero/core/stores/cache"
11+
"github.com/zeromicro/go-zero/core/syncx"{{ end }}
912

1013
"{{ .Module }}/internal/config"
1114
{{ if has "model" .Features }}"{{ .Module }}/internal/model"{{ end }}
@@ -15,8 +18,8 @@ type ServiceContext struct {
1518
ConfigCenter configcenter.ConfigCenter[config.Config]
1619
{{ if has "model" .Features }}SqlxConn sqlx.SqlConn
1720
Model model.Model{{ end }}
18-
{{ if has "redis" .Features }}RedisConn *redis.Redis
19-
Cache cache.Cache{{ end }}
21+
{{ if has "redis" .Features }}RedisConn *redis.Redis{{ end }}
22+
{{ if has "cache" .Features }}Cache cache.Cache{{ end }}
2023
}
2124

2225
func NewServiceContext(cc configcenter.ConfigCenter[config.Config]) *ServiceContext {
@@ -26,8 +29,21 @@ func NewServiceContext(cc configcenter.ConfigCenter[config.Config]) *ServiceCont
2629

2730
{{ if has "model" .Features }}svcCtx.SqlxConn = modelx.MustNewConn(cc.MustGetConfig().Sqlx.SqlConf){{ end }}
2831
{{ if has "redis" .Features }}svcCtx.RedisConn = redis.MustNewRedis(cc.MustGetConfig().Redis.RedisConf){{ end }}
29-
{{ if has "redis" .Features }}svcCtx.Cache = cache.NewRedisNode(svcCtx.RedisConn, errors.New("cache not found")){{ end }}
30-
{{ if and (has "model" .Features) (has "redis" .Features) }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn, modelx.WithCachedConn(modelx.NewConnWithCache(svcCtx.SqlxConn, svcCtx.Cache))){{else if has "model" .Features }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn){{ end }}
32+
{{ if has "cache" .Features }}svcCtx.Cache = cache.New(cc.MustGetConfig().Cache.Redis,
33+
syncx.NewSingleFlight(),
34+
cache.NewStat("redis-cache"),
35+
errors.New("cache not found"),
36+
cache.WithExpiry(time.Duration(cc.MustGetConfig().Cache.Expiry)*time.Millisecond),
37+
cache.WithNotFoundExpiry(time.Duration(cc.MustGetConfig().Cache.NotFoundExpiry)*time.Millisecond),
38+
){{ end }}
39+
{{ if and (has "model" .Features) (has "cache" .Features) }}svcCtx.Model = model.NewModel(
40+
svcCtx.SqlxConn,
41+
modelx.WithCacheConf(cc.MustGetConfig().Cache.Redis),
42+
modelx.WithCacheOpts(
43+
cache.WithExpiry(time.Duration(cc.MustGetConfig().Cache.Expiry)*time.Millisecond),
44+
cache.WithNotFoundExpiry(time.Duration(cc.MustGetConfig().Cache.NotFoundExpiry)*time.Millisecond),
45+
),
46+
){{else if has "model" .Features }}svcCtx.Model = model.NewModel(svcCtx.SqlxConn){{ end }}
3147
svcCtx.SetConfigListener()
3248
return svcCtx
3349
}

cmd/jzero/.template/model/model.go.tpl

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,134 @@
33
package model
44

55
import (
6+
"time"
7+
68
"github.com/zeromicro/go-zero/core/stores/sqlx"
79
"github.com/jzero-io/jzero/core/stores/modelx"
810
"github.com/eddieowens/opts"
11+
"github.com/zeromicro/go-zero/core/stores/cache"
912

1013
{{range $v := .ImportsWithAlias}}{{if $v.Alias}}{{$v.Alias}}{{end}} "{{$v.Path}}"
1114
{{end}}
1215
)
1316

17+
{{if .ModelExpiryTable}}
18+
var (
19+
ModelExpiryTable map[string]int64 = map[string]int64{
20+
{{range $k, $v := .ModelExpiryTable}}"{{$k}}": {{$v}},
21+
{{end}}
22+
}
23+
)
24+
{{end}}
25+
26+
{{if .ModelNotFoundExpiryTable}}
27+
var (
28+
ModelNotFoundExpiryTable map[string]int64 = map[string]int64{
29+
{{range $k, $v := .ModelNotFoundExpiryTable}}"{{$k}}": {{$v}},
30+
{{end}}
31+
}
32+
)
33+
{{end}}
34+
1435
{{range $k,$v := .MutiModelsWithAlias}} type {{$k | FirstUpper | ToCamel}}Model struct {
1536
{{range $vv := $v}}{{$vv.Name | FirstUpper | ToCamel}} {{$vv.Alias}}.{{$vv.Name | FirstUpper |ToCamel}}Model
1637
{{end}}
1738
}
1839
{{end}}
1940

2041
type Model struct {
21-
{{range $v := .TablePackages}}{{$v | FirstUpper | ToCamel}} {{$v}}.{{$v | FirstUpper |ToCamel}}Model
42+
{{range $v := .TableInfos}}{{$v.Name | FirstUpper | ToCamel}} {{$v.Name}}.{{$v.Name | FirstUpper |ToCamel}}Model
2243
{{end}}
2344
}
2445

46+
{{if .ModelNewOriginal}}
47+
{{range $v := .TableInfos}}
48+
func NewOriginal{{$v.Name | FirstUpper | ToCamel}}Model(conn sqlx.SqlConn{{if and $.ModelCache $v.WithCache}}, c cache.CacheConf, op ...cache.Option{{end}}) {{$v.Name}}.{{$v.Name | FirstUpper | ToCamel}}Model {
49+
{{if and $.ModelCache $v.WithCache}}
50+
{{if $v.HasCacheExpiry}}
51+
if expiry, ok := ModelExpiryTable["{{$v.Name}}"]; ok {
52+
op = append(op, cache.WithExpiry(time.Duration(expiry)*time.Millisecond))
53+
}
54+
{{end}}
55+
{{if $v.HasNotFoundExpiry}}
56+
if notFoundExpiry, ok := ModelNotFoundExpiryTable["{{$v.Name}}"]; ok {
57+
op = append(op, cache.WithNotFoundExpiry(time.Duration(notFoundExpiry)*time.Millisecond))
58+
}
59+
{{end}}
60+
return {{$v.Name}}.NewOriginal{{$v.Name | FirstUpper | ToCamel}}Model(conn, c, op...)
61+
{{else}}
62+
return {{$v.Name}}.NewOriginal{{$v.Name | FirstUpper | ToCamel}}Model(conn)
63+
{{end}}
64+
}
65+
{{end}}
66+
{{end}}
67+
68+
{{if .ModelNewOriginal}}
69+
{{range $k,$v := .MutiModelsWithAlias}}
70+
{{range $vv := $v}}
71+
func NewOriginal{{$k | FirstUpper | ToCamel}}{{$vv.Name | FirstUpper | ToCamel}}Model(conn sqlx.SqlConn{{if and $.ModelCache $vv.WithCache}}, c cache.CacheConf, op ...cache.Option{{end}}) {{$vv.Alias}}.{{$vv.Name | FirstUpper |ToCamel}}Model {
72+
{{if and $.ModelCache $vv.WithCache}}
73+
{{if $vv.HasCacheExpiry}}
74+
if expiry, ok := ModelExpiryTable["{{$vv.FullName}}"]; ok {
75+
op = append(op, cache.WithExpiry(time.Duration(expiry)*time.Millisecond))
76+
}
77+
{{end}}
78+
{{if $vv.HasNotFoundExpiry}}
79+
if notFoundExpiry, ok := ModelNotFoundExpiryTable["{{$vv.FullName}}"]; ok {
80+
op = append(op, cache.WithNotFoundExpiry(time.Duration(notFoundExpiry)*time.Millisecond))
81+
}
82+
{{end}}
83+
return {{$vv.Alias}}.NewOriginal{{$vv.Name | FirstUpper | ToCamel}}Model(conn, c, op...)
84+
{{else}}
85+
return {{$vv.Alias}}.NewOriginal{{$vv.Name | FirstUpper | ToCamel}}Model(conn)
86+
{{end}}
87+
}
88+
{{end}}
89+
{{end}}
90+
{{end}}
91+
2592
func NewModel(conn sqlx.SqlConn, op ...opts.Opt[modelx.ModelOpts]) Model {
93+
{{range $v := .TableInfos}}
94+
{{if and $.ModelCache (or $v.HasCacheExpiry $v.HasNotFoundExpiry)}}
95+
{{$v.Name | ToCamel}}CacheOpts := opts.DefaultApply(op...).CacheOpts
96+
{{if $v.HasCacheExpiry}}
97+
if expiry, ok := ModelExpiryTable["{{$v.Name}}"]; ok {
98+
{{$v.Name | ToCamel}}CacheOpts = append({{$v.Name | ToCamel}}CacheOpts, cache.WithExpiry(time.Duration(expiry)*time.Millisecond))
99+
}
100+
{{end}}
101+
{{if $v.HasNotFoundExpiry}}
102+
if notFoundExpiry, ok := ModelNotFoundExpiryTable["{{$v.Name}}"]; ok {
103+
{{$v.Name | ToCamel}}CacheOpts = append({{$v.Name | ToCamel}}CacheOpts, cache.WithNotFoundExpiry(time.Duration(notFoundExpiry)*time.Millisecond))
104+
}
105+
{{end}}
106+
{{end}}
107+
{{end}}
108+
26109
return Model{
27-
{{range $v := .TablePackages}}{{$v | FirstUpper | ToCamel}}: {{$v}}.New{{ $v | FirstUpper | ToCamel }}Model(conn, op...),
110+
{{range $v := .TableInfos}}{{if and $.ModelCache (or $v.HasCacheExpiry $v.HasNotFoundExpiry)}}{{$v.Name | FirstUpper | ToCamel}}: {{$v.Name}}.New{{ $v.Name | FirstUpper | ToCamel }}Model(conn, append(op, modelx.WithCacheOpts({{$v.Name | ToCamel}}CacheOpts...))...),{{else}}{{$v.Name | FirstUpper | ToCamel}}: {{$v.Name}}.New{{ $v.Name | FirstUpper | ToCamel }}Model(conn, op...),{{end}}
28111
{{end}}
29112
}
30113
}
31114

32115
{{range $k,$v := .MutiModelsWithAlias}} func New{{$k | FirstUpper | ToCamel}}Model(conn sqlx.SqlConn, op ...opts.Opt[modelx.ModelOpts]) {{$k | FirstUpper | ToCamel}}Model {
116+
{{range $vv := $v}}
117+
{{if and $.ModelCache (or $vv.HasCacheExpiry $vv.HasNotFoundExpiry)}}
118+
{{$vv.Name | ToCamel}}CacheOpts := opts.DefaultApply(op...).CacheOpts
119+
{{if $vv.HasCacheExpiry}}
120+
if expiry, ok := ModelExpiryTable["{{$vv.FullName}}"]; ok {
121+
{{$vv.Name | ToCamel}}CacheOpts = append({{$vv.Name | ToCamel}}CacheOpts, cache.WithExpiry(time.Duration(expiry)*time.Millisecond))
122+
}
123+
{{end}}
124+
{{if $vv.HasNotFoundExpiry}}
125+
if notFoundExpiry, ok := ModelNotFoundExpiryTable["{{$vv.FullName}}"]; ok {
126+
{{$vv.Name | ToCamel}}CacheOpts = append({{$vv.Name | ToCamel}}CacheOpts, cache.WithNotFoundExpiry(time.Duration(notFoundExpiry)*time.Millisecond))
127+
}
128+
{{end}}
129+
{{end}}
130+
{{end}}
131+
33132
return {{$k | FirstUpper | ToCamel}}Model{
34-
{{range $vv := $v}}{{$vv.Name | FirstUpper | ToCamel}}: {{$vv.Alias}}.New{{ $vv.Name | FirstUpper | ToCamel }}Model(conn, op...),
133+
{{range $vv := $v}}{{$vv.Name | FirstUpper | ToCamel}}: {{$vv.Alias}}.New{{ $vv.Name | FirstUpper | ToCamel }}Model(conn, {{if and $.ModelCache (or $vv.HasCacheExpiry $vv.HasNotFoundExpiry)}}append(op, modelx.WithCacheOpts({{$vv.Name | ToCamel}}CacheOpts...))...{{else}}op...{{end}}),
35134
{{end}}
36135
}
37136
}

0 commit comments

Comments
 (0)