use of com.actionworks.flashsale.app.service.stock.model.ItemStockCache in project flash-sale by ThoughtsBeta.
the class BucketsCacheService method getAvailableItemStock.
@Override
public ItemStockCache getAvailableItemStock(Long userId, Long itemId) {
Integer subBucketsQuantity = getSubBucketsQuantity(itemId);
if (subBucketsQuantity == null) {
return null;
}
Integer targetBucketSerialNo = getTargetBucketSerialNo(userId, subBucketsQuantity);
String bucketCacheKey = getBucketAvailableStocksCacheKey(itemId, targetBucketSerialNo);
Integer availableBucketStocks = bucketAvailableStocksLocalCache.getIfPresent(bucketCacheKey);
if (availableBucketStocks == null) {
availableBucketStocks = distributedCacheService.getObject(getBucketAvailableStocksCacheKey(itemId, targetBucketSerialNo), Integer.class);
}
return new ItemStockCache().with(availableBucketStocks);
}
use of com.actionworks.flashsale.app.service.stock.model.ItemStockCache in project flash-sale by ThoughtsBeta.
the class NormalStockCacheService method getAvailableItemStock.
@Override
public ItemStockCache getAvailableItemStock(Long userId, Long itemId) {
ItemStockCache itemStockCache = itemStockLocalCache.getIfPresent(itemId);
if (itemStockCache != null) {
return itemStockCache;
}
Integer availableStock = distributedCacheService.getObject(getItemStocksCacheKey(itemId), Integer.class);
if (availableStock == null) {
return null;
}
itemStockCache = new ItemStockCache().with(availableStock);
itemStockLocalCache.put(itemId, itemStockCache);
return itemStockCache;
}
use of com.actionworks.flashsale.app.service.stock.model.ItemStockCache in project flash-sale by ThoughtsBeta.
the class QueuedPlaceOrderTaskService method refreshLatestAvailableTokens.
private Integer refreshLatestAvailableTokens(Long itemId) {
DistributedLock refreshTokenLock = lockFactoryService.getDistributedLock(getRefreshTokensLockKey(itemId));
try {
boolean isLockSuccess = refreshTokenLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
if (!isLockSuccess) {
return null;
}
ItemStockCache itemStockCache = itemStockCacheService.getAvailableItemStock(null, itemId);
if (itemStockCache != null && itemStockCache.isSuccess() && itemStockCache.getAvailableStock() != null) {
Integer latestAvailableOrderTokens = (int) Math.ceil(itemStockCache.getAvailableStock() * 1.5);
redisCacheService.put(getItemAvailableTokensKey(itemId), latestAvailableOrderTokens, HOURS_24);
availableOrderTokensLocalCache.put(itemId, latestAvailableOrderTokens);
return latestAvailableOrderTokens;
}
} catch (Exception e) {
logger.error("refreshAvailableTokens|刷新tokens失败|{}", itemId, e);
} finally {
refreshTokenLock.unlock();
}
return null;
}
Aggregations