国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Spring集成jedis的配置與使用簡單實(shí)例

2024-07-14 08:43:46
字體:
供稿:網(wǎng)友

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

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

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

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

  <!-- redis依賴包 -->  <dependency>   <groupId>redis.clients</groupId>   <artifactId>jedis</artifactId>   <version>2.9.0</version>  </dependency>

ShardedJedisPool是redis集群客戶端的對(duì)象池,可以通過他來操作ShardedJedis,下面是ShardedJedisPool的xml配置,spring-jedis.xml

<?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配置文件 -->  <!--如果你有多個(gè)數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個(gè)配置文件里,那么一定要加上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>

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

<?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配置文件 -->  <!--如果你有多個(gè)數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個(gè)配置文件里,那么一定要加上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>

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

#最大分配的對(duì)象數(shù)redis.pool.maxActive=200#最大能夠保持idel狀態(tài)的對(duì)象數(shù)redis.pool.maxIdle=50redis.pool.minIdle=10redis.pool.maxWaitMillis=20000#當(dāng)池內(nèi)沒有返回對(duì)象時(shí),最大等待時(shí)間redis.pool.maxWait=300#格式:redis://:[密碼]@[服務(wù)器地址]:[端口]/[db index]redis.uri = redis://:12345@127.0.0.1:6379/0redis.host = 127.0.0.1redis.port = 6379redis.timeout=30000redis.password = 12345redis.database = 0

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

ShardedJedis操作:

  @Autowired  private ShardedJedisPool shardedJedisPool;//注入ShardedJedisPool  @RequestMapping(value = "/demo_set",method = RequestMethod.GET)  @ResponseBody  public String demo_set(){    //獲取ShardedJedis對(duì)象    ShardedJedis shardJedis = shardedJedisPool.getResource();    //存入鍵值對(duì)    shardJedis.set("key1","hello jedis");    //回收ShardedJedis實(shí)例    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操作:

  @Autowired  private JedisPool jedisPool;//注入JedisPool  @RequestMapping(value = "/demo_set",method = RequestMethod.GET)  @ResponseBody  public String demo_set(){    //獲取ShardedJedis對(duì)象    Jedis jedis = jedisPool.getResource();    //存入鍵值對(duì)    jedis.set("key2","hello jedis one");    //回收ShardedJedis實(shí)例    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)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 富阳市| 类乌齐县| 左贡县| 乳源| 陕西省| 改则县| 长海县| 盱眙县| 始兴县| 临武县| 东乌珠穆沁旗| 祁门县| 定州市| 通榆县| 梁河县| 山丹县| 乌恰县| 石嘴山市| 太谷县| 白朗县| 镇康县| 平昌县| 景德镇市| 凌海市| 东阿县| 兴安盟| 临澧县| 淮北市| 旌德县| 汉阴县| 汉川市| 清原| 永新县| 广水市| 綦江县| 防城港市| 凤庆县| 巢湖市| 威远县| 安远县| 江孜县|