redis(jedis)工具类实例:集群模式和单机模式
在写redis工具类之前,我们首先需要知道如何读取redis配置文件,此处提供properties配置文件的读取工具类,大家可以直接使用:https://blog.csdn.net/weixin_42315600/article/details/87170735
1、集群模式工具类
package com.wonddream.utils;
import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JedisClusterUtil {
private static final Log logger = LogFactory.getLog(JedisClusterUtil.class);
private static Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
private static JedisCluster jedisCluster = null;
/**
* 初始化jedisCluster对象
*/
static {
try {
jedisCluster = reloadJedisCluster();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
/**
* 集群模式
* 获取JedisCluster对象
* @return
* @throws Exception
*/
public static JedisCluster getJedisCluster() throws Exception {
if (jedisCluster == null) {
synchronized (JedisClusterUtil.class) {
jedisCluster = reloadJedisCluster();
}
}
return jedisCluster;
}
/**
* 集群模式
* 获取JedisCluster方法
* @return
* @throws Exception
*/
public static JedisCluster reloadJedisCluster() throws Exception {
logger.info("初始化实体");
JedisCluster cluster;
String redisAddrCfg =
PropertiesUtil.getString("redis.address","config/redisConfig");
logger.info("******redis集群配置:"+redisAddrCfg);
if (StringUtil.isEmpty(redisAddrCfg) || redisAddrCfg.split(",").length == 0) {
throw new Exception("System.properties中REDIS_ADDR_CFG属性为空");
}
String[] addrs = redisAddrCfg.split(",");
for (String addr : addrs) {
String[] ipAndPort = addr.split(":");
if (ipAndPort == null || ipAndPort.length != 2) {
throw new Exception("System.properties中REDIS_ADDR_CFG属性配置错误");
}
jedisClusterNodes.add(new HostAndPort(ipAndPort[0],
Integer.parseInt(ipAndPort[1])));
}
cluster = new JedisCluster(jedisClusterNodes, 2000, 6);
return cluster;
}
/*
* 测试
*/
public static void main(String[] args) throws Exception {
}
}
2、单机模式工具类
package com.wonddream.utils;
import redis.clients.jedis.Jedis;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JedisUtil {
private static final Log logger = LogFactory.getLog(JedisUtil.class);
private static Jedis jedis = null;
/**
* 初始化jedis对象
*/
static {
try {
jedis = reloadJedis();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
/**
* 单机模式
* 获取Jedis对象
* @return
* @throws Exception
*/
public static Jedis getJedis() throws Exception {
if (jedis == null) {
synchronized (JedisUtil.class) {
jedis = reloadJedis();
}
}
return jedis;
}
/**
* 单机模式
* 获取Jedis方法
* @return
* @throws Exception
*/
public static Jedis reloadJedis() throws Exception {
logger.info("初始化实体");
Jedis jedis;
String redisAddrCfg = PropertiesUtil.getString("redis.address","config/redisConfig");
logger.info("******redis单机配置:"+redisAddrCfg);
if (StringUtil.isEmpty(redisAddrCfg) || redisAddrCfg.split(",").length == 0) {
throw new Exception("System.properties中REDIS_ADDR_CFG属性为空");
}
String[] ipAndPort = redisAddrCfg.split(":");
if (ipAndPort == null || (ipAndPort.length != 2 && ipAndPort.length != 3)) {
throw new Exception("System.properties中REDIS_ADDR_CFG属性配置错误");
}
jedis = new Jedis(ipAndPort[0], Integer.parseInt(ipAndPort[1]), 2000, 30);
//如果使用这redis设置有密码,则需要再次设置授权。我的redis配置格式:ip:port:password
//使用者可以根据自己的情况自行调整
if(ipAndPort.length == 3) {
jedis.auth(ipAndPort[2]);
}
return jedis;
}
/*
* 测试
*/
public static void main(String[] args) throws Exception {
getJedis().setex("name", 2, "wangpenghui");
System.out.println(getJedis().get("name"));
Thread.sleep(3000);
System.out.println(getJedis().get("name"));
}
}