Search in sources :

Example 1 with FlashActivityCache

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

the class DefaultActivityAppService method getFlashActivity.

@Override
public AppSimpleResult<FlashActivityDTO> getFlashActivity(Long userId, Long activityId, Long version) {
    if (userId == null || activityId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    FlashActivityCache flashActivityCache = flashActivityCacheService.getCachedActivity(activityId, version);
    if (!flashActivityCache.isExist()) {
        throw new BizException(ACTIVITY_NOT_FOUND.getErrDesc());
    }
    if (flashActivityCache.isLater()) {
        return AppSimpleResult.tryLater();
    }
    FlashActivityDTO flashActivityDTO = FlashActivityAppBuilder.toFlashActivityDTO(flashActivityCache.getFlashActivity());
    flashActivityDTO.setVersion(flashActivityCache.getVersion());
    return AppSimpleResult.ok(flashActivityDTO);
}
Also used : FlashActivityDTO(com.actionworks.flashsale.app.model.dto.FlashActivityDTO) FlashActivityCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 2 with FlashActivityCache

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

the class DefaultActivityAppService method isAllowPlaceOrderOrNot.

@Override
public boolean isAllowPlaceOrderOrNot(Long activityId) {
    FlashActivityCache flashActivityCache = flashActivityCacheService.getCachedActivity(activityId, null);
    if (flashActivityCache.isLater()) {
        logger.info("isAllowPlaceOrderOrNot|稍后再试|{}", activityId);
        return false;
    }
    if (!flashActivityCache.isExist() || flashActivityCache.getFlashActivity() == null) {
        logger.info("isAllowPlaceOrderOrNot|活动不存在|{}", activityId);
        return false;
    }
    FlashActivity flashActivity = flashActivityCache.getFlashActivity();
    if (!flashActivity.isOnline()) {
        logger.info("isAllowPlaceOrderOrNot|活动尚未上线|{}", activityId);
        return false;
    }
    if (!flashActivity.isInProgress()) {
        logger.info("isAllowPlaceOrderOrNot|活动非秒杀时段|{}", activityId);
        return false;
    }
    // 可在此处丰富其他校验规则
    return true;
}
Also used : FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivityCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)

Example 3 with FlashActivityCache

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

the class FlashActivityCacheService method tryToUpdateActivityCacheByLock.

public FlashActivityCache tryToUpdateActivityCacheByLock(Long activityId) {
    logger.info("activityCache|更新远程缓存|{}", activityId);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ACTIVITY_CACHE_LOCK_KEY + activityId);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashActivityCache().tryLater();
        }
        FlashActivity flashActivity = flashActivityDomainService.getFlashActivity(activityId);
        FlashActivityCache flashActivityCache;
        if (flashActivity == null) {
            flashActivityCache = new FlashActivityCache().notExist();
        } else {
            flashActivityCache = new FlashActivityCache().with(flashActivity).withVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildActivityCacheKey(activityId), JSON.toJSONString(flashActivityCache), FIVE_MINUTES);
        logger.info("activityCache|远程缓存已更新|{}", activityId);
        return flashActivityCache;
    } catch (InterruptedException e) {
        logger.error("activityCache|远程缓存更新失败|{}", activityId);
        return new FlashActivityCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivityCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)

Example 4 with FlashActivityCache

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

the class FlashActivityCacheService method getLatestDistributedCache.

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

Aggregations

FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)4 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)2 BizException (com.actionworks.flashsale.app.exception.BizException)1 FlashActivityDTO (com.actionworks.flashsale.app.model.dto.FlashActivityDTO)1 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)1