SpringDataRedis入门

一.介绍

     Spring-data-redisspring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis,  JRedis, and RJC)进行了高度封装,

       RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

   spring-data-redis针对jedis提供了如下功能:
   1.连接池自动管理,提供了一个高度封装的“RedisTemplate”
   2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
     ValueOperations:简单K-V操作
     SetOperationsset类型数据操作
     ZSetOperationszset类型数据操作
     HashOperations:针对map类型的数据操作
     ListOperations:针对list类型的数据操作

二.入门小Demo

  1).建一个maven工程 导入依赖

    1.导入Jedis和Spring-Data-Redis依赖,及spring相关依赖    测试依赖junit

    2.配置文件

     redis-config.properties

    

# Redis settings 
# server IP 
redis.host=127.0.0.1
# server port 
redis.port=6379
# server pass 
redis.pass=
# use dbIndex 
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B 
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B  
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684  
redis.testOnBorrow=true   //提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;

 

   applicationContext-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:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
   <context:property-placeholder location="classpath*:properties/*.properties" />   
  
   <!-- redis 相关配置 --> 
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
     <property name="maxIdle" value="${redis.maxIdle}" />   
     <property name="maxWaitMillis" value="${redis.maxWait}" />  
     <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
   </bean>  
  
   <bean id="JedisConnectionFactory" 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"/>  
   
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
        <property name="connectionFactory" ref="JedisConnectionFactory" />
       <property name="enableTransactionSupport" value="true"/><!--开启事务-->
   </bean>  
      
</beans>  

   3.测试

    

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class RedisHashTest { //hash类型测试类
    @Autowired
    private RedisTemplate redisTemplate; //spring依赖注入

    @Test
    public void setValue(){
        redisTemplate.boundHashOps("myHash").put(1,"zhangsan" );
        redisTemplate.boundHashOps("myHash").put(2,"zhangsan1" );
        redisTemplate.boundHashOps("myHash").put(3,"zhangsan2" );
    }
    @Test
    public void getValue(){
        String myHash = (String) redisTemplate.boundHashOps("myHash").get(3);
        System.out.println(myHash);
        Set myHash1 = redisTemplate.boundHashOps("myHash").keys();
        System.out.println(myHash1);
        List myHash2 = redisTemplate.boundHashOps("myHash").values();
        System.out.println(myHash2);

    }
    @Test
    public void deleteValue(){
        redisTemplate.boundHashOps("myHash").delete(2);
    }

    @Test
    public void deleteAll(){
        redisTemplate.delete("myHash");
    }

    @Test
    public void getMap(){
        Map myHash = redisTemplate.boundHashOps("myHash").entries();
        System.out.println(myHash);
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class RedisListTest {  //list类型测试类
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        redisTemplate.boundListOps("myList").rightPush("a");
        redisTemplate.boundListOps("myList").rightPush("b");
        redisTemplate.boundListOps("myList").rightPush("c");
    }

    @Test
    public void getValue(){
        List myList = redisTemplate.boundListOps("myList").range(0, -1);
        System.out.println(myList);//有顺序
    }

    @Test
    public void getValue2(){
        String myList = (String) redisTemplate.boundListOps("myList").index(0);
        System.out.println(myList);
    }

    @Test
    public void deleteValue(){
        redisTemplate.boundListOps("myList").remove(1,"b" );
    }
    @Test
    public void deleteAllValue(){
        redisTemplate.delete("myList");
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class RedisSetTest { //set类型测试类
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        redisTemplate.boundSetOps("myset").add("1zhangsan","3lisi","2wangwu");
    }

    @Test
    public void getValue(){
        Set myset = redisTemplate.boundSetOps("myset").members();
        System.out.println(myset);
    }

    /**
     * 删除一个或多个值
     */
    @Test
    public void removeValue(){
        redisTemplate.boundSetOps("myset").remove("lisi");
    }
    @Test
    public void deleteValue(){
        redisTemplate.delete("myset");
    }


}

 

posted on 2018-11-06 18:15  雨后黄昏  阅读(208)  评论(0编辑  收藏  举报

导航