use of com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache in project flash-sale by ThoughtsBeta.
the class FlashItemsCacheService method tryToUpdateItemsCacheByLock.
public FlashItemsCache tryToUpdateItemsCacheByLock(Long activityId) {
logger.info("itemsCache|更新远程缓存|{}", activityId);
DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ITEMS_CACHE_LOCK_KEY + activityId);
try {
boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
if (!isLockSuccess) {
return new FlashItemsCache().tryLater();
}
PagesQueryCondition pagesQueryCondition = new PagesQueryCondition();
pagesQueryCondition.setActivityId(activityId);
pagesQueryCondition.setStatus(FlashItemStatus.ONLINE.getCode());
PageResult<FlashItem> flashItemPageResult = flashItemDomainService.getFlashItems(pagesQueryCondition);
FlashItemsCache flashItemsCache;
if (flashItemPageResult == null) {
flashItemsCache = new FlashItemsCache().notExist();
} else {
flashItemsCache = new FlashItemsCache().setTotal(flashItemPageResult.getTotal()).setFlashItems(flashItemPageResult.getData()).setVersion(System.currentTimeMillis());
}
distributedCacheService.put(buildItemCacheKey(activityId), JSON.toJSONString(flashItemsCache), FIVE_MINUTES);
logger.info("itemsCache|远程缓存已更新|{}", activityId);
return flashItemsCache;
} catch (Exception e) {
logger.error("itemsCache|远程缓存更新失败|{}", activityId);
return new FlashItemsCache().tryLater();
} finally {
lock.unlock();
}
}
use of com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache in project flash-sale by ThoughtsBeta.
the class FlashItemsCacheService method getLatestDistributedCache.
private FlashItemsCache getLatestDistributedCache(Long activityId) {
logger.info("itemsCache|读取远程缓存|{}", activityId);
FlashItemsCache distributedCachedFlashItem = distributedCacheService.getObject(buildItemCacheKey(activityId), FlashItemsCache.class);
if (distributedCachedFlashItem == null) {
distributedCachedFlashItem = tryToUpdateItemsCacheByLock(activityId);
}
if (distributedCachedFlashItem != null && !distributedCachedFlashItem.isLater()) {
boolean isLockSuccess = localCacleUpdatelock.tryLock();
if (isLockSuccess) {
try {
flashItemsLocalCache.put(activityId, distributedCachedFlashItem);
logger.info("itemsCache|本地缓存已更新|{}", activityId);
} finally {
localCacleUpdatelock.unlock();
}
}
}
return distributedCachedFlashItem;
}
use of com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache in project flash-sale by ThoughtsBeta.
the class DefaultFlashItemAppService method getFlashItems.
@Override
public AppMultiResult<FlashItemDTO> getFlashItems(Long userId, Long activityId, FlashItemsQuery flashItemsQuery) {
if (flashItemsQuery == null) {
return AppMultiResult.empty();
}
flashItemsQuery.setActivityId(activityId);
List<FlashItem> activities;
Integer total;
if (flashItemsQuery.isOnlineFirstPageQuery()) {
FlashItemsCache flashItemsCache = flashItemsCacheService.getCachedItems(activityId, flashItemsQuery.getVersion());
if (flashItemsCache.isLater()) {
return AppMultiResult.tryLater();
}
activities = flashItemsCache.getFlashItems();
total = flashItemsCache.getTotal();
} else {
PageResult<FlashItem> flashItemPageResult = flashItemDomainService.getFlashItems(toFlashItemsQuery(flashItemsQuery));
activities = flashItemPageResult.getData();
total = flashItemPageResult.getTotal();
}
List<FlashItemDTO> flashItemDTOList = activities.stream().map(FlashItemAppBuilder::toFlashItemDTO).collect(Collectors.toList());
return AppMultiResult.of(flashItemDTOList, total);
}
Aggregations