Search in sources :

Example 1 with SeckillCache

use of com.whoiszxl.cqrs.cache.SeckillCache in project shopzz by whoiszxl.

the class QueuedPlaceOrderServiceImpl method checkSeckill.

private boolean checkSeckill(Long seckillId) {
    // 1. 从缓存中拿到活动信息
    SeckillCache seckillCache = seckillCachedService.getCachedSeckill(seckillId, null);
    // 2. 校验缓存是否需要延迟加载
    if (seckillCache.isLater()) {
        log.info("从缓存中获取秒杀活动信息需要延迟加载, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 3. 校验活动是否存在
    if (!seckillCache.isExist() || seckillCache.getSeckill() == null) {
        log.info("活动不存在, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 4. 校验活动是否上线
    Seckill seckill = seckillCache.getSeckill();
    if (!seckill.getStatus().equals(StatusEnum.OPEN.getCode())) {
        log.info("活动未开启, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 5. 校验活动是否在进行中
    LocalDateTime now = LocalDateTime.now();
    if (!seckill.getStartTime().isBefore(now) || !seckill.getEndTime().isAfter(now)) {
        log.info("活动未在开启时间内, 秒杀活动ID: {}", seckillId);
        return false;
    }
    return true;
}
Also used : LocalDateTime(java.time.LocalDateTime) SeckillCache(com.whoiszxl.cqrs.cache.SeckillCache) Seckill(com.whoiszxl.entity.Seckill)

Example 2 with SeckillCache

use of com.whoiszxl.cqrs.cache.SeckillCache in project shopzz by whoiszxl.

the class DefaultPlaceOrderServiceImpl method checkSeckill.

private boolean checkSeckill(Long seckillId) {
    // 1. 从缓存中拿到活动信息
    SeckillCache seckillCache = seckillCachedService.getCachedSeckill(seckillId, null);
    // 2. 校验缓存是否需要延迟加载
    if (!seckillCache.isLater()) {
        log.info("从缓存中获取秒杀活动信息需要延迟加载, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 3. 校验活动是否存在
    if (!seckillCache.isExist() || seckillCache.getSeckill() == null) {
        log.info("活动不存在, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 4. 校验活动是否上线
    Seckill seckill = seckillCache.getSeckill();
    if (!seckill.getStatus().equals(StatusEnum.OPEN.getCode())) {
        log.info("活动未开启, 秒杀活动ID: {}", seckillId);
        return false;
    }
    // 5. 校验活动是否在进行中
    LocalDateTime now = LocalDateTime.now();
    if (!seckill.getStartTime().isBefore(now) || !seckill.getEndTime().isAfter(now)) {
        log.info("活动未在开启时间内, 秒杀活动ID: {}", seckillId);
        return false;
    }
    return true;
}
Also used : LocalDateTime(java.time.LocalDateTime) SeckillCache(com.whoiszxl.cqrs.cache.SeckillCache) Seckill(com.whoiszxl.entity.Seckill)

Example 3 with SeckillCache

use of com.whoiszxl.cqrs.cache.SeckillCache in project shopzz by whoiszxl.

the class SeckillCachedServiceImpl method loadDb.

private SeckillCache loadDb(Long seckillId) {
    DistributedLock lock = distributedLockFactory.getDistributedLock(RedisKeyPrefixConstants.LOCK_GET_SECKILL_FROM_DB + seckillId);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new SeckillCache().tryLater();
        }
        Seckill seckill = seckillService.getById(seckillId);
        SeckillCache seckillCache;
        if (seckill == null) {
            seckillCache = new SeckillCache().notExist();
        } else {
            seckillCache = new SeckillCache().with(seckill);
        }
        redisUtils.setEx(RedisKeyPrefixConstants.CACHE_SECKILL + seckillId, JsonUtil.toJson(seckillCache), 3, TimeUnit.MINUTES);
        log.info("从数据库加载数据,并保存到redis中:{}", seckillCache);
        return seckillCache;
    } catch (Exception e) {
        log.error("从数据库加载数据失败", e);
        return new SeckillCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.whoiszxl.DistributedLock) SeckillCache(com.whoiszxl.cqrs.cache.SeckillCache) Seckill(com.whoiszxl.entity.Seckill)

Example 4 with SeckillCache

use of com.whoiszxl.cqrs.cache.SeckillCache in project shopzz by whoiszxl.

the class SeckillCachedServiceImpl method getDistributedCache.

/**
 * 从分布式缓存中获取秒杀活动
 * @param seckillId 秒杀活动ID
 * @return
 */
private SeckillCache getDistributedCache(Long seckillId) {
    String seckillKey = RedisKeyPrefixConstants.CACHE_SECKILL + seckillId;
    String seckillJson = redisUtils.get(seckillKey);
    SeckillCache seckillCache = JsonUtil.fromJson(seckillJson, SeckillCache.class);
    if (seckillCache == null) {
        // 缓存不存在,需要从数据库中读取,并更新缓存
        seckillCache = loadDb(seckillId);
    }
    if (seckillCache != null && !seckillCache.isLater()) {
        boolean isLockSuccess = localCacleUpdatelock.tryLock();
        if (isLockSuccess) {
            try {
                seckillGuavaCache.put(seckillId, seckillCache);
                log.info("更新了本地秒杀活动缓存:{}", seckillCache);
            } finally {
                localCacleUpdatelock.unlock();
            }
        }
    }
    return seckillCache;
}
Also used : SeckillCache(com.whoiszxl.cqrs.cache.SeckillCache)

Aggregations

SeckillCache (com.whoiszxl.cqrs.cache.SeckillCache)4 Seckill (com.whoiszxl.entity.Seckill)3 LocalDateTime (java.time.LocalDateTime)2 DistributedLock (com.whoiszxl.DistributedLock)1