您的位置:首页 > 数据库 > Redis

springmvc整合redis

2016-06-20 16:03 471 查看
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里面的缓存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: