spring Boot 熟悉後,集成一個外部擴展是一件很容易的事,集成Redis也很簡單,以下是本站小編搜索整理的關於詳解Spring Boot Redis集成配置,供參考學習,希望對大家有所幫助!想了解更多相關信息請持續關注我們應屆畢業生考試網!
一、添加pom依賴
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
二、創建
注意該類存放的package
package s;
import ArrayInputStream;
import ArrayOutputStream;
import ception;
import ctInputStream;
import ctOutputStream;
import pportedEncodingException;
import ngUtils;
import er;
import erFactory;
import s;
import ocol;
import sException;
/**
* 工具類 RedisClient
* 因爲本類中獲取JedisPool調用的是JedisConnectionFactory中protected修飾的方法fetchJedisConnector()
* 所以該類需要與JedisConnectionFactory在同一個package中
*
* @author 單紅宇(CSDN CATOOP)
* @create 2017年4月9日
*/
public class RedisClient {
private static Logger logger = ogger(s);
private JedisConnectionFactory factory;
public RedisClient(JedisConnectionFactory factory) {
super();
ory = factory;
}
/**
* put操作(存儲序列化對象)+ 生效時間
*
* @param key
* @param value
* @return
*/
public void putObject(final String key, final Object value, final int cacheSeconds) {
if (tBlank(key)) {
redisTemplete(key, new RedisExecute<Object>() {
@Override
public Object doInvoker(Jedis jedis) {
try {
x(ytes(SET), cacheSeconds, serialize(value));
} catch (UnsupportedEncodingException e) {
}
return null;
}
});
}
}
/**
* get操作(獲取序列化對象)
*
* @param key
* @return
*/
public Object getObject(final String key) {
return redisTemplete(key, new RedisExecute<Object>() {
@Override
public Object doInvoker(Jedis jedis) {
try {
byte[] byteKey = ytes(SET);
byte[] byteValue = (byteKey);
if (byteValue != null) {
return deserialize(byteValue);
}
} catch (UnsupportedEncodingException e) {
return null;
}
return null;
}
});
}
/**
* setex操作
*
* @param key
* 鍵
* @param value
* 值
* @param cacheSeconds
* 超時時間,0爲不超時
* @return
*/
public String set(final String key, final String value, final int cacheSeconds) {
return redisTemplete(key, new RedisExecute<String>() {
@Override
public String doInvoker(Jedis jedis) {
if (cacheSeconds == 0) {
return (key, value);
}
return x(key, cacheSeconds, value);
}
});
}
/**
* get操作
*
* @param key
* 鍵
* @return 值
*/
public String get(final String key) {
return redisTemplete(key, new RedisExecute<String>() {
@Override
public String doInvoker(Jedis jedis) {
String value = (key);
return tBlank(value) && !"nil"lsIgnoreCase(value) ? value : null;
}
});
}
/**
* del操作
*
* @param key
* 鍵
* @return
*/
public long del(final String key) {
return redisTemplete(key, new RedisExecute<Long>() {
@Override
public Long doInvoker(Jedis jedis) {
return (key);
}
});
}
/**
* 獲取資源
*
* @return
* @throws JedisException
*/
public Jedis getResource() throws JedisException {
Jedis jedis = null;
try {
jedis = hJedisConnector();
} catch (JedisException e) {
r("getResource.", e);
returnBrokenResource(jedis);
throw e;
}
return jedis;
}
/**
* 獲取資源
*
* @return
* @throws JedisException
*/
public Jedis getJedis() throws JedisException {
return getResource();
}
/**
* 歸還資源
*
* @param jedis
* @param isBroken
*/
public void returnBrokenResource(Jedis jedis) {
if (jedis != null) {
e();
}
}
/**
* 釋放資源
*
* @param jedis
* @param isBroken
*/
public void returnResource(Jedis jedis) {
if (jedis != null) {
e();
}
}
/**