Spring Data Redis 小demo

一、简介

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类型的数据操作

二、添加依赖

<!-- 缓存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>

三、配置文件

a、src/main/resources下创建properties文件夹,建立redis-config.properties 

# Redis settings 
# server IP 
redis.host=192.168.200.128
# server port 
redis.port=6379
# server pass 
redis.pass= 
# use dbIndex 
redis.database=0
# u63A7u5236u4E00u4E2Apoolu6700u591Au6709u591Au5C11u4E2Au72B6u6001u4E3Aidle(u7A7Au95F2u7684)u7684jedisu5B9Eu4F8B 
redis.maxIdle=300
# u8868u793Au5F53borrow(u5F15u5165)u4E00u4E2Ajedisu5B9Eu4F8Bu65F6uFF0Cu6700u5927u7684u7B49u5F85u65F6u95F4uFF0Cu5982u679Cu8D85u8FC7u7B49u5F85u65F6u95F4(u6BEBu79D2)uFF0Cu5219u76F4u63A5u629Bu51FAJedisConnectionExceptionuFF1B  
redis.maxWait=3000
# u5728borrowu4E00u4E2Ajedisu5B9Eu4F8Bu65F6uFF0Cu662Fu5426u63D0u524Du8FDBu884Cvalidateu64CDu4F5CuFF1Bu5982u679Cu4E3AtrueuFF0Cu5219u5F97u5230u7684jedisu5B9Eu4F8Bu5747u662Fu53EFu7528u7684  
redis.testOnBorrow=true 

b、在src/main/resources下创建spring文件夹 ,创建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">  
  
   
  
   <!-- 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" />  
   </bean>  
      
</beans>  

四、测试代码

a、值类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestValue {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Test
    public void setValue(){
        redisTemplate.boundValueOps("name").set("offcn");        
    }
    
    @Test
    public void getValue(){
        String str = (String) redisTemplate.boundValueOps("name").get();
        System.out.println(str);        
    }
    
    @Test
    public void deleteValue(){
        redisTemplate.delete("name");
    }

}

b、set类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestSet {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Test
    public void setValue(){
        redisTemplate.boundSetOps("nameset").add("曹操");
        redisTemplate.boundSetOps("nameset").add("刘备");
        redisTemplate.boundSetOps("nameset").add("孙权");        
    }
    
    @Test
    public void getValue(){
        Set set = redisTemplate.boundSetOps("nameset").members();
        System.out.println(set);        
    }
    
    @Test
    public void removeValue(){
        redisTemplate.boundSetOps("nameset").remove("孙权");
    }
    
    @Test
    public void delete(){
        redisTemplate.delete("nameset");
    }

}

c、list类型操作

右压栈:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestList {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    /*
     * 右压栈 : 后添加的元素排在后边
     */
    @Test
    public void testSetValue1(){
        redisTemplate.boundListOps("namelist1").rightPush("刘备");
        redisTemplate.boundListOps("namelist1").rightPush("关羽");
        redisTemplate.boundListOps("namelist1").rightPush("张飞");
    }
    
    /**
     * 显示右压栈的值
     */
    @Test
    public void testGetValue1(){
        List list = redisTemplate.boundListOps("namelist1").range(0, 10);
        System.out.println(list);
    }
    
    @Test
    public void delete(){
        redisTemplate.delete("namelist1");
    }

}

左压栈:

    /**
     * 左压栈
     */
    @Test
    public void testSetValue2(){
        redisTemplate.boundListOps("namelist2").leftPush("刘备");
        redisTemplate.boundListOps("namelist2").leftPush("关羽");
        redisTemplate.boundListOps("namelist2").leftPush("张飞");
    }
    
    /**
     * 显示左压栈的值
     */
    @Test
    public void testGetValue2(){
        List list = redisTemplate.boundListOps("namelist2").range(0, 10);
        System.out.println(list);
    }

d、hash类型

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestHash {

    @Autowired
    private RedisTemplate redisTemplate;
    
    /**
     * 存值
     */
    @Test
    public void testSetValue(){
        redisTemplate.boundHashOps("namehash").put("a", "唐僧");
        redisTemplate.boundHashOps("namehash").put("b", "悟空");
        redisTemplate.boundHashOps("namehash").put("c", "八戒");
        redisTemplate.boundHashOps("namehash").put("d", "沙僧");
        
    }
原文地址:https://www.cnblogs.com/lqcswy/p/11900624.html