68、Spring Data Redis

佐手、 提交于 2019-11-30 12:27:59

SpringDataRedis是Spring大家族中的一部分,提供了在Spring应用中通过简单的配置访问redis服务,对redis底层开发包进行了高度封装,RedisTemplate提供了redis各种操作,异常处理及序列化,支持分布订阅,并对spring3.1cache进行了实现。

SpringDataRedis提供的功能:

1、连接池自动管理,提供了一个高度封装的RedisTemplate类

2、针对Jedis客户端中大量的API进行了归类封装,将同一类型操作封装为operation接口

ValueOperations:简单K-V操作

SetOperations:set类型数据操作

ZSetOperations:zset类型数据操作

HashOperations:针对map类型的数据操作

ListOperations:针对list类型的数据操作

 

入门案例:

1、导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>

</dependency>

2、创建启动类

@SpringBootApplication
public class RedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class);
    }
}

3、操作值类型

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTests {

    @Autowired
    private RedisTemplate template;


    @Test
    public void setValue (){
        template.boundValueOps("name").set("lianbang.wu");
    }

    @Test
    public void getValue(){
        String str =(String) template.boundValueOps("name").get();
        System.out.println(str);
    }

    @Test
    public void deleteValue(){
        template.delete("name");
    }

}

4、操作set类型

@Test
public void setValue(){
    template.boundSetOps("nameset").add("读者1");
    template.boundSetOps("nameset").add("读者2");
    template.boundSetOps("nameset").add("读者3");
    
}

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

@Test
public void deleteValue(){
    template.boundSetOps("nameset").remove("读者1");
}

@Test
public void deleteAll(){
    template.delete("nameset");
}

5、 操作List

@Test
 public void testSetValue(){
    template.boundListOps("namelist1").rightPush("读者1");
    template.boundListOps("namelist1").leftPush("读者2");
    
}

@Test
 public void testGetValue(){
    List namelist1 = template.boundListOps("namelist1").range(0, 10);
    System.out.println(namelist1);
}

@Test
 public void testSearechIndex(){
    Object namelist1 = template.boundListOps("namelist1").index(1);
    System.out.println(namelist1);
}

@Test
 public void testRemoveByIndex(){
    template.boundListOps("namelist1").remove(1,"读者1");
    
}

6、操作Hash类型

@Test
public void testSetValue(){
    template.boundHashOps("namehash").put("a","读者1");
    template.boundHashOps("namehash").put("b","读者2");
}

@Test
public void testGetKeys(){
    Set s = template.boundHashOps("namehash").keys();
    System.out.println(s);
}

@Test
public void testGetValue(){
    List values = template.boundHashOps("namehash").values();
    System.out.println(values);
}

@Test
public void testGetValueByKey(){
    Object o = template.boundHashOps("namehash").get("a");
    System.out.println(o);
}

@Test
public void testRemoveValueByKey(){
    template.boundHashOps("namehash").delete("b");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!