springmvc整合redis

redis作为web项目一个较好解决缓存问题的方案被大量运用,redis具体的概念在此不做详细介绍,本文介绍项目整合redis并且做一系列的操作

1:maven项目引入依赖

<!-- redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.0</version>
        </dependency>

       <dependency>  
            <groupId>org.springframework.data</groupId>  
            <artifactId>spring-data-redis</artifactId>  
            <version>1.5.0.RELEASE</version>  
        </dependency>

2:新建一个spring-redis.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd    
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
   
      <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  p:host-name="172.16.10.247" p:port="6379" p:password="" p:pool-config-ref="poolConfig"/>  
     
      <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
        <property name="connectionFactory"   ref="connectionFactory" />  
        <property name="keySerializer">
            <bean
                 class="org.springframework.data.redis.serializer.StringRedisSerializer" />
         </property>
        <property name="valueSerializer">
             <bean
                 class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
         </property>
      </bean>   
    
</beans>


3:建一个测试类

@Autowired RedisTemplate redisTemplate;//自动注入

      // String读写
            redisTemplate.delete("myStr");
            redisTemplate.opsForValue().set("myStr", "abc");
            System.out.println(redisTemplate.opsForValue().get("myStr"));
            System.out.println("---------------");
            
         // List读写
            redisTemplate.delete("myList");
            redisTemplate.opsForList().rightPush("myList", "A");
            redisTemplate.opsForList().rightPush("myList", "B");
            redisTemplate.opsForList().leftPush("myList", "0");
            List<String> listCache = redisTemplate.opsForList().range("myList", 0, -1);
            for (String s : listCache) {
                  System.out.println(s);
            }
            System.out.println("---------------");
            
            // Set读写
            redisTemplate.delete("mySet");
            redisTemplate.opsForSet().add("mySet", "A");
            redisTemplate.opsForSet().add("mySet", "B");
            redisTemplate.opsForSet().add("mySet", "C");
            Set<String> setCache = redisTemplate.opsForSet().members("mySet");
            for (String s : setCache) {
                System.out.println(s);
            }
            System.out.println("---------------");
            
            // Hash读写
            redisTemplate.delete("myHash");
            redisTemplate.opsForHash().put("myHash", "PEK", "北京");
            redisTemplate.opsForHash().put("myHash", "SHA", "上海虹桥");
            redisTemplate.opsForHash().put("myHash", "PVG", "浦东");
            Map<Object, Object> hashCache = redisTemplate.opsForHash().entries("myHash");
            for (Map.Entry<Object, Object> entry : hashCache.entrySet()) {
                System.out.println(entry.getKey() + " - " + entry.getValue());
            }
            System.out.println("---------------");
            
            User user = new User();
            user.setId(3);
            user.setAddress("11111");
            user.setEmail("zsdfsd111111fsdf@163.com");
            user.setName("发11发11发");
            user.setSex(1);
            System.out.println("==== putting objects into redis ====");  
            redisTemplate.opsForValue().set( user.getId()+"", user);
            System.out.println("==== getting objects from redis ====");  
            System.out.println( redisTemplate.opsForValue().get(""+user.getId()));

至此,一个完整而简单的整合完毕,可以取出redis里面的缓存。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值