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