import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtil {
private static String ADDR = "10.256456.26";
private static Integer PORT = 6379;
private static String AUTH ;
private static Integer MAX_TOTAL = 1024;
private static Integer MAX_IDLE = 200;
private static Integer MAX_WAIT_MILLIS = 10000;
private static Integer TIMEOUT = 10000;
private static Boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_TOTAL);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT_MILLIS);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config,ADDR,PORT,TIMEOUT,AUTH);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized static Jedis getJedis(){
try {
if(jedisPool != null){
Jedis jedis = jedisPool.getResource();
return jedis;
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void returnResource(final Jedis jedis){
if(jedis!=null){
jedisPool.returnResource(jedis);
}
}
}
@RequestMapping(value = "/Test")
public RestResultDTO Test(String days) {
try {
RedisUtil.getJedis().set("name","liming");
System.out.println(RedisUtil.getJedis().get("name"));
return RestResultDTO.success(RedisUtil.getJedis().get("name"));
} catch (Exception e) {
return RestResultDTO.error(ResponseCodeDTO.INTERNAL_SERVER_ERROR, e.getMessage());
}
}