go-zero
go-zero copied to clipboard
How can i add expiration time for redis
I get trouble setting expiration time for redis. How can i do that.
Do I need to add in config.go or .yaml file? Please show me with example code.
Use ExpireCtx
Use
ExpireCtx
So If I want to add expire time in the method of model from goctl generation, I need to add method custom model with redis function?
var r redis.Redis = redis.Redis{
Addr: "127.0.0.1:6379",
}
type (
UserModel interface {
userModel
FindAll(ctx context.Context) ([]*User, error)
FindOneByUsername(ctx context.Context, username string) (*User, error)
}
)
func (m *customUserModel) FindOneByUsername(ctx context.Context, username string) (*User, error) {
userUsernameKey := fmt.Sprintf("%s%v", cacheUserUsernamePrefix, username)
var resp User
err := m.QueryRowIndexCtx(ctx, &resp, userUsernameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", userRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, username); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
cuname := fmt.Sprintf("cache:user:username:%v", "strange")
err := r.ExpireCtx(ctx, cuname, 10)
if err != nil {
return &resp, err
}
err = r.ExpireCtx(ctx, fmt.Sprintf("cache:user:id:%v", 1), 10)
if err != nil {
return &resp, err
}
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
but I'm keep getting this error
{
"@timestamp":"2022-09-22T15:24:14.334+06:30",
"caller":"internal/log.go:57",
"content":"(/v1/users/strange - 127.0.0.1:51864) runtime error: invalid memory address or nil pointer dereference\ngoroutine 86 [running]:\nruntime/debug.Stack()\n\t/usr/local/go/src/runtime/debug/stack.go:24 +0x65\ngithub.com/zeromicro/go-zero/rest/handler.RecoverHandler.func1.1()\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/rest/handler/recoverhandler.go:16 +0x66\npanic({0xc5d300, 0x13d6ec0})\n\t/usr/local/go/src/runtime/panic.go:884 +0x212\ngithub.com/zeromicro/go-zero/core/stores/redis.(*Redis).ExpireCtx(0x13e1480, {0xe7cd00?, 0xc000301080}, {0xc0004440a0, 0x1b}, 0xa)\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/core/stores/redis/redis.go:409 +0xda\nzero-strange/service/authentication/model.(*customUserModel).FindOneByUsername(0xc00041c078, {0xe7cd00, 0xc000301080}, {0xc00015654e, 0x7})\n\t/home/sithu/go/src/zero-strange/service/authentication/model/usermodel.go:67 +0x354\nzero-strange/service/broker/api/internal/logic.(*GetuserLogic).Getuser(0xc000417cf8?, 0xc000128008?)\n\t/home/sithu/go/src/zero-strange/service/broker/api/internal/logic/getuserlogic.go:34 +0xcf\nzero-strange/service/broker/api/internal/handler.getuserHandler.func1({0xe7bdb0, 0xc0003010e0}, 0xc00065d400)\n\t/home/sithu/go/src/zero-strange/service/broker/api/internal/handler/getuserhandler.go:17 +0x16c\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0xe7bdb0?, 0xc0003010e0?}, 0x0?)\n\t/usr/local/go/src/net/http/server.go:2109 +0x2f\ngithub.com/zeromicro/go-zero/rest/handler.GunzipHandler.func1({0xe7bdb0, 0xc0003010e0}, 0xc00065d400)\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/rest/handler/gunziphandler.go:26 +0xe9\nnet/http.HandlerFunc.ServeHTTP(0xedabe19b6?, {0xe7bdb0?, 0xc0003010e0?}, 0x237cc472b?)\n\t/usr/local/go/src/net/http/server.go:2109 +0x2f\ngithub.com/zeromicro/go-zero/rest/handler.MaxBytesHandler.func2.1({0xe7bdb0?, 0xc0003010e0?}, 0x13f6820?)\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/rest/handler/maxbyteshandler.go:24 +0xff\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0xe7bdb0?, 0xc0003010e0?}, 0x0?)\n\t/usr/local/go/src/net/http/server.go:2109 +0x2f\ngithub.com/zeromicro/go-zero/rest/handler.MetricHandler.func1.1({0xe7bdb0, 0xc0003010e0}, 0x0?)\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/rest/handler/metrichandler.go:21 +0xd5\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0xe7bdb0?, 0xc0003010e0?}, 0x0?)\n\t/usr/local/go/src/net/http/server.go:2109 +0x2f\ngithub.com/zeromicro/go-zero/rest/handler.RecoverHandler.func1({0xe7bdb0?, 0xc0003010e0?}, 0x0?)\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/rest/handler/recoverhandler.go:21 +0x83\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0xe7bdb0?, 0xc0003010e0?}, 0x0?)\n\t/usr/local/go/src/net/http/server.go:2109 +0x2f\ngithub.com/zeromicro/go-zero/rest/handler.(*timeoutHandler).ServeHTTP.func1()\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/rest/handler/timeouthandler.go:79 +0x7c\ncreated by github.com/zeromicro/go-zero/rest/handler.(*timeoutHandler).ServeHTTP\n\t/home/sithu/go/pkg/mod/github.com/zeromicro/[email protected]/rest/handler/timeouthandler.go:73 +0x33b\n",
"level":"error",
"span":"98dedc689766d375",
"trace":"aac21cce11574cb22e799f269e8a46a1"
}
Here is my github repo link.
Create a new Cache using cache.New
https://github.com/zeromicro/go-zero/blob/1a6d7b3ef692e9c49b2cc8018ee3593ea2ebc0f7/core/stores/cache/cache.go#L58
I have the same problem,I found that it is possible to add a method like the following in the xx_gen.go
file。
But this file is marked as unmodifiable, and it will be overwritten every time it is generated. I think it can be specified through a template, but it is not scientific to use the same expiration time for all models.
I think it's better to open this configuration to another file and pass it in as a parameter。
Modify the generated xx.go.