Search in sources :

Example 1 with ItemStockCache

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);
}
Also used : ItemStockCache(com.actionworks.flashsale.app.service.stock.model.ItemStockCache)

Example 2 with ItemStockCache

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;
}
Also used : ItemStockCache(com.actionworks.flashsale.app.service.stock.model.ItemStockCache)

Example 3 with 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;
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) ItemStockCache(com.actionworks.flashsale.app.service.stock.model.ItemStockCache)

Aggregations

ItemStockCache (com.actionworks.flashsale.app.service.stock.model.ItemStockCache)3 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)1