Search in sources :

Example 1 with FlashItemCache

use of com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method getFlashItem.

@Override
public AppSimpleResult<FlashItemDTO> getFlashItem(Long itemId) {
    FlashItemCache flashItemCache = flashItemCacheService.getCachedItem(itemId, null);
    if (!flashItemCache.isExist()) {
        throw new BizException(ACTIVITY_NOT_FOUND.getErrDesc());
    }
    if (flashItemCache.isLater()) {
        return AppSimpleResult.tryLater();
    }
    updateLatestItemStock(null, flashItemCache.getFlashItem());
    FlashItemDTO flashItemDTO = FlashItemAppBuilder.toFlashItemDTO(flashItemCache.getFlashItem());
    flashItemDTO.setVersion(flashItemCache.getVersion());
    return AppSimpleResult.ok(flashItemDTO);
}
Also used : FlashItemCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO)

Example 2 with FlashItemCache

use of com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache in project flash-sale by ThoughtsBeta.

the class FlashItemCacheService method tryToUpdateItemCacheByLock.

public FlashItemCache tryToUpdateItemCacheByLock(Long itemId) {
    logger.info("itemCache|更新远程缓存|{}", itemId);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ITEM_CACHE_LOCK_KEY + itemId);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashItemCache().tryLater();
        }
        FlashItem flashItem = flashItemDomainService.getFlashItem(itemId);
        FlashItemCache flashItemCache;
        if (flashItem == null) {
            flashItemCache = new FlashItemCache().notExist();
        } else {
            flashItemCache = new FlashItemCache().with(flashItem).withVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildItemCacheKey(itemId), JSON.toJSONString(flashItemCache), FIVE_MINUTES);
        logger.info("itemCache|远程缓存已更新|{}", itemId);
        return flashItemCache;
    } catch (InterruptedException e) {
        logger.error("itemCache|远程缓存更新失败|{}", itemId);
        return new FlashItemCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashItemCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem)

Example 3 with FlashItemCache

use of com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache in project flash-sale by ThoughtsBeta.

the class FlashItemCacheService method getLatestDistributedCache.

private FlashItemCache getLatestDistributedCache(Long itemId) {
    logger.info("itemCache|读取远程缓存|{}", itemId);
    FlashItemCache distributedFlashItemCache = distributedCacheService.getObject(buildItemCacheKey(itemId), FlashItemCache.class);
    if (distributedFlashItemCache == null) {
        distributedFlashItemCache = tryToUpdateItemCacheByLock(itemId);
    }
    if (distributedFlashItemCache != null && !distributedFlashItemCache.isLater()) {
        boolean isLockSuccess = localCacleUpdatelock.tryLock();
        if (isLockSuccess) {
            try {
                flashItemLocalCache.put(itemId, distributedFlashItemCache);
                logger.info("itemCache|本地缓存已更新|{}", itemId);
            } finally {
                localCacleUpdatelock.unlock();
            }
        }
    }
    return distributedFlashItemCache;
}
Also used : FlashItemCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)

Aggregations

FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)3 BizException (com.actionworks.flashsale.app.exception.BizException)1 FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)1 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)1 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)1