Spring Data Redis —— 快速入门

  环境要求:Redis 2.6及以上,javase 8.0及以上;

一、Spring Data Redis 介绍

  Spring-data-redis是spring的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:


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

2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口

  • 
ValueOperations:简单键值对操作 String

  • SetOperations:set类型数据操作 set

  • ZSetOperations:zset类型数据操作 sortedset---->zset

  • HashOperations:针对hash类型的数据操作 hash

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

二、入门案例

1、环境构建

  使用springboot构建项目选择redis依赖

 

2、配置redis

将application.properties修改为application.yml的格式

 1 spring:
 2   redis:
 3     database: 0
 4     host: localhost
 5     port: 6379
 6     password:
 7     jedis:
 8       pool:
 9         max-active: 8
10         max-idle: 8
11         min-idle: 0

3、在test中SpringDataRedisDemoApplicationTests.java中测试相关api

  1 package com.cenobitor.spring_data_redis_demo;
  2 
  3 import org.junit.Assert;
  4 import org.junit.Test;
  5 import org.junit.runner.RunWith;
  6 import org.springframework.beans.factory.annotation.Autowired;
  7 import org.springframework.boot.test.context.SpringBootTest;
  8 import org.springframework.data.redis.core.RedisTemplate;
  9 import org.springframework.test.context.junit4.SpringRunner;
 10 import java.util.List;
 11 import java.util.Set;
 12 
 13 @RunWith(SpringRunner.class)
 14 @SpringBootTest
 15 public class SpringDataRedisDemoApplicationTests {
 16 
 17     @Autowired
 18     private RedisTemplate redisTemplate;
 19 
 20     @Test
 21     public void contextLoads() {
 22     }
 23 
 24     /**
 25      * 值得操作
 26      */
 27     @Test
 28     public void setValue(){
 29         redisTemplate.boundValueOps("name").set("redis");
 30     }
 31 
 32     @Test
 33     public void getValue(){
 34         String str = (String) redisTemplate.boundValueOps("name").get();
 35         System.out.println(str);
 36         Assert.assertNotNull(str);
 37     }
 38 
 39     /**
 40      * set类型的操作
 41      */
 42     @Test
 43     public void setSetValue(){
 44         redisTemplate.boundSetOps("nameset").add("曹操");
 45         redisTemplate.boundSetOps("nameset").add("孙权");
 46         redisTemplate.boundSetOps("nameset").add("刘备");
 47     }
 48     @Test
 49     public void getSetValue(){
 50         Set nameset = redisTemplate.boundSetOps("nameset").members();
 51         System.out.println(nameset);//[刘备, 孙权, 曹操]
 52     }
 53     //删除集合中的某个元素
 54     @Test
 55     public void deleteSetValue(){
 56         Long remove = redisTemplate.boundSetOps("nameset").remove("刘备");
 57         System.out.println(remove);
 58         Assert.assertEquals("1",remove);
 59     }
 60     //删除整个集合
 61     @Test
 62     public void deleteSet(){
 63         Boolean nameset = redisTemplate.delete("nameset");
 64         Assert.assertEquals(true,nameset);
 65     }
 66 
 67     /**
 68      * List类型操作
 69      */
 70     //右压栈:后添加的对象排在后边
 71     @Test
 72     public void setListValue1(){
 73         redisTemplate.boundListOps("namelist1").rightPush("刘备");
 74         redisTemplate.boundListOps("namelist1").rightPush("关羽");
 75         redisTemplate.boundListOps("namelist1").rightPush("张飞");
 76     }
 77     @Test
 78     public void getListValue1(){
 79         List list = redisTemplate.boundListOps("namelist1").range(0, -1);
 80         System.out.println(list);//[刘备, 关羽, 张飞]
 81     }
 82     //左压栈:后添加的对象排在前边
 83     @Test
 84     public void setListValue2(){
 85         redisTemplate.boundListOps("namelist2").leftPush("刘备");
 86         redisTemplate.boundListOps("namelist2").leftPush("关羽");
 87         redisTemplate.boundListOps("namelist2").leftPush("张飞");
 88     }
 89     @Test
 90     public void getListValue2(){
 91         List list = redisTemplate.boundListOps("namelist2").range(0, -1);
 92         System.out.println(list);//[张飞, 关羽, 刘备]
 93     }
 94     //查询集合某个元素
 95     @Test
 96     public void searchListByIndex(){
 97         String s = (String) redisTemplate.boundListOps("namelist1").index(1);
 98         System.out.println(s);//关羽
 99     }
100     //移除集合某个元素
101     @Test
102     public void removeListByIndex(){
103         redisTemplate.boundListOps("namelist1").remove(1, "关羽");
104     }
105 
106     /**
107      * Hash类型操作
108      */
109     @Test
110     public void setHashValue(){
111         redisTemplate.boundHashOps("namehash").put("a", "唐僧");
112         redisTemplate.boundHashOps("namehash").put("b", "悟空");
113         redisTemplate.boundHashOps("namehash").put("c", "八戒");
114         redisTemplate.boundHashOps("namehash").put("d", "沙僧");
115     }
116     @Test
117     public void getHash(){
118         //提取所有的KEY
119         Set s = redisTemplate.boundHashOps("namehash").keys();
120         System.out.println(s);//[a, b, c, d]
121         //提取所有的值
122         List values = redisTemplate.boundHashOps("namehash").values();
123         System.out.println(values);//[唐僧, 悟空, 八戒, 沙僧]
124         //根据KEY提取值
125         String str = (String) redisTemplate.boundHashOps("namehash").get("b");
126         System.out.println(str);//悟空
127     }
128     //根据KEY移除值
129     @Test
130     public void removeHashByKey() {
131         redisTemplate.boundHashOps("namehash").delete("c");
132     }
133 }

 

posted @ 2018-05-04 15:20  gdwkong  阅读(3803)  评论(0编辑  收藏  举报