Search in sources :

Example 16 with DistributedLock

use of com.actionworks.flashsale.lock.DistributedLock 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 17 with DistributedLock

use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.

the class QueuedPlaceOrderTaskService method refreshLatestAvailableTokens.

private Integer refreshLatestAvailableTokens(Long itemId) {
    DistributedLock refreshTokenLock = lockFactoryService.getDistributedLock(getRefreshTokensLockKey(itemId));
    try {
        boolean isLockSuccess = refreshTokenLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            return null;
        }
        ItemStockCache itemStockCache = itemStockCacheService.getAvailableItemStock(null, itemId);
        if (itemStockCache != null && itemStockCache.isSuccess() && itemStockCache.getAvailableStock() != null) {
            Integer latestAvailableOrderTokens = (int) Math.ceil(itemStockCache.getAvailableStock() * 1.5);
            redisCacheService.put(getItemAvailableTokensKey(itemId), latestAvailableOrderTokens, HOURS_24);
            availableOrderTokensLocalCache.put(itemId, latestAvailableOrderTokens);
            return latestAvailableOrderTokens;
        }
    } catch (Exception e) {
        logger.error("refreshAvailableTokens|刷新tokens失败|{}", itemId, e);
    } finally {
        refreshTokenLock.unlock();
    }
    return null;
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) ItemStockCache(com.actionworks.flashsale.app.service.stock.model.ItemStockCache)

Aggregations

DistributedLock (com.actionworks.flashsale.lock.DistributedLock)17 BizException (com.actionworks.flashsale.app.exception.BizException)9 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)7 AuthException (com.actionworks.flashsale.controller.exception.AuthException)7 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)4 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)4 Bucket (com.actionworks.flashsale.domain.model.Bucket)2 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AppException (com.actionworks.flashsale.app.exception.AppException)1 StockBucketException (com.actionworks.flashsale.app.exception.StockBucketException)1 PlaceOrderResult (com.actionworks.flashsale.app.model.result.PlaceOrderResult)1 FlashActivitiesCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)1 FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)1 FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)1 FlashItemsCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)1 ItemStockCache (com.actionworks.flashsale.app.service.stock.model.ItemStockCache)1 TimeUnit (java.util.concurrent.TimeUnit)1 RLock (org.redisson.api.RLock)1 DefaultRedisScript (org.springframework.data.redis.core.script.DefaultRedisScript)1