Skip to content

Commit

Permalink
Merge pull request #16 from go-eagle/feat/add_singleflight
Browse files Browse the repository at this point in the history
feat: use singleflight to get data
  • Loading branch information
qloog authored Nov 14, 2024
2 parents d3b9cfa + 47450f6 commit b729c71
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 100 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ all: lint test build
.PHONY: build
# make build, Build the binary file
build: wire
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -ldflags ${ldflags} -o bin/$(SERVICE_NAME) cmd/server/main.go cmd/server/wire_gen.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -race -ldflags ${ldflags} -o bin/$(SERVICE_NAME) cmd/server/main.go cmd/server/wire_gen.go
# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -ldflags ${ldflags} -o bin/$(SERVICE_NAME)-consumer cmd/consumer/main.go cmd/consumer/wire_gen.go

.PHONY: run
Expand Down Expand Up @@ -75,7 +75,7 @@ lint:
.PHONY: test
# make test
test: vet
@go test -short ${PKG_LIST}
@go test -race -short ${PKG_LIST}

.PHONY: vet
# make vet
Expand Down
138 changes: 70 additions & 68 deletions api/user/v1/user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/user/v1/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package user.v1;

import "validate/validate.proto";
import "google/api/annotations.proto";
import "gogo/protobuf/gogo.proto";

option go_package = "github.com/go-eagle/eagle-layout/api/user/v1;v1";

Expand Down Expand Up @@ -190,6 +191,7 @@ message GetUserReply {

message BatchGetUsersRequest {
repeated int64 ids = 1;
// int64 ids = 1 [(gogoproto.moretags) = 'form:"ids"'];
}

message BatchGetUsersReply {
Expand Down
5 changes: 2 additions & 3 deletions api/user/v1/user_gin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions cmd/gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ func main() {
dal.Init()

g := gen.NewGenerator(gen.Config{
OutPath: "./internal/dal/db/dao", // 生成的查询类代码的输出路径,默认./query, 这里改为dao
ModelPkgPath: "./internal/dal/db/model", // 默认情况下会跟随OutPath参数,在同目录下生成model目录
Mode: gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
WithUnitTest: true, // 为生成的查询类代码生成单元测试
OutPath: "./internal/dal/db/dao", // 生成的查询类代码的输出路径,默认./query, 这里改为dao
ModelPkgPath: "./internal/dal/db/model", // 默认情况下会跟随OutPath参数,在同目录下生成model目录
Mode: gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
WithUnitTest: true, // 为生成的查询类代码生成单元测试
FieldNullable: true, // 对于数据库中nullable的数据,在生成代码中自动对应为指针类型
FieldWithIndexTag: true, // 从数据库同步的表结构代码包含gorm的index tag
FieldWithTypeTag: true, // 同步的表结构代码包含gorm的type tag(数据库中对应数据类型)
})

g.UseDB(dal.DB) // reuse your gorm db
Expand Down
2 changes: 1 addition & 1 deletion internal/dal/cache/user_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *userCache) GetUserCache(ctx context.Context, id int64) (data *model.Use
cacheKey := c.GetUserCacheKey(id)
err = c.cache.Get(ctx, cacheKey, &data)
if err != nil && !errors.Is(err, redis.Nil) {
log.WithContext(ctx).Warnf("get err from redis, err: %v", err)
log.WithContext(ctx).Warnf("[cache] GetUserCache err: %v", err)
return nil, err
}
return data, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
/ | \
/ | \
db rpc cache
```
```
Loading

0 comments on commit b729c71

Please sign in to comment.