-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
81 additions
and
47 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ethanvc/evo/base" | ||
"github.com/ethanvc/evo/plog" | ||
"github.com/redis/go-redis/v9" | ||
"go.uber.org/fx" | ||
"google.golang.org/grpc/codes" | ||
"log/slog" | ||
) | ||
|
||
func main() { | ||
fx.New().Run() | ||
} | ||
|
||
// https://learn.microsoft.com/en-gb/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5 | ||
// DTO对象,用于传输和对外输出实体属性 | ||
|
||
type QueryUserReq struct { | ||
Uid int64 | ||
} | ||
|
||
type UserDto struct { | ||
} | ||
|
||
type userController struct { | ||
redisCli redis.UniversalClient | ||
} | ||
|
||
func (controller *userController) QueryUser(c context.Context, req *QueryUserReq) (resp *UserDto, err error) { | ||
// query redis cache, case can downgrade | ||
switch base.Code(err) { | ||
case codes.OK: | ||
plog.ReportEvent(c, "UserFoundInCache") | ||
return | ||
case codes.NotFound: | ||
plog.ReportEvent(c, "UserNotFoundInCache") | ||
default: | ||
plog.ReportErrEvent(c, "UserCacheUnknownErr") | ||
slog.ErrorContext(c, "UserCacheUnknownErr") | ||
} | ||
return | ||
} | ||
|
||
func (controller *userController) queryUserFromCache(c context.Context, req *QueryUserReq) (resp *UserDto, err error) { | ||
cmd := controller.redisCli.Get(c, fmt.Sprintf("a_%d", req.Uid)) | ||
if cmd.Err() != nil { | ||
return nil, cmd.Err() | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters