Files
go-redis-inventory/README.md
T
matsandClaude Opus 4.8 7b25571a23 Merge 51b30de (more fancy features) into preupdate
Resolve README install URL to the git.k6n.net module path (matches go.mod
and the import examples); keep the cart-reservation additions. go mod tidy
promotes miniredis to a direct dep (used in tests).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 22:01:46 +02:00

67 lines
1.7 KiB
Markdown

# Go Redis Inventory
A Go library for managing inventory using Redis.
## Features
- 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
```bash
go get git.k6n.net/mats/go-redis-inventory
```
## Usage
Import the package:
```go
import "git.k6n.net/mats/go-redis-inventory/pkg/inventory"
```
Create a Redis client and initialize the service:
```go
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
service, err := inventory.NewRedisInventoryService(client)
if err != nil {
log.Fatal(err)
}
```
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 summary for an item
summary, err := cartService.GetReservationSummary(ctx, "item1", "warehouse1")
fmt.Printf("Reservations: %d, Next release: %v, Remaining: %d\n", summary.ReservationCount, summary.EarliestRelease, summary.RemainingItems)
```
## Dependencies
- Go 1.25.1 or later
- Redis server
- [github.com/redis/go-redis/v9](https://github.com/redis/go-redis)