redis实现分布式锁的方式就是使用多参数的set方法。。
同时设置 setnx 和 expire
具体工具类见下!
package com.duilu.user.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;
/**
* @Author: Yun
* @Description:
* @Date: Created in 2018-05-22 13:22
*/
@Service
public class RedisLockService {
private static final String LOCK_SUCCESS = "OK";
/**只在键不存在时,才对键进行设置操作
*
*/
private static final String SET_IF_NOT_EXIST = "NX";
/**
* 设置键的过期时间 单位秒
*/
private static final String SET_WITH_EXPIRE_TIME = "EX";
@Autowired
private Pool<Jedis> pool;
public Pool<Jedis> getPool() {
return pool;
}
/**
* redis分布式锁
* @param key
* @param value
* @param seconds
* @return
*/
public boolean lock(final String key, String value, long seconds) {
Jedis jedis = null;
try {
jedis = getPool().getResource();
String result=jedis.set(key,value,SET_IF_NOT_EXIST,SET_WITH_EXPIRE_TIME,seconds);
if(LOCK_SUCCESS.equals(result)){
return true;
}else{
return false;
}
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
使用方式:
if(redisLockService.lock(key,value,expireTime)){
//TODO 加锁成功 做一些操作
}