Home > Database > Redis > body text

Springboot integrated redis instance analysis

WBOY
Release: 2023-06-03 19:07:02
forward
923 people have browsed it

Import redis pom file

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

Write redis configuration

spring:
  redis:
    password:
    port: 6379
    host: localhost
    database: 0
    jedis:
      pool:
        ## 连接池最大连接数(使用负值表示没有限制)
        #spring.redis.pool.max-active=8
        max-active: 8
        ## 连接池最大阻塞等待时间(使用负值表示没有限制)
        #spring.redis.pool.max-wait=-1
        max-wait: -1
        ## 连接池中的最大空闲连接
        #spring.redis.pool.max-idle=8
        max-idle: 8
        ## 连接池中的最小空闲连接
        #spring.redis.pool.min-idle=0
        min-idle: 0
      ## 连接超时时间(毫秒)
    lettuce:
      shutdown-timeout: 0
Copy after login

Write springConfig file

Since storage requires serialization, we need to configure the serialization method of redis. If If not configured, both key and value use StringRedisSerializer by default, which can only be used to store String type data, so we need to configure our commonly used types. At the same time, our Java entity class must also inherit the Serializable interface

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String , Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}
Copy after login

Testing redis

Before this step, we must make sure that the connected redis service has been started

@Autowired
    private RedisTemplate<String , Object> redisTemplate;
@Test
    public void testSelect() throws SQLException {
        redisTemplate.opsForValue().set("qqq",userMapper.findByUname("zengkaitian"));
        System.out.println("redis中获取的:"+redisTemplate.opsForValue().get("qqq"));
    }
Copy after login

Test results
Springboot integrated redis instance analysis

The above is the detailed content of Springboot integrated redis instance analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template