A preface

A little knowledge about springboot integration redis, if you have learned the basic redis reader is easy to understand, did not learn to understand the recommendation to look at the content of this article;

Public account: Knowledge seeker

Inheriting the spirit of open Source, Spreading technology knowledge;

2 Integration Configuration

2.1 depend on

Springboot 2.1.1 version

  <dependencies>
        <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>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
Copy the code

2.2 configuration class

Using a custom configuration to replace JdkSerializationRedisSerializer, or serialized into redis will appear as a special characters;

Refer to RedisReactiveAutoConfiguration class can view reactiveRedisTemplate assembly process ‘;

Refer to the RedisAutoConfiguration class to see the redisTemplate assembly process.

/ * * *@AuthorLSC * <p> Redis configuration replaces the default JDK serialization mechanism </p> */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        / / create redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);

        / / use Jackson2JsonRedisSerialize replace the default serialization
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // Key uses String serialization
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // Value serialization uses Jackson
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // The hash key also uses String serialization
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // The hash value serialization method uses Jackson
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        returnredisTemplate; }}Copy the code

2.3 application. Yml

Redis connection property configuration and connection pool configuration;

server:
  port: 9000

spring:
  redis:
    # Redis server address
    host: localhost
    #Redis server connection port
    port: 6379
    #password:
    # Redis database index (default 0)
    database: 1
    Connection timeout (ms)
    timeout: 5000
    jedis:
      pool:
        # maximum number of connections in the pool (use negative values to indicate no limit)
        max-active: 100
        Minimum free connection in connection pool
        max-idle: 10
        Maximum connection pool blocking wait time (negative value indicates no limit)
        max-wait: 100000
Copy the code

Data type test

3.1 the String operation

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    / / the string into the Treasury
    @Test
    public void testForValue1(a){
        String key = "zszxz";
        String value = "Knowledge seekers";
        redisTemplate.opsForValue().set(key, value);
    }
    
    / / string output
    @Test
    public void testForValue2(a){
        String key = "zszxz";
        Object value = redisTemplate.opsForValue().get(key);
        // Knowledge seekers
        System.out.println(value);
    }

    // string Key expiration time
    @Test
    public void testForValue3(a){
        String key = "today";
        String value = "Saturday";
        long time = 60;
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }
Copy the code

3.2 the key operation

	@Autowired
    RedisTemplate<String,Object> redisTemplate;

    // Tests whether the key exists
    @Test
    public void test1(a){
        String key = "zszxz";
        Boolean exist = redisTemplate.hasKey(key);
        // true
        System.out.println(exist);
    }
    // Set the key expiration time
    @Test
    public void test2(a){
        String key = "zszxz";
        long time = 60;
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    // Get the key expiration time
    @Test
    public void test3(a){
        String key = "zszxz";
        Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
        / / 7
        System.out.println(expire);
    }

    / / delete key
    @Test
    public void test4(a){
        String key = "zszxz";
        redisTemplate.delete(key);
    }
Copy the code

3.3 Hash Operations

 	@Autowired
    RedisTemplate<String,Object> redisTemplate;

    // Add a hash (key value)
    @Test
    public void test1(a){
        String key = "zszxz";
        String item = "name";
        String value = "Knowledge seekers";
        redisTemplate.opsForHash().put(key, item, value);
    }

    // Store a map to the hash
    @Test
    public void test2(a){
        String key = "feature";
        Map<String, Object> map = new HashMap<>();
        map.put("name"."Knowledge seekers");
        map.put("age"."18");
        redisTemplate.opsForHash().putAll(key, map);
    }

    // Get all key-values of a hash
    @Test
    public void test3(a){
        String key = "feature";
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
        // {name= knowledge seeker, age=18}
        System.out.println(entries);
    }

    // Get a hash key value
    @Test
    public void test4(a){
        String key = "feature";
        String item = "name";
        Object value = redisTemplate.opsForHash().get(key, item);
        // Knowledge seekers
        System.out.println(value);
    }

    // Delete the value of the hash key
    @Test
    public void test5(a){
        String key = "zszxz";
        String item = "name";
        redisTemplate.opsForHash().delete(key, item);
    }

    // Whether there is a hash key
    @Test
    public void test6(a){
        String key = "zszxz";
        String item = "name";
        Boolean exist = redisTemplate.opsForHash().hasKey(key, item);
        // false
        System.out.println(exist);
    }
Copy the code

3.4 List Operations

 	@Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Test
    public void test(a){}// Push the list right
    @Test
    public void test1(a){
        String key = "zszxz";
        String value = "Knowledge seekers";
        redisTemplate.opsForList().rightPush(key, value);
    }

    // Push the list left
    @Test
    public void test2(a){
        String key = "zszxz";
        String value = "Rain or shine";
        redisTemplate.opsForList().leftPush(key, value);
    }
    // The list pops up left
    @Test
    public void test3(a){
        String key = "zszxz";
        Object value = redisTemplate.opsForList().leftPop(key);
        / sunny/rainy day
        System.out.println(value);

    }
    // The list pops up right
    @Test
    public void test4(a){
        String key = "zszxz";
        Object value = redisTemplate.opsForList().rightPop(key);
        // Knowledge seekers
        System.out.println(value);
    }

    // Push the list right into the list
    @Test
    public void test5(a){
        ArrayList<Object> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        String key = "number";
        redisTemplate.opsForList().rightPushAll(key, list);
    }

    // Change the value of the index specified in the list
    @Test
    public void test7(a){
        String key = "number";
        int index = 0;
        int value = Awesome!;
        redisTemplate.opsForList().set(key, index, value);
    }

    // Gets the value of the specified index of the list
    @Test
    public void test8(a){
        String key = "number";
        int index = 0;
        Object value = redisTemplate.opsForList().index(key, index);
        / / 666
        System.out.println(value);
    }
Copy the code

3.5 set operation

  @Autowired
    RedisTemplate<String,Object> redisTemplate;

    // Set stores values
    @Test
    public void test1(a){
        String key = "zszxz";
        String value1 = "Rain or shine";
        String value2 = "Public account: Knowledge Seeker";
        redisTemplate.opsForSet().add(key, value1, value2);
    }

    // Take the value from set
    @Test
    public void test2(a){
        String key = "zszxz";
        Set<Object> members = redisTemplate.opsForSet().members(key);
        // [Rainy day, public id: Knowledge Seeker]
        System.out.println(members);
    }

    // Check whether there is a key-value in set
    @Test
    public void test3(a){
        String key = "zszxz";
        String value = "Rain or shine";
        Boolean member = redisTemplate.opsForSet().isMember(key, value);
        // true
        System.out.println(member);
    }
Copy the code

Focus on knowledge seekers: