问题场景,秒杀活动,或者其他情况下,用户多次点击下单,导致发生了订单重复提交的问题
根据这问题,结合Redis的
这个特性,我们就有了一个解决用户重复提交订单的问题,案例代码如下
package com.example.demo2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class Demo2ApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
while (true) {
Long increment = redisTemplate.boundHashOps("A").increment("B", 1);
if (increment > 1) {
System.out.println("出现重复:"+increment);
if (increment>100){
System.exit(0);
}
}else {
System.out.println("第一次排队:"+increment);
}
}
}
}
关于具体怎么搭建整合Redis的SpringBoot,就在这里不细说了,
下一期,将弄一个整合Redis分布式锁的Demo出来