Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache connection test and close #4411

Open
kingsley8524267 opened this issue Oct 6, 2024 · 2 comments
Open

Cache connection test and close #4411

kingsley8524267 opened this issue Oct 6, 2024 · 2 comments

Comments

@kingsley8524267
Copy link

kingsley8524267 commented Oct 6, 2024

I have checked the related cache issues and documents here, but I have not found how to verify the connection of the cache connection during NewServiceContext to ensure that the configuration connection is correct, and how to close the cache conn?

package svc

import (
	"bookstore/rpc/check/internal/config"
	"bookstore/rpc/model"
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	c     config.Config
	Model model.BookModel // 手动代码
}

func NewServiceContext(c config.Config) (*ServiceContext, error) {
	mysql := sqlx.NewMysql(c.DataSource)

	db, err := mysql.RawDB()
	if err != nil {
		return nil, err
	}

	if err = db.PingContext(context.Background()); err != nil {
		return nil, err
	}

	return &ServiceContext{
		c:     c,
		Model: model.NewBookModel(mysql, c.Cache), // 手动代码
	}, nil
}
@kingsley8524267 kingsley8524267 changed the title 缓存连接测试和关闭 Cache connection test and close Oct 6, 2024
@MarkJoyMa
Copy link
Collaborator

Set the cache configuration in redis to NonBlock = false, and a redis ping check will be performed when creating the cache.

@kingsley8524267
Copy link
Author

It working! But how to close the cache connection or go-zero will automatically close the cache connection while context done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants