首先是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 set (String key, value) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.set(key.getBytes(), Util.serialize(value));
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
jedis.close();
}
}
/**
* 得到对应键的对象
* @param key
* @return
* @author hw
* @date 2018年12月14日
*/
public get (String key) {
Jedis jedis = jedisPool.getResource();
try {
byte[] byteArr = jedis.get(key.getBytes());
return Util.unSerialize(byteArr);
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
jedis.close();
}
}
}
附上 Util
public class Util {
/**
* 判断对象是否为空
* @param o
* @return
* @author hw
* @date 2018年12月11日
*/
@SuppressWarnings(\"rawtypes\")
public static boolean isEmpty( 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( o) {
OutputStream oos = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
oos = new OutputStream(baos);
oos.write (o);
} catch (IOException e) {
e.printStackTrace();
throw new SysException(e.getMessage());
} finally {
close OutputStream(oos);
}
return baos.toByteArray();
}
/**
* 将对象反序列化
* @param byteArr
* @return
* @author hw
* @date 2018年12月14日
*/
public static unSerialize(byte[] byteArr) {
InputStream ois = null;
ByteArrayInputStream bais = new ByteArrayInputStream(byteArr);
o = null;
try {
ois = new InputStream(bais);
o = ois.read ();
} catch (Exception e) {
throw new SysException(e.getMessage());
} finally {
close IutputStream(ois);
}
return o;
}
/**
* 关闭对象输出流
* @param oos
* @author hw
* @date 2018年12月14日
*/
private static void close OutputStream( OutputStream 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 close IutputStream( InputStream ois) {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
throw new SysException(e.getMessage());
}
}
ois = null;
}
}
以上代码由网上收集整合而成,测试有效
继续阅读与本文标签相同的文章
上一篇 :
阿里云PAI正式商业化
-
小程序如何引流?企业推广其实很简单
2026-05-19栏目: 教程
-
2019云栖大会 | 云原生时代 带你聚焦新数据库的硬核科技
2026-05-19栏目: 教程
-
AI人工智能了不得,帮助研究人员设计自行车,速度打破世界纪录
2026-05-19栏目: 教程
-
RNG官宣入驻快手,UZI成首席电竞官,S9成快手布局游戏圈跳板
2026-05-19栏目: 教程
-
国家邮政局官宣万国邮联国际小包终端费改革 你想知道的都在这里
2026-05-19栏目: 教程
