Search in sources :

Example 1 with FlashItemsCache

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();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) FlashItemsCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition)

Example 2 with FlashItemsCache

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

Example 3 with FlashItemsCache

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);
}
Also used : FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO) FlashItemsCache(com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)

Aggregations

FlashItemsCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)3 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)2 FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)1 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)1 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)1