SpringMvc 4.3 整合redis

这篇博客记录了SpringMvc 4.3整合Redis的过程,包括在pom.xml中添加Redis依赖,调整redis.properties中的timeout以避免连接超时问题,以及在springMvc-base.xml中的配置变化。博主通过测试发现,20个线程存储300万个hash时,超过600万数据插入会报超时,因此将timeout设置为100秒,并且测试了不同线程数量下的性能。尽管在Windows环境下CPU性能限制了效率提升,但在Linux下表现更佳。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇仅限自己备忘,因框架配置不尽相同,其他人仅供参考!

主要修改以下文件:
pom.xml
redis.properties
springMvc-base.xml


pom.xml

添加redis依赖

 <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.1.0</version>
    </dependency>

redis.properties

# Redis settings
redis.host=192.168.5.99
redis.pass=
redis.port      =6379    
redis.maxIdle       =300  
redis.default.db    =0  
redis.timeout       =100000  
redis.maxActive     =600  
redis.maxWait       =1000  
redis.testOnBorrow  =true  

timeout 默认是6000 即6秒,我改成了100秒,因为我写了个测试,20个线程,每个线程负责存储30万条hash,结果每次到200多万就会报与redis链接超时,所以我就改大了。实际测试600万数据插入耗时4分半,后来无论怎么增加线程数量,都不会再有效率提升,在linux测试更快一些。我猜想是受限于windows 下cpu的性能。

springMvc-base.xml

因为需要即支持物理数据库,又要支持内存数据库,所以需要变更以下配置

 <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:Jdbc/jdbc.properties" />  
    </bean> 

变更为:

 <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!--<property name="location" value="classpath:Jdbc/jdbc.properties" />-->
        <property name="locations">
            <list>
                <value>classpath:Jdbc/jdbc.properties</value>
                <value>classpath:redis/redis.properties</value>
            </list>
        </property>
    </bean>

增加如下配置:

<!-- redis缓存数据库的相关配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxActive" value="${redis.maxActive}" />
        <property name="maxWait" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redis.host}"
          p:port="${redis.port}"
          p:password="${redis.pass}"
          p:pool-config-ref="poolConfig"
          p:timeout="${redis.timeout}"
    />

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>
    <bean id="LoginRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>

测试:

controller

    /**
     * 保存用户
     * @param -name
     * @param- sex
     * @param -age
     * @return
     */

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(@RequestParam(value="userName", defaultValue="") String userName,
                        @RequestParam(value="password", defaultValue="") String password){
        System.err.println("name: ----------"+userName );
        service.saveUser(userName, password);
        return 1;
    }

service (我的框架里面没有dao,懒的写。项目小,没啥用。技术就这么烂,唉没办法)

@Resource
    protected RedisTemplate<String, Object> redisTemplate;
    @Override
    public void saveUser(String userName, String pwd) {
        for (int i = 0; i <20 ; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    Long a =System.currentTimeMillis();
                    System.out.println("start===="+(a)/1000);
                    for (int i = 0; i <300000 ; i++) {
                        String uuid = UUID.randomUUID().toString().replace("-","");
                        redisTemplate.opsForValue().set(uuid,"liushuai");
                    }
                    System.out.println("end===="+(System.currentTimeMillis()-a)/1000);
                }
            }).start();
        }


        Map<String,String> maps = new HashMap<String, String>();
        maps.put("multi1","multi1");
        maps.put("multi2","multi2");
        maps.put("multi3","multi3");
        redisTemplate.opsForValue().multiSet(maps);
        List<String> keys = new ArrayList<String>();
        keys.add("multi1");
        keys.add("multi2");
        keys.add("multi3");
        System.out.println(redisTemplate.opsForValue().multiGet(keys));

    }

以上就是集成过程了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值