/**
* 秒杀
*/
public boolean secKillBubble(Integer bubbleId, Integer userId) {
Jedis jedis = getJedis();
Long incr = jedis.incr(RedisConstant.bubbleEntryNum + bubbleId);
try {
jedis.watch(RedisConstant.bubbleForOne + bubbleId);// watchkeys
if (incr <=1) {//1可以改成任何数量
Transaction tx = jedis.multi();// 开启事务
tx.setex(RedisConstant.bubbleForOne + incr, RedisConstant.bubble_exp, userId.toString());//设置值及过期时间, 原子性
List<Object> list = tx.exec();// 提交事务,如果此时watchkeys被改动了,则返回null
if (list != null) {
log.info("气泡秒杀成功!" + ",userId" + userId + ",气泡Id:" + bubbleId);
return true;
}
}
log.info("气泡没有秒杀到" + ",userId" + userId + ",气泡Id:" + bubbleId);
return false;
} catch (Exception e) {
log.error("错误:秒杀气泡" + ",userId" + userId + ",气泡Id:" + bubbleId, e);
e.printStackTrace();
} finally {
jedis.close();
}
return false;
}