对redis要求严格的推荐使用redission分布式锁
1.redis 分布式锁 工具类demo
public class RedisLockUtil {
private String key;
private Object value;
private RedisTemplate<String,Object> redisTemplate;
private Integer time;
private TimeUnit timeUnit;
public RedisLockUtil(){}
public RedisLockUtil(RedisTemplate<String, Object> redisTemplate,String key) {
this.redisTemplate = redisTemplate;
this.key = key;
this.value="1";
}
public RedisLockUtil(RedisTemplate<String, Object> redisTemplate,String key, Integer time,TimeUnit timeUnit) {
if(timeUnit==null){timeUnit=TimeUnit.SECONDS;}
if(time==null || time<=0){time=1;}
this.time = time;
this.timeUnit=timeUnit;
this.redisTemplate = redisTemplate;
this.key = key;
this.value = getTimeByPattern(time,timeUnit);
}
public boolean seckill_lock(){
try {
Boolean isLock = redisTemplate.opsForValue().setIfAbsent(key, value);
if (isLock) {
return true;
}
Object obj = redisTemplate.opsForValue().get(key);
String currentValue = obj==null?null:String.valueOf(obj);
if (currentValue!=null && currentValue.length()>0
&& Long.valueOf(currentValue) < getTimeByPattern(time,timeUnit)) {
String oldValue = String.valueOf(redisTemplate.opsForValue().getAndSet(key, value));
if (oldValue!=null && oldValue.length()>0
&& oldValue.equals(currentValue)) {
System.out.println("锁过期并返回true");
return true;
}
}
}catch (Exception e){
e.printStackTrace();
System.out.println("redis分布式锁,加锁异常");
}
return false;
}
public void seckill_unlock(){
try {
Object obj = redisTemplate.opsForValue().get(key);
String currentValue = obj==null?null:String.valueOf(obj);
if (currentValue!=null && currentValue.length()>0
&& currentValue.equals(value)) {
redisTemplate.opsForValue().getOperations().delete(key);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("redis分布式锁,解锁异常");
}
}
public long getTimeByPattern(Integer time,TimeUnit timeUnit){
long millis = System.currentTimeMillis();
return millis/timeUnit.toMillis(time);
}
public boolean lock(){
try {
if(time!=null && time>0){
return redisTemplate.opsForValue().setIfAbsent(key, value,time,timeUnit);
}else{
return redisTemplate.opsForValue().setIfAbsent(key, value);
}
}catch (Exception e){
e.printStackTrace();
System.out.println("redis 锁,加锁异常");
}
return false;
}
public void unlock(){
try {
redisTemplate.opsForValue().getOperations().delete(key);
} catch (Exception e) {
e.printStackTrace();
System.out.println("redis 锁,解锁异常");
}
}
}
2.redis 多线程测试接口-分布式锁
@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
private volatile int num=100;
@RequestMapping("/thread")
public String redisThread(@RequestParam(required = false) Integer num2){
if(num2!=null && num2>0) {
this.num = num2;
}
for(int i=0;i<10;i++) {
new Thread(() -> {
while(num>0){
RedisLockUtil lockUtil = new RedisLockUtil(redisTemplate,"lock_redis",1,TimeUnit.SECONDS);
boolean lock = lockUtil.lock();
if(lock) {
if(num<=0){
break;
}
System.out.println("print out num=" + num);
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
num--;
lockUtil.unlock();
}else{
}
}
}).start();
}
return "print success";
}
}
3.测试例子
http://127.0.0.1:1234/redis/thread?num2=100