如有错误希望指出,谢谢
一、redis 池创建
public class RedisPool {
private static JedisPool pool;//jedis连接池
private static int maxTotal = 20;//最大连接数
private static int maxIdle = 10;//最大空闲连接数
private static int minIdle = 5;//最小空闲连接数
private static boolean testOnBorrow = true;//在取连接时测试连接的可用性
private static boolean testOnReturn = false;//再还连接时不测试连接的可用性
static {
initPool();//初始化连接池
}
public static Jedis getJedis(){
return pool.getResource();
}
public static void close(Jedis jedis){
jedis.close();
}
private static void initPool(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
config.setBlockWhenExhausted(true);
pool = new JedisPool(config, "127.0.0.1", 6379, 5000, "liqiyao");
}
}
二、 redis 方法
public class JedisService {
@Autowired
private JedisPool jedisPool;
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* redis set 方法时添加时间 和格式
* 参数用法 nxxx 对应 NX XX
* NX : not exists, 只有key 不存在时才把key value set 到redis
* XX : is exists ,只有 key 存在是,才把key value set 到redis
* expx 对应 EX PX EX是秒,PX是毫秒
*/
public String set(final String key, final String value, final String nxxx, final String expx,
final long time) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.set(key, value,nxxx,expx,time);
} catch (JedisConnectionException e) {
jedis.disconnect();
throw e;
} finally {
if(jedis!=null){
jedis.close();
}
}
}
/**
* 获取自增长ID
* @param redisKey 这个是保存在缓存中的key,所以最好复杂点
* @return
*/
public long generateId(String redisKey) {
RedisAtomicLong counter = new RedisAtomicLong(GENERATE_KEY + redisKey,redisTemplate.getConnectionFactory());
return counter.incrementAndGet();
}
public void set(String redisKey,int value,long timeout,TimeUnit unit) {
RedisAtomicLong counter = new RedisAtomicLong(GENERATE_KEY + redisKey, redisTemplate.getConnectionFactory());
counter.set(value);
counter.expire(timeout, unit);
}
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.set(key, value);
} catch (JedisConnectionException e) {
jedis.disconnect();
throw e;
} finally {
if(jedis!=null){
jedis.close();
}
}
}
public Long setnx(String key, String value) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.setnx(key, value);
} catch (JedisConnectionException e) {
jedis.disconnect();
throw e;
} finally {
if(jedis!=null){
jedis.close();
}
}
}
public String setex(String key, int seconds, String value) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.setex(key, seconds, value);
} catch (JedisConnectionException e) {
jedis.disconnect();
throw e;
} finally {
if(jedis!=null){
jedis.close();
}
}
}
/**
* 成功 1 失败 0
*/
public Long move(String key, int dbIndex) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.move(key, dbIndex);
} catch (JedisConnectionException e) {
jedis.disconnect();
throw e;
} finally {
if(jedis!=null){
jedis.close();
}
}
}
}
三、通过setnx实现分布式锁
https://blog.csdn.net/qq_35406874/article/details/95187102
四、redis 之Redis之Pipeline