Skip to content

Commit

Permalink
feat: 将stock存储改为redis (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx authored Jan 30, 2024
1 parent 464688b commit 09bceb0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 51 deletions.
7 changes: 2 additions & 5 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ func (s *TradeServerImpl) GetStocks(ctx context.Context, req *trade.GetStocksReq
return s.StockService.GetStocks(ctx, req)
}

func (s *TradeServerImpl) CreateStock(ctx context.Context, req *trade.CreateStockReq) (resp *trade.CreateStockResp, err error) {
return s.StockService.CreateStock(ctx, req)
}
func (s *TradeServerImpl) UpdateStock(ctx context.Context, req *trade.UpdateStockReq) (resp *trade.UpdateStockResp, err error) {
return s.StockService.UpdateStock(ctx, req)
func (s *TradeServerImpl) AddStock(ctx context.Context, req *trade.AddStockReq) (resp *trade.AddStockResp, err error) {
return s.StockService.AddStock(ctx, req)
}
64 changes: 27 additions & 37 deletions biz/application/service/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package service

import (
"context"
"fmt"
"github.com/CloudStriver/cloudmind-trade/biz/infrastructure/config"
stockmapper "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/stock"
gentrade "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/trade"
"github.com/google/wire"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/stores/redis"
"go.mongodb.org/mongo-driver/bson/primitive"
"strconv"
)

type StockService interface {
GetStocks(ctx context.Context, req *gentrade.GetStocksReq) (resp *gentrade.GetStocksResp, err error)
GetStock(ctx context.Context, req *gentrade.GetStockReq) (resp *gentrade.GetStockResp, err error)
UpdateStock(ctx context.Context, req *gentrade.UpdateStockReq) (resp *gentrade.UpdateStockResp, err error)
CreateStock(ctx context.Context, req *gentrade.CreateStockReq) (resp *gentrade.CreateStockResp, err error)
AddStock(ctx context.Context, req *gentrade.AddStockReq) (resp *gentrade.AddStockResp, err error)
}

var StockSet = wire.NewSet(
Expand All @@ -24,59 +23,50 @@ var StockSet = wire.NewSet(
)

type StockServiceImpl struct {
Config *config.Config
Redis *redis.Redis
StockMongoMapper stockmapper.IStockMongoMapper
}

func (s *StockServiceImpl) CreateStock(ctx context.Context, req *gentrade.CreateStockReq) (resp *gentrade.CreateStockResp, err error) {
resp = new(gentrade.CreateStockResp)
oid, _ := primitive.ObjectIDFromHex(req.ProductId)
if _, err = s.StockMongoMapper.Insert(ctx, &stockmapper.Stock{
ID: oid,
Amount: lo.ToPtr(req.Stock),
}); err != nil {
return resp, err
}
return resp, nil
Config *config.Config
Redis *redis.Redis
}

func (s *StockServiceImpl) GetStocks(ctx context.Context, req *gentrade.GetStocksReq) (resp *gentrade.GetStocksResp, err error) {
resp = new(gentrade.GetStocksResp)
stocks, err := s.StockMongoMapper.FindMany(ctx, req.ProductIds)

req.ProductIds = lo.Map(req.ProductIds, func(item string, _ int) string {
return fmt.Sprintf("stock:%s", item)
})

stocks, err := s.Redis.MgetCtx(ctx, req.ProductIds...)
if err != nil {
return resp, err
}

index := make(map[string]int, len(req.ProductIds))
lo.ForEach[string](req.ProductIds, func(v string, i int) {
index[v] = i
})

resp.Stocks = make([]int64, len(req.ProductIds), len(req.ProductIds))
lo.ForEach[*stockmapper.Stock](stocks, func(v *stockmapper.Stock, _ int) {
resp.Stocks[index[v.ID.Hex()]] = *v.Amount
resp.Stocks = lo.Map[string, int64](stocks, func(item string, _ int) int64 {
if item == "" {
return 0
} else {
stock, _ := strconv.ParseInt(item, 10, 64)
return stock
}
})
return resp, nil
}

func (s *StockServiceImpl) GetStock(ctx context.Context, req *gentrade.GetStockReq) (resp *gentrade.GetStockResp, err error) {
resp = new(gentrade.GetStockResp)
stock, err := s.StockMongoMapper.FindOne(ctx, req.ProductId)
val, err := s.Redis.GetCtx(ctx, fmt.Sprintf("stock:%s", req.ProductId))
if err != nil {
return resp, err
}
resp.Stock = *stock.Amount
if val == "" {
resp.Stock = 0
return resp, nil
}
resp.Stock, _ = strconv.ParseInt(val, 10, 64)
return resp, nil
}

func (s *StockServiceImpl) UpdateStock(ctx context.Context, req *gentrade.UpdateStockReq) (resp *gentrade.UpdateStockResp, err error) {
resp = new(gentrade.UpdateStockResp)
oid, _ := primitive.ObjectIDFromHex(req.ProductId)
if _, err = s.StockMongoMapper.Update(ctx, &stockmapper.Stock{
ID: oid,
Amount: lo.ToPtr(req.Amount),
}, lo.ToPtr(req.OldAmount)); err != nil {
func (s *StockServiceImpl) AddStock(ctx context.Context, req *gentrade.AddStockReq) (resp *gentrade.AddStockResp, err error) {
resp = new(gentrade.AddStockResp)
if _, err := s.Redis.IncrbyCtx(ctx, fmt.Sprintf("stock:%s", req.ProductId), req.Amount); err != nil {
return resp, err
}
return resp, nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240119095418-bddcb5ccb1c3
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085
github.com/cloudwego/kitex v0.8.0
github.com/google/wire v0.5.0
github.com/kitex-contrib/obs-opentelemetry v0.2.5
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af h1:tl3MgdfL4fOkZxFYqqLXYr6dEk5TvT/hGe06uoB+spY=
github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af/go.mod h1:RMjN80WnoDiqHZIsv27u9BxJ9axldr+elFRHgSjhXnY=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240119095418-bddcb5ccb1c3 h1:W1BXEGDRI6Jh8VDK0+iT4IwdK0bEMpclCAx6aJdCWLI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240119095418-bddcb5ccb1c3/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085 h1:slsYIWCHTDv8Z5N+cZ3TY6fvmrsNuJhCrliljQOBP2w=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
Expand Down
2 changes: 0 additions & 2 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/CloudStriver/cloudmind-trade/biz/application/service"
"github.com/CloudStriver/cloudmind-trade/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/balance"
"github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/stock"
"github.com/CloudStriver/cloudmind-trade/biz/infrastructure/stores/redis"
"github.com/google/wire"
)
Expand All @@ -26,6 +25,5 @@ var InfrastructureSet = wire.NewSet(
)

var MapperSet = wire.NewSet(
stock.NewMongoMapper,
balance.NewMongoMapper,
)
7 changes: 2 additions & 5 deletions provider/wire_gen.go

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

0 comments on commit 09bceb0

Please sign in to comment.