feat: implement HashArray utility and refactor product key generation for Redis

This commit is contained in:
2025-08-17 11:46:44 +03:00
parent e35d44dacb
commit fb1be5ddc3
6 changed files with 40 additions and 14 deletions

View File

@@ -1,8 +1,11 @@
package main package main
//package main import "sipro-mps/pkg/utils"
// package main
//
// import (
// //
//import (
// "context" // "context"
// "fmt" // "fmt"
// "sipro-mps/internal/wb/products/mapping/generated" // "sipro-mps/internal/wb/products/mapping/generated"
@@ -12,15 +15,26 @@ package main
// "sipro-mps/pkg/api/wb/client" // "sipro-mps/pkg/api/wb/client"
// //
// "github.com/deliveryhero/pipeline/v2" // "github.com/deliveryhero/pipeline/v2"
//)
// //
//func main() { // )
// //
// input := "1,2,3,4,5" // func main() {
// //
// for out := range pipeline.Process(context.Background(), apply, pipeline.Emit(input)) { // input := "1,2,3,4,5"
// for j := range out { //
// fmt.Printf("process: %s\n", out[j]) // for out := range pipeline.Process(context.Background(), apply, pipeline.Emit(input)) {
// for j := range out {
// fmt.Printf("process: %s\n", out[j])
// }
// } // }
// } // }
//} func main() {
arr := []string{"1", "2", "3", "4", "5"}
hash, err := utils.HashArray(arr)
if err != nil {
panic(err)
}
println("Hash of array:", hash)
}

View File

@@ -22,6 +22,10 @@ type apiRepository struct {
marketplaceRepository marketplace.Repository marketplaceRepository marketplace.Repository
} }
func GetProductsKey(identifier string) string {
return fmt.Sprintf("ozon:products:%s", identifier)
}
func NewAPIRepository(marketplaceRepository marketplace.Repository) Repository { func NewAPIRepository(marketplaceRepository marketplace.Repository) Repository {
return &apiRepository{ return &apiRepository{
marketplaceRepository: marketplaceRepository, marketplaceRepository: marketplaceRepository,
@@ -136,7 +140,7 @@ func (a *apiRepository) StreamAllProductsCache(ctx context.Context, marketplaceI
errChan <- fmt.Errorf("getting marketplace identifier: %w", err) errChan <- fmt.Errorf("getting marketplace identifier: %w", err)
return return
} }
key := fmt.Sprintf("ozon:products:%s", identifier) key := GetProductsKey(identifier)
var cachedMessage pb.GetListOfProductsResponse var cachedMessage pb.GetListOfProductsResponse
err = redis.ReadProtoMessage(ctx, key, &cachedMessage) err = redis.ReadProtoMessage(ctx, key, &cachedMessage)
if err == nil && len(cachedMessage.Products) > 0 { if err == nil && len(cachedMessage.Products) > 0 {

View File

@@ -53,7 +53,7 @@ func (p *FetchProductsProcessor) ProcessTask(ctx context.Context, task *asynq.Ta
productsProto := lo.Map(productsRaw, func(item products.OzonProduct, _ int) *products.PbProduct { productsProto := lo.Map(productsRaw, func(item products.OzonProduct, _ int) *products.PbProduct {
return converter.ToProto(&item) return converter.ToProto(&item)
}) })
redisKey := fmt.Sprintf("ozon:products:%s", identifier) redisKey := products.GetProductsKey(identifier)
err = redis.WriteProtoMessage(ctx, redisKey, &pb.GetListOfProductsResponse{Products: productsProto}) err = redis.WriteProtoMessage(ctx, redisKey, &pb.GetListOfProductsResponse{Products: productsProto})
if err != nil { if err != nil {
fmt.Printf("Failed to write products to Redis for marketplace %s: %v\n", identifier, err) fmt.Printf("Failed to write products to Redis for marketplace %s: %v\n", identifier, err)

View File

@@ -46,7 +46,7 @@ func (p *FetchProductsProcessor) ProcessTask(ctx context.Context, task *asynq.Ta
defer cancel() defer cancel()
redisKey := fmt.Sprintf("wb:products:%s", sellerId) redisKey := products.GetProductsKey(sellerId)
productsRaw, err := repo.GetAllProducts(ctx, payload.MarketplaceId) productsRaw, err := repo.GetAllProducts(ctx, payload.MarketplaceId)
if err != nil { if err != nil {
return fmt.Errorf("failed to fetch products for marketplace %d: %w", payload.MarketplaceId, err) return fmt.Errorf("failed to fetch products for marketplace %d: %w", payload.MarketplaceId, err)

View File

@@ -25,6 +25,10 @@ type apiRepository struct {
marketplaceRepository marketplace.Repository marketplaceRepository marketplace.Repository
} }
func GetProductsKey(identifier string) string {
return fmt.Sprintf("wb:products:%s", identifier)
}
func (a apiRepository) ParseMarketplace(ctx context.Context, marketplaceId int) (*marketplace.Marketplace, string, error) { func (a apiRepository) ParseMarketplace(ctx context.Context, marketplaceId int) (*marketplace.Marketplace, string, error) {
marketplaceByID, err := a.marketplaceRepository.GetMarketplaceByID(ctx, marketplaceId) marketplaceByID, err := a.marketplaceRepository.GetMarketplaceByID(ctx, marketplaceId)
if err != nil { if err != nil {
@@ -110,7 +114,7 @@ func (a apiRepository) StreamAllProductsCache(ctx context.Context, marketplaceId
converter := generated.ConverterImpl{} converter := generated.ConverterImpl{}
key := fmt.Sprintf("wb:products:%s", identifier) key := GetProductsKey(identifier)
var cachedMessage pb.GetProductsResponse var cachedMessage pb.GetProductsResponse
err = redis.ReadProtoMessage(ctx, key, &cachedMessage) err = redis.ReadProtoMessage(ctx, key, &cachedMessage)
if err == nil && len(cachedMessage.Products) > 0 { if err == nil && len(cachedMessage.Products) > 0 {

View File

@@ -39,6 +39,10 @@ func NewAPIRepository(marketplaceRepository marketplace.Repository) Repository {
} }
} }
func GetProductKey(identifier string) string {
return fmt.Sprintf("ym:products:%s", identifier)
}
// getBusinessID retrieves the business ID for a given marketplace by looking up the campaign // getBusinessID retrieves the business ID for a given marketplace by looking up the campaign
func (r *apiRepository) getBusinessID(ctx context.Context, mp *marketplace.Marketplace) (int64, error) { func (r *apiRepository) getBusinessID(ctx context.Context, mp *marketplace.Marketplace) (int64, error) {
if mp.CampaignID == "" { if mp.CampaignID == "" {
@@ -101,7 +105,7 @@ func (r *apiRepository) GetProducts(ctx context.Context, marketplaceID int, req
errChan <- err errChan <- err
return return
} }
key := fmt.Sprintf("ym:products:%d", businessID) key := GetProductKey(strconv.Itoa(int(businessID)))
var cachedMessage pb.GetProductsResponse var cachedMessage pb.GetProductsResponse
err = redis.ReadProtoMessage(ctx, key, &cachedMessage) err = redis.ReadProtoMessage(ctx, key, &cachedMessage)
if err == nil && len(cachedMessage.Offers) > 0 { if err == nil && len(cachedMessage.Offers) > 0 {