redis工具类
package Source;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class RedisUtils{
private static final Logger log = LoggerFactory.getLogger(RedisUtils.class);
//Redis服务器地址
private static final String redisHost = LoadProperties.getProperty("redis.host");
//Redis服务端口
private static final Integer redisPort = Integer.parseInt(LoadProperties.getProperty("redis.port"));
//Redis密码
private static final String redisPassword = LoadProperties.getProperty("redis.password");
//围栏信息数据库index
private static final int fenceIndex = Integer.parseInt(LoadProperties.getProperty("redis.fenceIndex"));
//设备信息数据库index
private static final int vinMsgIndex = Integer.parseInt(LoadProperties.getProperty("redis.vinMsgIndex"));
//Redis可连接实例最大数目
private static final Integer redisMaxTotal = Integer.parseInt(LoadProperties.getProperty("redis.maxTotal"));
//Redis最多有多少个状态为idle(空闲)的jedis实例,默认值是8
private static final Integer redisMaxIdle = Integer.parseInt(LoadProperties.getProperty("redis.maxIdle"));
//Reids等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。
private static final Integer redisMaxWaitNillis = Integer.parseInt(LoadProperties.getProperty("redis.maxWaitNillis"));
//Redis设置超时时间
private static final Integer redisTimeOut = Integer.parseInt(LoadProperties.getProperty("redis.timeOut"));
//在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作;
private static final Boolean redisTestOnBorrow = true;
// private static Jedis fenceJedisPool = null;
// private static Jedis vinMsgJedisPool = null;
private static JedisPool jedisPool = null;
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(redisMaxTotal);
config.setMaxIdle(redisMaxIdle);
config.setMaxWaitMillis(redisMaxWaitNillis);
config.setTestOnBorrow(redisTestOnBorrow);
jedisPool = new JedisPool(config, redisHost, redisPort, redisTimeOut);
} catch (Exception e) {
log.warn("Redis初始化失败{}", e.getMessage());
}
}
/**
* JSON数据,转成Object对象
*/
public static <T> T fromJson(String json, Class<T> clazz){
try{
return JSON.parseObject(json, clazz);
}catch (Exception e){
log.warn("redis value fromJson error. msg: {}, value: {}, class: {}", e.getMessage(), json, clazz);
}
return null;
}
/**
* Object转成JSON数据
*/
public static String toJson(Object object){
try {
if (object instanceof Integer || object instanceof Long || object instanceof Float ||
object instanceof Double || object instanceof Boolean || object instanceof String) {
return String.valueOf(object);
}
return JSON.toJSONString(object);
}catch (Exception e){
log.warn("value toJson error. msg: {}, object: {}", e.getMessage(), object);
}
return null;
}
/**
* redis get
*/
public static <T> T get(Integer index, String key, Class<T> clazz){
try{
if(jedisPool!=null) {
Jedis jedis = jedisPool.getResource();
jedis.select(index);
String value = jedis.get(key);
return value == null ? null : fromJson(value, clazz);
}
}catch (Exception e){
log.warn(" redis get operation error. msg: {}, key: {}, class: {}",e.getMessage(), key, clazz);
}
return null;
}
/**
* redis hget
*/
public static <T> T hget(Integer index, String name, String key, Class<T> clazz){
try{
if(jedisPool!=null) {
Jedis jedis = jedisPool.getResource();
jedis.select(index);
String value = jedis.hget(name, key);
return value == null ? null : fromJson(value, clazz);
}
}catch (Exception e){
log.warn(" redis hget operation error. msg: {}, key: {}, class: {}",e.getMessage(), key, clazz);
}
return null;
}
/**
* redis get keys
*/
public static List<String> getKeys(Integer index){
try{
if(jedisPool!=null) {
Jedis jedis = jedisPool.getResource();
jedis.select(index);
return new ArrayList<String>(jedis.keys("*"));
}
}catch (Exception e){
log.warn(" redis getKeys operation error. msg: {}",e.getMessage());
}
return null;
}
/**
* redis get keys by prefix
*/
public static List<String> getPrefixKeys(Integer index, String prefix){
try{
if(jedisPool!=null) {
Jedis jedis = jedisPool.getResource();
jedis.select(index);
return new ArrayList<String>(jedis.keys(prefix + "*"));
}
}catch (Exception e){
log.warn(" redis getPrefixKeys operation error. msg: {} prefix: {}",e.getMessage(), prefix);
}
return null;
}
public static Boolean exists(Integer index, String key){
try{
if(jedisPool!=null){
Jedis jedis = jedisPool.getResource();
jedis.select(index);
return jedis.exists(key);
}
}catch (Exception e){
log.warn(" redis exists operation error. msg: {} index: {} key: {}", e.getMessage(), index, key);
}
return false;
}
/**
* close redispool
*/
public static void close(){
if(jedisPool!=null){
jedisPool.close();
}
}
}
redis 哨兵模式连接
private static JedisSentinelPool jedisPool = null;
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(redisMaxTotal);
config.setMaxIdle(redisMaxIdle);
config.setMaxWaitMillis(redisMaxWaitNillis);
config.setTestOnBorrow(redisTestOnBorrow);
//哨兵信息
Set<String> sentinels = new HashSet<String>(Arrays.asList(redisHost.split(",")));
//jedisPool = new JedisPool(config, redisHost, redisPort, redisTimeOut);
//哨兵模式连接 第一个参数是配置给哨兵的服务名称 第二个是哨兵信息 第三个是连接配置 最后是密码
jedisPool = new JedisSentinelPool("sentine", sentinels, config,redisPassword);
} catch (Exception e) {
log.warn("Redis初始化失败{}", e.getMessage());
}
}