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();
}
}
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;
}
Aggregations