use of com.goodskill.entity.Seckill in project goodsKill by techa03.
the class SeckillServiceImpl method exportSeckillUrl.
@Override
public ExposerDTO exportSeckillUrl(long seckillId) {
// 从redis中获取缓存秒杀信息
Seckill seckill = redisService.getSeckill(seckillId);
Date startTime = seckill.getStartTime();
Date endTime = seckill.getEndTime();
Date nowTime = new Date();
if (nowTime.getTime() < startTime.getTime() || nowTime.getTime() > endTime.getTime()) {
return new ExposerDTO(false, seckillId, nowTime.getTime(), startTime.getTime(), endTime.getTime());
}
String md5 = MD5Util.getMD5(seckillId);
return new ExposerDTO(true, md5, seckillId);
}
use of com.goodskill.entity.Seckill in project goodsKill by techa03.
the class SeckillServiceImpl method prepareSeckill.
@Override
public void prepareSeckill(Long seckillId, int seckillCount) {
// 初始化库存数量
Seckill entity = new Seckill();
entity.setSeckillId(seckillId);
entity.setNumber(seckillCount);
entity.setStatus(SeckillStatusConstant.IN_PROGRESS);
this.updateById(entity);
// 清理已成功秒杀记录
this.deleteSuccessKillRecord(seckillId);
redisService.removeSeckill(seckillId);
Seckill seckill = redisService.getSeckill(seckillId);
redisTemplate.delete(seckillId);
seckill.setStatus(SeckillStatusConstant.IN_PROGRESS);
redisService.putSeckill(seckill);
// 清理mongo表数据
try {
successKilledMongoService.deleteRecord(seckillId);
} catch (Exception e) {
log.error("mongo服务不可用请检查!", e);
throw e;
}
}
use of com.goodskill.entity.Seckill in project goodsKill by techa03.
the class SeckillProcedureExecutor method dealSeckill.
/**
* 处理用户秒杀请求
*
* @param seckillId 秒杀活动id
* @param userPhone
* @param note 秒杀备注信息
*/
@Override
public void dealSeckill(long seckillId, String userPhone, String note) {
try {
InetAddress localHost = InetAddress.getLocalHost();
SuccessKilled successKilled = new SuccessKilled();
successKilled.setSeckillId(seckillId);
successKilled.setUserPhone(userPhone);
successKilled.setCreateTime(new Date());
successKilled.setServerIp(localHost.getHostAddress() + ":" + localHost.getHostName());
if (seckillService.reduceNumber(successKilled) < 1) {
Seckill seckill = seckillMapper.selectById(seckillId);
log.debug("当前库存:{}", seckill.getNumber());
// 高并发时限制只能发一次秒杀完成通知
if (!SeckillStatusConstant.END.equals(seckill.getStatus()) && sendTopicTimes.getAndIncrement() == 0) {
streamBridge.send(DEFAULT_BINDING_NAME, MessageBuilder.withPayload(SeckillMockResponseDTO.builder().seckillId(seckillId).note(note).status(true).build()).build());
Seckill sendTopicResult = new Seckill();
sendTopicResult.setSeckillId(seckillId);
sendTopicResult.setStatus(SeckillStatusConstant.END);
seckillMapper.updateById(sendTopicResult);
// 重置发送成功通知次数
sendTopicTimes.set(0);
}
if (seckill.getNumber() <= 0) {
log.debug("库存不足,无法继续秒杀!");
}
}
} catch (UnknownHostException e) {
log.error(e.getMessage(), e);
}
}
use of com.goodskill.entity.Seckill in project goodsKill by techa03.
the class SeckillServiceImpl method getSeckillList.
@Override
public Page<Seckill> getSeckillList(int pageNum, int pageSize, String goodsName) {
String key = "seckill:list:" + pageNum + ":" + pageSize + ":" + goodsName;
ValueOperations valueOperations = redisTemplate.opsForValue();
Page pageCache = (Page) valueOperations.get(key);
Page<Seckill> page;
if (pageCache == null) {
QueryWrapper<Seckill> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotBlank(goodsName)) {
queryWrapper.lambda().like(Seckill::getName, goodsName);
}
page = this.page(new Page(pageNum, pageSize), queryWrapper);
valueOperations.set(key, page, 5, TimeUnit.MINUTES);
} else {
page = pageCache;
}
return page;
}
use of com.goodskill.entity.Seckill in project goodsKill by techa03.
the class RedisMongoReactiveStrategy method execute.
@Override
public void execute(SeckillMockRequestDTO requestDto) {
long seckillId = requestDto.getSeckillId();
Seckill seckill = redisService.getSeckill(seckillId);
if (redisTemplate.opsForValue().increment(seckillId) <= seckill.getNumber()) {
taskExecutor.execute(() -> streamBridge.send(DEFAULT_BINDING_NAME_MONGO_SAVE, MessageBuilder.withPayload(SeckillMockSaveVo.builder().seckillId(seckillId).userPhone(requestDto.getPhoneNumber()).note(REDIS_MONGO_REACTIVE.getName()).build()).build()));
log.debug("已发送");
} else {
synchronized (this) {
seckill = redisService.getSeckill(seckillId);
if (!SeckillStatusConstant.END.equals(seckill.getStatus())) {
log.info("秒杀商品暂无库存,发送活动结束消息!");
streamBridge.send(DEFAULT_BINDING_NAME, MessageBuilder.withPayload(SeckillMockResponseDTO.builder().status(true).seckillId(seckillId).note(REDIS_MONGO_REACTIVE.getName()).build()).build());
Seckill sendTopicResult = new Seckill();
sendTopicResult.setSeckillId(seckillId);
sendTopicResult.setStatus(SeckillStatusConstant.END);
extSeckillMapper.updateById(sendTopicResult);
seckill.setStatus(SeckillStatusConstant.END);
redisService.putSeckill(seckill);
}
}
}
}
Aggregations