update
This commit is contained in:
@@ -11,29 +11,27 @@ import (
|
||||
|
||||
type RedisInventoryService struct {
|
||||
client *redis.Client
|
||||
ctx context.Context
|
||||
luaScripts map[string]*redis.Script
|
||||
}
|
||||
|
||||
func NewRedisInventoryService(client *redis.Client, ctx context.Context) (*RedisInventoryService, error) {
|
||||
func NewRedisInventoryService(client *redis.Client) (*RedisInventoryService, error) {
|
||||
rdb := client
|
||||
|
||||
// Ping Redis to check connection
|
||||
_, err := rdb.Ping(ctx).Result()
|
||||
_, err := rdb.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &RedisInventoryService{
|
||||
client: rdb,
|
||||
ctx: ctx,
|
||||
luaScripts: make(map[string]*redis.Script),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *RedisInventoryService) LoadLuaScript(key string) error {
|
||||
// Get the script from Redis
|
||||
script, err := s.client.Get(s.ctx, key).Result()
|
||||
script, err := s.client.Get(context.Background(), key).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -53,13 +51,13 @@ func (s *RedisInventoryService) AddWarehouse(warehouse *Warehouse) error {
|
||||
|
||||
// Store in Redis with a key pattern like "warehouse:<ID>"
|
||||
key := "warehouse:" + string(warehouse.ID)
|
||||
_, err := s.client.HMSet(s.ctx, key, data).Result()
|
||||
_, err := s.client.HMSet(context.Background(), key, data).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *RedisInventoryService) GetInventory(sku SKU, locationID LocationID) (int64, error) {
|
||||
func (s *RedisInventoryService) GetInventory(ctx context.Context, sku SKU, locationID LocationID) (int64, error) {
|
||||
|
||||
cmd := s.client.Get(s.ctx, getInventoryKey(sku, locationID))
|
||||
cmd := s.client.Get(ctx, getInventoryKey(sku, locationID))
|
||||
if err := cmd.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -76,9 +74,9 @@ func getInventoryKey(sku SKU, locationID LocationID) string {
|
||||
return fmt.Sprintf("inventory:%s:%s", sku, locationID)
|
||||
}
|
||||
|
||||
func (s *RedisInventoryService) UpdateInventory(rdb redis.Pipeliner, sku SKU, locationID LocationID, quantity int64) error {
|
||||
func (s *RedisInventoryService) UpdateInventory(ctx context.Context, rdb redis.Pipeliner, sku SKU, locationID LocationID, quantity int64) error {
|
||||
key := getInventoryKey(sku, locationID)
|
||||
cmd := rdb.Set(s.ctx, key, quantity, 0)
|
||||
cmd := rdb.Set(ctx, key, quantity, 0)
|
||||
return cmd.Err()
|
||||
}
|
||||
|
||||
@@ -101,7 +99,7 @@ func makeKeysAndArgs(req ...ReserveRequest) ([]string, []string) {
|
||||
return keys, args
|
||||
}
|
||||
|
||||
func (s *RedisInventoryService) ReservationCheck(req ...ReserveRequest) (*ReserveRequest, error) {
|
||||
func (s *RedisInventoryService) ReservationCheck(ctx context.Context, req ...ReserveRequest) (*ReserveRequest, error) {
|
||||
if len(req) == 0 {
|
||||
return nil, ErrMissingReservation
|
||||
}
|
||||
@@ -111,7 +109,7 @@ func (s *RedisInventoryService) ReservationCheck(req ...ReserveRequest) (*Reserv
|
||||
return nil, ErrInvalidQuantity
|
||||
}
|
||||
|
||||
cmd := reservationCheck.Run(s.ctx, s.client, keys, args)
|
||||
cmd := reservationCheck.Run(ctx, s.client, keys, args)
|
||||
if err := cmd.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,7 +122,7 @@ func (s *RedisInventoryService) ReservationCheck(req ...ReserveRequest) (*Reserv
|
||||
return &req[failingIndex], ErrInsufficientInventory
|
||||
}
|
||||
|
||||
func (s *RedisInventoryService) ReserveInventory(req ...ReserveRequest) error {
|
||||
func (s *RedisInventoryService) ReserveInventory(ctx context.Context, req ...ReserveRequest) error {
|
||||
if len(req) == 0 {
|
||||
return ErrMissingReservation
|
||||
}
|
||||
@@ -133,7 +131,7 @@ func (s *RedisInventoryService) ReserveInventory(req ...ReserveRequest) error {
|
||||
if keys == nil || args == nil {
|
||||
return ErrInvalidQuantity
|
||||
}
|
||||
cmd := reserveScript.Run(s.ctx, s.client, keys, args)
|
||||
cmd := reserveScript.Run(ctx, s.client, keys, args)
|
||||
if err := cmd.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package inventory
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SKU string
|
||||
|
||||
@@ -19,9 +22,9 @@ type Warehouse struct {
|
||||
}
|
||||
|
||||
type InventoryService interface {
|
||||
GetInventory(sku SKU, locationID LocationID) (int64, error)
|
||||
ReserveInventory(req ...ReserveRequest) error
|
||||
ReservationCheck(req ...ReserveRequest) (*ReserveRequest, error)
|
||||
GetInventory(ctx context.Context, sku SKU, locationID LocationID) (int64, error)
|
||||
ReserveInventory(ctx context.Context, req ...ReserveRequest) error
|
||||
ReservationCheck(ctx context.Context, req ...ReserveRequest) (*ReserveRequest, error)
|
||||
}
|
||||
|
||||
type ReserveRequest struct {
|
||||
|
||||
Reference in New Issue
Block a user