redis watch java_redis watch命令实现秒杀demo

1.思路

在redis提供了incr命令进行递增操作,可以保证原子性。利用watch实现也可以实现递增递减的操作。

WATCH命令可以监控一个或多个键,一旦其中有一个键被修改(或删除),之后的事务就不会执行。监控一直持续到EXEC命令(事务中的命令是在EXEC之后才执行的,所以在MULTI命令后可以修改WATCH监控的键值)

2.代码实现

public class SecKillDemo implements Runnable {

private Jedis jedis = new Jedis("192.168.99.100", 6379);

private String customerName;

private String key;

public SecKillDemo(String customerName, String key) {

this.customerName = customerName;

this.key = key;

}

@Override

public void run() {

boolean success = false;

String data;

int currentNum;

while (!success) {//可重复抢购直到成功

//通过watch实现redis的incr(原子递增操作)

jedis.watch(key);

data = jedis.get(key);

currentNum = Integer.parseInt(data);

if (currentNum > 0) {

//开启事务

Transaction transaction = jedis.multi();

//设置新值,如果key的值被其它连接的客户端修改,那么当前连接的exec命令将执行失败

currentNum--;

transaction.set(key, String.valueOf(currentNum));

List res = transaction.exec();

if (res.size() == 0) {

System.out.println(customerName + " 抢购失败");

success = false;

} else {

success = true;

System.out.println(customerName + " 抢购成功,[" + key + "]剩余:" + currentNum);

}

} else {

System.out.println("商品售空,活动结束!");

System.exit(0);

}

}

}

}

3.测试

public class SecKillTest {

private static String key = "macbook";

private static String num = "100";

private static ExecutorService executorService = Executors.newFixedThreadPool(8);

@Before

public void before() {

Jedis jedis = new Jedis("192.168.99.100");

String script = "redis.call('del',KEYS[1]);return redis.call('set',KEYS[1],ARGV[1])";

jedis.eval(script, Collections.singletonList(key), Collections.singletonList(num));

jedis.close();

}

@Test

public void test() {

}

public static void main(String[] args) {

try{

for (int i = 1; i <= 600; i++) {

executorService.submit(new SecKillDemo("顾客"+i,key));

}

}finally {

executorService.shutdown();

}

}

}

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值