Spring Data Redis - quick start

Environmental requirements: Redis 2.6 and above, javase 8.0 and above;

1, Introduction to Spring Data Redis

Spring data redis is a part of spring. It provides access to redis services through simple configuration in srping applications. It highly encapsulates the underlying development package of reids (Jedis, JRedis, and RJC). RedisTemplate provides various redis operations, exception handling and serialization, supports publishing and subscription, and implements spring 3.1 cache.
Spring data redis provides the following functions for jedis:

1. Connection pool automatic management provides a highly encapsulated "RedisTemplate" class

2. A large number of APIs in jedis client are classified and encapsulated, and the same type of operation is encapsulated as operation interface

  • 
 ValueOperations: simple key value pair operation String 

  • SetOperations: set type data operation set 

  • ZSetOperations: zset type data operationsortedset ------ > zset 

  • HashOperations: for data operations of hash type hash 

  • ListOperations: list of data operations of type list

2, Entry case

1. Environment construction

Using spring boot to build projects to select redis dependency

 

2. Configure redis

Change application.properties to application.yml format

 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 related APIs in SpringDataRedisDemoApplicationTests.java in test

  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      * Worth operating
 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 Operation of type
 41      */
 42     @Test
 43     public void setSetValue(){
 44         redisTemplate.boundSetOps("nameset").add("Cao Cao");
 45         redisTemplate.boundSetOps("nameset").add("king of Wu in the Three Kingdoms Era");
 46         redisTemplate.boundSetOps("nameset").add("Liu Bei");
 47     }
 48     @Test
 49     public void getSetValue(){
 50         Set nameset = redisTemplate.boundSetOps("nameset").members();
 51         System.out.println(nameset);//[Liu Bei, king of Wu in the Three Kingdoms Era, Cao Cao]
 52     }
 53     //Delete an element in the collection
 54     @Test
 55     public void deleteSetValue(){
 56         Long remove = redisTemplate.boundSetOps("nameset").remove("Liu Bei");
 57         System.out.println(remove);
 58         Assert.assertEquals("1",remove);
 59     }
 60     //Delete entire collection
 61     @Test
 62     public void deleteSet(){
 63         Boolean nameset = redisTemplate.delete("nameset");
 64         Assert.assertEquals(true,nameset);
 65     }
 66 
 67     /**
 68      * List Type operation
 69      */
 70     //Right stack: objects added later are listed later
 71     @Test
 72     public void setListValue1(){
 73         redisTemplate.boundListOps("namelist1").rightPush("Liu Bei");
 74         redisTemplate.boundListOps("namelist1").rightPush("Guan Yu");
 75         redisTemplate.boundListOps("namelist1").rightPush("Zhang Fei");
 76     }
 77     @Test
 78     public void getListValue1(){
 79         List list = redisTemplate.boundListOps("namelist1").range(0, -1);
 80         System.out.println(list);//[Liu Bei, Guan Yu, Zhang Fei]
 81     }
 82     //Stack left: objects added after are in front
 83     @Test
 84     public void setListValue2(){
 85         redisTemplate.boundListOps("namelist2").leftPush("Liu Bei");
 86         redisTemplate.boundListOps("namelist2").leftPush("Guan Yu");
 87         redisTemplate.boundListOps("namelist2").leftPush("Zhang Fei");
 88     }
 89     @Test
 90     public void getListValue2(){
 91         List list = redisTemplate.boundListOps("namelist2").range(0, -1);
 92         System.out.println(list);//[Zhang Fei, Guan Yu, Liu Bei]
 93     }
 94     //Query collection for an element
 95     @Test
 96     public void searchListByIndex(){
 97         String s = (String) redisTemplate.boundListOps("namelist1").index(1);
 98         System.out.println(s);//Guan Yu
 99     }
100     //Remove a collection element
101     @Test
102     public void removeListByIndex(){
103         redisTemplate.boundListOps("namelist1").remove(1, "Guan Yu");
104     }
105 
106     /**
107      * Hash Type operation
108      */
109     @Test
110     public void setHashValue(){
111         redisTemplate.boundHashOps("namehash").put("a", "Tang Seng");
112         redisTemplate.boundHashOps("namehash").put("b", "name of a fictitious monkey with supernatural powers");
113         redisTemplate.boundHashOps("namehash").put("c", "Eight quit");
114         redisTemplate.boundHashOps("namehash").put("d", "Sha Seng");
115     }
116     @Test
117     public void getHash(){
118         //Extract all KEY
119         Set s = redisTemplate.boundHashOps("namehash").keys();
120         System.out.println(s);//[a, b, c, d]
121         //Extract all values
122         List values = redisTemplate.boundHashOps("namehash").values();
123         System.out.println(values);//[Tang Seng, name of a fictitious monkey with supernatural powers, Eight quit, Sha Seng]
124         //according to KEY Extraction value
125         String str = (String) redisTemplate.boundHashOps("namehash").get("b");
126         System.out.println(str);//name of a fictitious monkey with supernatural powers
127     }
128     //according to KEY Remove value
129     @Test
130     public void removeHashByKey() {
131         redisTemplate.boundHashOps("namehash").delete("c");
132     }
133 }

Tags: Java Redis Spring Jedis

Posted on Sat, 21 Mar 2020 13:20:30 -0400 by kmussel