Redis was used for writing requirements the previous two days, although it was already configured in the project. But configure it to yourself. There was a problem with configuration. Then I relearned the redis integration.

Import dependence

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

Redis information is configured in the configuration file

The main configuration information is the IP address, port number, and password, which springBoot defaults to

# Redis database index (default 0)
spring.redis.database=0
# Redis server addressSpring. Redis. Host = 127.0.0.1# Redis server connection port
spring.redis.port=6379
# Redis server connection password (default null)
spring.redis.password= root
# maximum number of connections in the pool (use negative values to indicate no limit)
spring.redis.jedis.pool.max-active=200
Maximum connection pool blocking wait time (negative value indicates no limit)
spring.redis.jedis.pool.max-wait=-1
The maximum number of free connections in the connection pool
spring.redis.jedis.pool.max-idle=10
Minimum free connection in connection pool
spring.redis.jedis.pool.min-idle=0
Connection timeout (ms)
spring.redis.timeout = 6000
Copy the code

RedisTemplate Bean configuration

package com.demo.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; @configuration public class RedisConfig extends CachingConfigurerSupport {/** * Select Redis as the default cache tool * @param redisConnectionFactory * @return*/ @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofHours(1)); // Set the cache validity period to one hourreturnRedisCacheManager .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)) .cacheDefaults(redisCacheConfiguration).build(); } /** * retemplate configuration * @param factory * @return*/ @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); / / configure the connection factory template. SetConnectionFactory (factory); / / use Jackson2JsonRedisSerializer to serialization and deserialization of redis value value (the default using JDK serialization) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // Specify the fields to serialize, field,get, andsetRange, as well as modifier, is ANY including private and public om. SetVisibility (PropertyAccessor. ALL, JsonAutoDetect. Visibility. ANY); // Specify the type of serialized input, class must be non-final, final modified class, Such as String, Integer later ran out of the abnormal om. EnableDefaultTyping (ObjectMapper. DefaultTyping. NON_FINAL); jacksonSeial.setObjectMapper(om); / / value using json serialization template. SetValueSerializer (jacksonSeial); / / use StringRedisSerializer to serialization and deserialization redis template. The key value of setKeySerializer (new StringRedisSerializer ()); / / sethashThe key and the value of serialization pattern template. SetHashKeySerializer (new StringRedisSerializer ()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet();returntemplate; }}Copy the code

test

package com.demo.config;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

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

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Test
    public void testAdd(){
        redisTemplate.opsForValue().set("test"."Test data");
        System.out.println("Added successfully");
    }

    @Test
    public void testGet(){
        String test = redisTemplate.opsForValue().get("test");
        System.out.println(test); }}Copy the code