詳解Spring Boot Redis集成配置
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();
}
}
/**
相關文章
-
Spring Boot基於註解的Redis緩存使用
數學是人們生活、勞動和學習必不可少的工具,來看看八年級數學教學計劃吧!下面是本站小編收集整理的八年級數學下冊教學計劃,歡迎閱讀。八年級數學下冊教學計劃篇一本學期教學總目標知識與技能1.瞭解分式的基本性質,掌握 -
Spring Boot使用slf4j+logback記錄日誌配置詳解
捨不得在牆上鑽洞,但又喜歡層板的收納用途,那你一定不要錯過下面這個不會用到一根釘子的層板架DIY,除了解決收納問題外,還可以讓舊物得到再利用哦!找來舊木梯,如果有裝修房子,一般木工都會做這麼一個梯子出來。舊舊、毛糙都 -
WebSphere MQ Solution Designer認證考試
IBM根據其產品分類設置了相應的專業認證項目,那麼IBM認證是什麼呢?IBM認證的優勢又怎樣的呢?下面是小編整理的關於WebSphere MQ Solution Designer認證考試,希望大家認真閱讀! 打開隊列本部分討論打開不同隊列類型的 -
Time To Stop Worrying About Invasive Species美文欣賞
Time To Stop Worrying About Invasive Species?You shouldn’t judge a book by its cover. And you shouldn’t judge a species by its place of origin. So say ecologists in a commentary in the joual Nature. -
Is It Good to Being A Postgraduate大學英語作文
Nowadays, many college students choose further study when they graduate, the rich students study abroad, while some continue their study in China. Actually, the latter are most favored. Being a postgr -
Unit six Opening and Closing Ceremonies口譯材料
Unit six Opening and Closing CeremoniesLesson 1女士們,先生們,上午好!我很榮幸地代表戴比爾斯公司歡迎你們來到美麗的大連參加“中國鑽石節”。//藉此機會我想介紹一下出席大會的代表們(介紹代表們的名字、職位和簡要 -
Should Internet Slang Be Prohibited英語作文
目前,一些諸如GG, MM, Xia Mi 等網絡語言在青少年中極爲盛行,並且出現在家庭作業報告,甚至全國入學考試的'作文中。請你以 “Should Inteet Slang Be Prohibited (禁止)?”爲題,根據下表內容用英語寫一篇短文,並談談你自 -
英語閱讀:VIT declines to acquire Ang Mo Kio property
The managers of Viva Industrial Trust (VIT) announced on Oct 9 that they have declined an offer to purchase an Ang Mo Kio property at $300 million from Ho Lee Group.Located along Ang Mo Kio Avenue 5, -
OFFICE2007每次打開word時都顯示配置microsoft office professio
Office 2007每次打開都要出現配置Microsoft Office Professional Plus 2007雙擊excel文件出現如下提示:然後提示:點擊確定出現:而上一步若點擊取消,則提示: 問題解決: 方法一:首先用360和諾頓都查了一遍,無木馬或病毒。參 -
Spare No Efforts in Doing Things英語作文
簡約風。主要材料:仿珍珠髮梳所需工具:金屬首飾線製作步驟:第1步:第一部。第2步:第二部。第3步:第三部。第4步:第四部。第5步:第五部。第6步:第六部。第7步:第七部。第8步:第八部。第9步:第九第10步:第十第11步:十一第12步:十二第13