Search in sources :

Example 1 with FlashActivitiesCache

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

the class DefaultActivityAppService method getFlashActivities.

@Override
public AppMultiResult<FlashActivityDTO> getFlashActivities(Long userId, FlashActivitiesQuery flashActivitiesQuery) {
    List<FlashActivity> activities;
    Integer total;
    if (flashActivitiesQuery.isFirstPureQuery()) {
        FlashActivitiesCache flashActivitiesCache = flashActivitiesCacheService.getCachedActivities(flashActivitiesQuery.getPageNumber(), flashActivitiesQuery.getVersion());
        if (flashActivitiesCache.isLater()) {
            return AppMultiResult.tryLater();
        }
        activities = flashActivitiesCache.getFlashActivities();
        total = flashActivitiesCache.getTotal();
    } else {
        PageResult<FlashActivity> flashActivityPageResult = flashActivityDomainService.getFlashActivities(toFlashActivitiesQuery(flashActivitiesQuery));
        activities = flashActivityPageResult.getData();
        total = flashActivityPageResult.getTotal();
    }
    List<FlashActivityDTO> flashActivityDTOList = activities.stream().map(FlashActivityAppBuilder::toFlashActivityDTO).collect(Collectors.toList());
    return AppMultiResult.of(flashActivityDTOList, total);
}
Also used : FlashActivityDTO(com.actionworks.flashsale.app.model.dto.FlashActivityDTO) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivitiesCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)

Example 2 with FlashActivitiesCache

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

the class FlashActivitiesCacheService method tryToUpdateActivitiesCacheByLock.

public FlashActivitiesCache tryToUpdateActivitiesCacheByLock(Integer pageNumber) {
    logger.info("activitiesCache|更新远程缓存|{}", pageNumber);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ACTIVITIES_CACHE_LOCK_KEY);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashActivitiesCache().tryLater();
        }
        PagesQueryCondition pagesQueryCondition = new PagesQueryCondition();
        PageResult<FlashActivity> flashActivityPageResult = flashActivityDomainService.getFlashActivities(pagesQueryCondition);
        FlashActivitiesCache flashActivitiesCache;
        if (flashActivityPageResult == null) {
            flashActivitiesCache = new FlashActivitiesCache().notExist();
        } else {
            flashActivitiesCache = new FlashActivitiesCache().setTotal(flashActivityPageResult.getTotal()).setFlashActivities(flashActivityPageResult.getData()).setVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildActivityCacheKey(pageNumber), JSON.toJSONString(flashActivitiesCache), FIVE_MINUTES);
        logger.info("activitiesCache|远程缓存已更新|{}", pageNumber);
        return flashActivitiesCache;
    } catch (InterruptedException e) {
        logger.error("activitiesCache|远程缓存更新失败", e);
        return new FlashActivitiesCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivitiesCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition)

Example 3 with FlashActivitiesCache

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

the class FlashActivitiesCacheService method getLatestDistributedCache.

private FlashActivitiesCache getLatestDistributedCache(Integer pageNumber) {
    logger.info("activitiesCache|读取远程缓存|{}", pageNumber);
    FlashActivitiesCache distributedFlashActivityCache = distributedCacheService.getObject(buildActivityCacheKey(pageNumber), FlashActivitiesCache.class);
    if (distributedFlashActivityCache == null) {
        distributedFlashActivityCache = tryToUpdateActivitiesCacheByLock(pageNumber);
    }
    if (distributedFlashActivityCache != null && !distributedFlashActivityCache.isLater()) {
        boolean isLockSuccess = localCacleUpdatelock.tryLock();
        if (isLockSuccess) {
            try {
                flashActivitiesLocalCache.put(pageNumber, distributedFlashActivityCache);
                logger.info("activitiesCache|本地缓存已更新|{}", pageNumber);
            } finally {
                localCacleUpdatelock.unlock();
            }
        }
    }
    return distributedFlashActivityCache;
}
Also used : FlashActivitiesCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)

Aggregations

FlashActivitiesCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)3 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)2 FlashActivityDTO (com.actionworks.flashsale.app.model.dto.FlashActivityDTO)1 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)1 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)1