Files
go-cart-actor/cmd/inventory/main.go
matst80 7c0e3e84a2
Some checks failed
Build and Publish / Metadata (push) Successful in 11s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled
Build and Publish / BuildAndDeployArm64 (push) Has been cancelled
implement redis inventory with threadsafe reservation
2025-11-04 18:13:06 +01:00

55 lines
1.3 KiB
Go

package main
import (
"context"
"log"
"git.tornberg.me/go-cart-actor/pkg/inventory"
"github.com/redis/go-redis/v9"
"github.com/redis/go-redis/v9/maintnotifications"
)
func main() {
var ctx = context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "10.10.3.18:6379",
Password: "slaskredis", // no password set
DB: 0, // use default DB
MaintNotificationsConfig: &maintnotifications.Config{
Mode: maintnotifications.ModeDisabled,
},
})
s, err := inventory.NewRedisInventoryService(rdb, ctx)
if err != nil {
log.Fatalf("Unable to connect to inventory redis", err)
return
}
rdb.Pipelined(ctx, func(p redis.Pipeliner) error {
s.UpdateInventory(p, "1", "1", 10)
s.UpdateInventory(p, "2", "2", 20)
s.UpdateInventory(p, "3", "3", 30)
s.UpdateInventory(p, "4", "4", 40)
return nil
})
err = s.ReserveInventory(inventory.ReserveRequest{
SKU: "1",
LocationID: "1",
Quantity: 3,
}, inventory.ReserveRequest{
SKU: "2",
LocationID: "2",
Quantity: 15,
})
if err != nil {
log.Printf("Unable to reserve inventory: %v", err)
return
}
v, err := s.GetInventory("1", "1")
if err != nil {
log.Printf("Unable to get inventory: %v", err)
return
}
log.Printf("Inventory after reservation: %v", v)
}