redis與spring整合使用的步驟實(shí)例教程
前言
做過大型軟件系統(tǒng)的同學(xué)都知道,隨著系統(tǒng)數(shù)據(jù)越來越龐大,越來越復(fù)雜,隨之帶來的問題就是系統(tǒng)性能越來越差,尤其是頻繁操作數(shù)據(jù)庫帶來的性能損耗更為嚴(yán)重。很多業(yè)績(jī)大牛為此提出了眾多的解決方案和開發(fā)了很多框架以優(yōu)化這種頻繁操作數(shù)據(jù)庫所帶來的性能損耗,其中,尤為突出的兩個(gè)緩存服務(wù)器是Memcached和Redis。今天,我們不講Memcached和Redis本身,這里主要為大家介紹Spring與Redis整合使用的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細(xì)的介紹吧。
方法如下
第一步,在項(xiàng)目中加入redis的pom代碼:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.0</version> </dependency>
第二步,spring中加載redis配置文件:applicationContext-redis.xml,內(nèi)容如下
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>
<bean class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="poolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.node1.host}" />
<constructor-arg index="1" value="${redis.node1.port}" />
</bean>
</list>
</constructor-arg>
</bean>
</beans>
第三步,編寫連接redis服務(wù)端的屬性文件:redis.properties
redis.maxTotal=100 redis.node1.host=127.0.0.1 redis.node1.port=6379
第四步,編寫redis的相關(guān)操作方法類,F(xiàn)unction類和RedisService類:
Funcrion類:
package xx.service;
/**
* 為了抽取相同的操作代碼
* @author yeying
*<p>Description:</p>
*<p>Company:</p>
* @date:2017年12月5日 下午9:02:44
*/
public interface Function<T,E> {
public T callback(E e);
}
RedisService類:
package com.taotao.common.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* redis的相關(guān)操作
* @author yeying
*<p>Description:</p>
*<p>Company:</p>
* @date:2017年12月3日 下午2:11:47
*/
@Service
public class RedisService {
@Autowired(required=false) //需要再注入進(jìn)去
private ShardedJedisPool shardedJedisPool;
private <T> T execute(Function<T, ShardedJedis> fun){
ShardedJedis shardedJedis = null;
try {
// 從連接池中獲取到j(luò)edis分片對(duì)象
shardedJedis = shardedJedisPool.getResource();
// 從redis中獲取數(shù)據(jù)
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 關(guān)閉,檢測(cè)連接是否有效,有效則放回到連接池中,無效則重置狀態(tài)
shardedJedis.close();
}
}
return null;
}
/**
* 執(zhí)行set操作
* @param key
* @param value
* @return
*/
public String set(final String key,final String value){
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.set(key, value);
}
});
}
/**
* 執(zhí)行set操作,并設(shè)置生存時(shí)間,單位為秒
* @param key
* @param value
* @param seconds
* @return
*/
public String set(final String key,final String value,final Integer seconds){
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
String str =e.set(key, value);
e.expire(key, seconds);
return str;
}
});
}
/**
* 執(zhí)行g(shù)et操作
* @param key
* @return
*/
public String get(final String key){
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**
* 執(zhí)行set操作
* @param key
* @return
*/
public Long del(final String key){
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.del(key);
}
});
}
/**
* 設(shè)置生存時(shí)間,單位為秒
* @param key
* @param seconds
* @return
*/
public Long expire(final String key, final Integer seconds) {
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.expire(key, seconds);
}
});
}
}
第五步,啟動(dòng)redis服務(wù),redis-server.exe,雙擊打開:

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
scala 隱式轉(zhuǎn)換與隱式參數(shù)的使用方法
這篇文章主要介紹了scala 隱式轉(zhuǎn)換與隱式參數(shù)的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
有關(guān)Java常見的誤解小結(jié)(來看一看)
下面小編就為大家?guī)硪黄嘘P(guān)Java常見的誤解小結(jié)(來看一看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)
這篇文章主要介紹了詳解Springboot整合ActiveMQ(Queue和Topic兩種模式),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
springboot中RestTemplate配置HttpClient連接池詳解
這篇文章主要介紹了springboot中RestTemplate配置HttpClient連接池詳解,這些Http連接工具,使用起來都比較復(fù)雜,如果項(xiàng)目中使用的是Spring框架,可以使用Spring自帶的RestTemplate來進(jìn)行Http連接請(qǐng)求,需要的朋友可以參考下2023-11-11
java數(shù)據(jù)結(jié)構(gòu)之二分查找法 binarySearch的實(shí)例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)之二分查找法 binarySearch的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10

