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);
}
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();
}
}
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;
}
Aggregations