一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring集成jedis的配置與使用簡單實例

Spring集成jedis的配置與使用簡單實例

2021-07-21 11:458blues Java教程

今天小編就為大家分享一篇關(guān)于Spring集成jedis的配置與使用簡單實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

jedis是redis的java客戶端,spring將redis連接池作為一個bean配置。

redis連接池分為兩種,一種是“redis.clients.jedis.shardedjedispool”,這是基于hash算法的一種分布式集群redis客戶端連接池。

另一種是“redis.clients.jedis.jedispool”,這是單機環(huán)境適用的redis連接池。

maven導(dǎo)入相關(guān)包:

?
1
2
3
4
5
6
<!-- redis依賴包 -->
<dependency>
 <groupid>redis.clients</groupid>
 <artifactid>jedis</artifactid>
 <version>2.9.0</version>
</dependency>

shardedjedispool是redis集群客戶端的對象池,可以通過他來操作shardedjedis,下面是shardedjedispool的xml配置,spring-jedis.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 引入jedis的properties配置文件 -->
  <!--如果你有多個數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個配置文件里,那么一定要加上ignore-unresolvable=“true"-->
  <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />
  <!--shardedjedispool的相關(guān)配置-->
  <bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">
    <!--新版是maxtotal,舊版是maxactive-->
    <property name="maxtotal">
      <value>${redis.pool.maxactive}</value>
    </property>
    <property name="maxidle">
      <value>${redis.pool.maxidle}</value>
    </property>
    <property name="testonborrow" value="true"/>
    <property name="testonreturn" value="true"/>
  </bean>
  <bean id="shardedjedispool" class="redis.clients.jedis.shardedjedispool" scope="singleton">
    <constructor-arg index="0" ref="jedispoolconfig" />
    <constructor-arg index="1">
      <list>
        <bean class="redis.clients.jedis.jedisshardinfo">
          <constructor-arg name="host" value="${redis.uri}" />
        </bean>
      </list>
    </constructor-arg>
  </bean>
</beans>

下面是單機環(huán)境下redis連接池的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 引入jedis的properties配置文件 -->
  <!--如果你有多個數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個配置文件里,那么一定要加上ignore-unresolvable=“true"-->
  <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />
  <!--jedis連接池的相關(guān)配置-->
  <bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">
    <!--新版是maxtotal,舊版是maxactive-->
    <property name="maxtotal">
      <value>${redis.pool.maxactive}</value>
    </property>
    <property name="maxidle">
      <value>${redis.pool.maxidle}</value>
    </property>
    <property name="testonborrow" value="true"/>
    <property name="testonreturn" value="true"/>
  </bean>
  <bean id="jedispool" class="redis.clients.jedis.jedispool">
    <constructor-arg name="poolconfig" ref="jedispoolconfig" />
    <constructor-arg name="host" value="${redis.host}" />
    <constructor-arg name="port" value="${redis.port}" type="int" />
    <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
    <constructor-arg name="password" value="${redis.password}" />
    <constructor-arg name="database" value="${redis.database}" type="int" />
  </bean>
</beans>

對應(yīng)的classpath:properties/redis.properties.xml為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#最大分配的對象數(shù)
redis.pool.maxactive=200
#最大能夠保持idel狀態(tài)的對象數(shù)
redis.pool.maxidle=50
redis.pool.minidle=10
redis.pool.maxwaitmillis=20000
#當池內(nèi)沒有返回對象時,最大等待時間
redis.pool.maxwait=300
#格式:redis://:[密碼]@[服務(wù)器地址]:[端口]/[db index]
redis.uri = redis://:[email protected]:6379/0
redis.host = 127.0.0.1
redis.port = 6379
redis.timeout=30000
redis.password = 12345
redis.database = 0

二者操作代碼類似,都是先注入連接池,然后通過連接池獲得jedis實例,通過實例對象操作redis。

shardedjedis操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@autowired
private shardedjedispool shardedjedispool;//注入shardedjedispool
@requestmapping(value = "/demo_set",method = requestmethod.get)
@responsebody
public string demo_set(){
  //獲取shardedjedis對象
  shardedjedis shardjedis = shardedjedispool.getresource();
  //存入鍵值對
  shardjedis.set("key1","hello jedis");
  //回收shardedjedis實例
  shardjedis.close();
  return "set";
}
@requestmapping(value = "/demo_get",method = requestmethod.get)
@responsebody
public string demo_get(){
  shardedjedis shardedjedis = shardedjedispool.getresource();
  //根據(jù)鍵值獲得數(shù)據(jù)
  string result = shardedjedis.get("key1");
  shardedjedis.close();
  return result;
}

jedis操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@autowired
private jedispool jedispool;//注入jedispool
@requestmapping(value = "/demo_set",method = requestmethod.get)
@responsebody
public string demo_set(){
  //獲取shardedjedis對象
  jedis jedis = jedispool.getresource();
  //存入鍵值對
  jedis.set("key2","hello jedis one");
  //回收shardedjedis實例
  jedis.close();
  return "set";
}
@requestmapping(value = "/demo_get",method = requestmethod.get)
@responsebody
public string demo_get(){
  jedis jedis = jedispool.getresource();
  //根據(jù)鍵值獲得數(shù)據(jù)
  string result = jedis.get("key2");
  jedis.close();
  return result;
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

原文鏈接:https://blog.csdn.net/u014320421/article/details/80576232

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 羞羞私人影院可以直接免费观影吗 | ffee性xxⅹ另类老妇hd | 国产 日韩 一区 | 欧美艳星julnaann | 四虎在线最新永久免费 | 美女被吸乳老师羞羞漫画 | 国产福利不卡视频在免费 | 日本xxx在线观看免费播放 | 国产成人久久久精品一区二区三区 | 日韩视频第二页 | 国产成+人+综合+欧美 亚洲 | 欧美一区二区三区精品 | 欧美精品一区二区三区免费 | 青青青青青国产费线在线观看 | 娇小老少配xxxxx性视频 | 啪哆哆| 国产福利在线观看第二区 | 日本哺乳期网站xxxx | 亚洲黄色免费在线观看 | 国产九九在线观看播放 | 国产欧美日韩不卡 | 四虎最新免费观看网址 | 日日摸日日碰夜夜爽97纠 | 2018亚洲男人天堂 | 国产无限制自拍 | 肥胖女性大bbbbbb视频女厕 | 亚洲天堂影视 | 免费一级片在线观看 | 国产日韩欧美精品在线 | 91精品手机国产在线观 | 免费看美女被靠到爽的视频 | 国产日产精品久久久久快鸭 | 国产高清经典露脸3p | 99热资源| 国内久久精品视频 | 91久久碰国产 | 四虎精品视频在线永久免费观看 | 欧美特一级 | 亚洲欧美日韩久久一区 | 粗暴hd另类另类 | 欧美综合另类 |