• 【Spring Boot && Spring Cloud系列】在spring-data-Redis中如何使用切换库


    前言

    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>
  • 相关阅读:
    kuangbin_ShortPath K (POJ 3159)
    kuangbin_ShortPath I (POJ 2240)
    kuangbin_ShortPath H (POJ 3660)
    kuangbin_ShortPath G (POJ 1502)
    kuangbin_ShortPath J (POJ 1511)
    kuangbin_ShortPath F (POJ 3259)
    kuangbin_ShortPath E (POJ 1860)
    StoryBoard中使用xib
    iOS APP 架构漫谈[转]
    Mac 快速修改 hosts 文件
  • 原文地址:https://www.cnblogs.com/dream-to-pku/p/7565170.html
Copyright © 2020-2023  润新知