Spring Data Redis - Quick Start

  Environment 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, which provides access to redis services through simple configuration in srping applications, and highly encapsulates the underlying development packages of reids (Jedis, JRedis, and RJC). RedisTemplate provides various operations of redis, Exception handling and serialization, support for publish and subscribe, and implementation of spring 3.1 cache.
spring-data-redis provides the following functions for jedis:


1. The connection pool is automatically managed, providing a highly encapsulated "RedisTemplate" class.

2. Classify and encapsulate a large number of APIs in the jedis client, and encapsulate the same type of operations as operation interfaces

  • 
ValueOperations: Simple key-value pair operations String

  • SetOperations: set type data operation set

  • ZSetOperations: zset type data operations sortedset---->zset

  • HashOperations: Operation hash for data of hash type

  • ListOperations: List of data operations for list type

Second, the entry case

1. Environment construction

  Use springboot to build a project to select redis dependencies

 

2. Configure redis

Modify application.properties to the format of 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 related api 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      * 值得操作
 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("Sun Quan" );
 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, Sun Quan, Cao Cao] 
52      }
 53      // Delete an element in the set 
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     //Right push stack: objects added later are in the back 
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      // Left push stack: objects added later are in the 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 a certain element in the collection 
95      @Test
 96      public  void searchListByIndex(){
 97          String s = (String) redisTemplate.boundListOps( "namelist1").index(1 );
 98          System.out.println(s); // Guan 
Yu99      }
 100      // Remove an element of the collection 
101      @Test
 102      public  void removeListByIndex(){
 103          redisTemplate.boundListOps(" namelist1").remove(1, "Guan Yu" );
 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         // Extract all KEY 
119          Set s = redisTemplate.boundHashOps("namehash" ).keys();
 120          System.out.println(s); // [a, b, c, d]
 121          // Extract all Value 
122          List values ​​= redisTemplate.boundHashOps("namehash" ).values();
 123          System.out.println(values); // [Tang Monk, Wukong, Bajie, Sha Monk]
 124          // Extract value 125 according to KEY
          String str = (String) redisTemplate.boundHashOps("namehash").get("b" );
 126          System.out.println(str); // Goku 127     }
 128 //
      
Remove value 129  according to KEY     @Test
 130      public  void removeHashByKey() {
 131          redisTemplate.boundHashOps("namehash").delete("c" );
 132      }
 133 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325275780&siteId=291194637