前言
Redis默認有16個庫,默認連接的是index=0的那一個。這16個庫直接是相互獨立的。
一、在命令行中切換
select 1;
二、在Spring中如何切換
1、在RedisConnectionCommands中使用redisConnection.select(1);
2、在配置文件中設置(JedisConnectionFactory)
<!-- jedis的連接工廠 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"/> <property name="port" value="${redis.port}"/> <property name="database" value="${redis.database}"/> <property name="password" value="${redis.pass}"/> <property name="timeout" value="2000"/> <property name="poolConfig" ref="poolConfig"/> </bean>
/**
* Sets the index of the database used by this connection factory. Default is 0.
*
* @param index database index
*/
public void setDatabase(int index) {
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
this.dbIndex = index;
}
spring-redis.xml完整配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:util="http://www.springframework.org/schema/util" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/util 9 http://www.springframework.org/schema/util/spring-util.xsd 10 "> 11 12 <!--jedis的連接池配置--> 13 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 14 <!-- 最大空閑連接數量 --> 15 <property name="maxIdle" value="${redis.maxIdle}"/> 16 <!-- 最小空閑連接數量, 處理間隔時間為 timeBetweenEvictionRunsMillis --> 17 <property name="minIdle" value="${redis.minIdle}"/> 18 <!-- 池中持有的最大連接數量 --> 19 <property name="maxTotal" value="${redis.maxTotal}"/> 20 <!-- borrowObject 方法的最大等待時間 --> 21 <property name="maxWaitMillis" value="${redis.maxWait}"/> 22 <!-- 池中可用資源耗盡時, borrow 方法是否阻塞等待 maxWaitMillis 毫秒 --> 23 <property name="blockWhenExhausted" value="true"/> 24 <!-- borrowObject 時是否執行檢測 --> 25 <property name="testOnBorrow" value="${redis.testOnBorrow}"/> 26 <!-- 是否檢測空閑連接鏈接的有效性, 間隔時間為 timeBetweenEvictionRunsMillis --> 27 <property name="testWhileIdle" value="true"/> 28 <!-- 空閑對象被清除需要達到的最小空閑時間 --> 29 <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/> 30 <!-- 空閑檢測線程,sleep 間隔多長時間,去處理與idle相關的事情 --> 31 <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/> 32 </bean> 33 <!-- jedis的連接工廠 --> 34 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 35 <property name="hostName" value="${redis.host}"/> 36 <property name="port" value="${redis.port}"/> 37 <property name="database" value="${redis.database}"/> 38 <property name="password" value="${redis.pass}"/> 39 <property name="timeout" value="2000"/> 40 <property name="poolConfig" ref="poolConfig"/> 41 </bean> 42 <!--redis實際使用的template--> 43 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 44 <property name="connectionFactory" ref="connectionFactory"/> 45 <property name="keySerializer"> 46 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> 47 </property> 48 <property name="valueSerializer"> 49 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 50 </property> 51 </bean> 52 <!--spring session . 禁用 To disable the automatic configuration --> 53 <util:constant 54 static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/> 55 <!--spring session 的redis配置--> 56 <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> 57 <property name="maxInactiveIntervalInSeconds" value="3600"/> 58 </bean> 59 60 </beans>
