| | |
| | | import java.math.BigDecimal; |
| | | import java.time.Duration; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | | @Service |
| | |
| | | public class WmsRedisLuaService { |
| | | |
| | | private static final DefaultRedisScript<Long> LOCATION_CLAIM_SCRIPT = createScript("wms-lua/location-claim.lua"); |
| | | private static final DefaultRedisScript<Long> STATION_CLAIM_SCRIPT = createScript("wms-lua/station-claim.lua"); |
| | | private static final DefaultRedisScript<Long> INVENTORY_RESERVE_SCRIPT = createScript("wms-lua/inventory-reserve.lua"); |
| | | |
| | | private final StringRedisTemplate redisTemplate; |
| | | |
| | | public boolean claimLocation(String occupyKey, String taskKey, String mode, String occupyValue, String taskValue, Duration ttl) { |
| | | public boolean claimLocation(String occupyKey, String occupyValue, Duration ttl) { |
| | | Long result = redisTemplate.execute( |
| | | LOCATION_CLAIM_SCRIPT, |
| | | Arrays.asList(occupyKey, taskKey), |
| | | mode, |
| | | List.of(occupyKey), |
| | | occupyValue, |
| | | String.valueOf(ttl.toMillis()) |
| | | ); |
| | | return result != null && result > 0; |
| | | } |
| | | |
| | | public boolean claimStation(String stationKey, String taskValue, Duration ttl) { |
| | | Long result = redisTemplate.execute( |
| | | STATION_CLAIM_SCRIPT, |
| | | List.of(stationKey), |
| | | taskValue, |
| | | String.valueOf(ttl.toMillis()) |
| | | ); |
| | |
| | | public boolean reserveInventory(String inventoryKey, String orderKey, BigDecimal initialAvailable, BigDecimal reserveQuantity, Duration ttl) { |
| | | Long result = redisTemplate.execute( |
| | | INVENTORY_RESERVE_SCRIPT, |
| | | Arrays.asList(inventoryKey, orderKey), |
| | | List.of(inventoryKey, orderKey), |
| | | initialAvailable.toPlainString(), |
| | | reserveQuantity.toPlainString(), |
| | | String.valueOf(ttl.toMillis()) |