首先是springboot配置文件application.properties中的redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=xxx
spring.redis.timeout=10
spring.redis.jedis.pool.max-active=200
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=10
spring.redis.jedis.pool.max-wait=10000
创建一个生成JedisPool的工厂
@Configuration
public class JedisPoolFactory {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Bean
public JedisPool generateJedisPoolFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
return jedisPool;
}
}
编写工具类,这里的@Component注解不能丢,否则jedisPool为空,
@Component
public class RedisUtil {
@Autowired
private JedisPool jedisPool;
/**
* 存储字符串键值对
* @param key
* @param value
* @return
* @author hw
* @date 2018年12月14日
*/
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.set(key, value);
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
jedis.close();
}
}
/**
* 得到对应键的字符串值
* @param key
* @return
* @author hw
* @date 2018年12月14日
*/
public String get(String key) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.get(key);
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
jedis.close();
}
}
/**
* 删除字符串键值对
* @param key
* @return
* @author hw
* @date 2018年12月14日
*/
public Long del(String key) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.del(key);
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
jedis.close();
}
}
/**
* 存储对象
* @param key
* @param value
* @return
* @author hw
* @date 2018年12月14日
*/
public String setObject(String key, Object value) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.set(key.getBytes(), ObjectUtil.serialize(value));
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
jedis.close();
}
}
/**
* 得到对应键的对象
* @param key
* @return
* @author hw
* @date 2018年12月14日
*/
public Object getObject(String key) {
Jedis jedis = jedisPool.getResource();
try {
byte[] byteArr = jedis.get(key.getBytes());
return ObjectUtil.unSerialize(byteArr);
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
jedis.close();
}
}
}
附上ObjectUtil
public class ObjectUtil {
/**
* 判断对象是否为空
* @param o
* @return
* @author hw
* @date 2018年12月11日
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object o) {
if (o == null) {
return true;
} else if (o instanceof String) {
String s = (String) o;
if ("".equals(s.trim())) {
return true;
} else {
return false;
}
} else if (o instanceof Collection) {
return ((Collection) o).isEmpty();
} else if (o instanceof Map) {
return ((Map) o).isEmpty();
} else if (o.getClass().isArray()) {
return Array.getLength(o) == 0;
} else {
return false;
}
}
/**
* 将对象序列化
* @param o
* @return
* @author hw
* @date 2018年12月14日
*/
public static byte[] serialize(Object o) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(o);
} catch (IOException e) {
e.printStackTrace();
throw new SysException(e.getMessage());
} finally {
closeObjectOutputStream(oos);
}
return baos.toByteArray();
}
/**
* 将对象反序列化
* @param byteArr
* @return
* @author hw
* @date 2018年12月14日
*/
public static Object unSerialize(byte[] byteArr) {
ObjectInputStream ois = null;
ByteArrayInputStream bais = new ByteArrayInputStream(byteArr);
Object o = null;
try {
ois = new ObjectInputStream(bais);
o = ois.readObject();
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
closeObjectIutputStream(ois);
}
return o;
}
/**
* 关闭对象输出流
* @param oos
* @author hw
* @date 2018年12月14日
*/
private static void closeObjectOutputStream(ObjectOutputStream oos) {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
throw new SysException(e.getMessage());
}
}
oos = null;
}
/**
* 关闭对象输入流
* @param ois
* @author hw
* @date 2018年12月14日
*/
private static void closeObjectIutputStream(ObjectInputStream ois) {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
throw new SysException(e.getMessage());
}
}
ois = null;
}
}
以上代码由网上收集整合而成,测试有效