update inventory package
This commit is contained in:
@@ -6,6 +6,8 @@ A Go library for managing inventory using Redis.
|
||||
|
||||
- Get and update inventory quantities
|
||||
- Reserve inventory with atomic operations using Lua scripts
|
||||
- Temporary cart-based reservations with expiration to prevent overselling
|
||||
- Optimized available inventory calculation using maintained reservation totals
|
||||
- Listen to inventory changes via Redis pub/sub
|
||||
|
||||
## Installation
|
||||
@@ -19,7 +21,7 @@ go get github.com/matst80/go-redis-inventory
|
||||
Import the package:
|
||||
|
||||
```go
|
||||
import "github.com/matst80/go-redis-inventory/pkg/inventory"
|
||||
import "git.k6n.net/go-redis-inventory/pkg/inventory"
|
||||
```
|
||||
|
||||
Create a Redis client and initialize the service:
|
||||
@@ -34,6 +36,32 @@ if err != nil {
|
||||
}
|
||||
```
|
||||
|
||||
For cart reservations:
|
||||
|
||||
```go
|
||||
cartService, err := inventory.NewRedisCartReservationService(client)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Reserve items for a cart with 10-minute TTL
|
||||
err = cartService.ReserveForCart(ctx, inventory.CartReserveRequest{
|
||||
InventoryReference: &inventory.InventoryReference{SKU: "item1", LocationID: "warehouse1"},
|
||||
CartID: "cart123",
|
||||
Quantity: 2,
|
||||
TTL: 10 * time.Minute,
|
||||
})
|
||||
|
||||
// Get available inventory (accounting for reservations)
|
||||
available, err := cartService.GetAvailableInventory(ctx, "item1", "warehouse1")
|
||||
|
||||
// Get reservation status and remaining time
|
||||
active, remaining, err := cartService.GetReservationStatus(ctx, "item1", "warehouse1", "cart123")
|
||||
if active {
|
||||
fmt.Printf("Reservation active, %v remaining\n", remaining)
|
||||
}
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
- Go 1.25.1 or later
|
||||
|
||||
Reference in New Issue
Block a user