Spring Boot integrates Redis

Keywords: Programming Redis Spring Database JSON

Preface

This article will simply integrate Redis based on springboot 2.1.8.RELEASE, which is suitable for beginners

Introduction to Spring Boot integrated Redis

1. Introduce redis dependency in pom.xml

<!-- Redis rely on -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. Configure Redis connection parameters in the application.yml configuration file

spring:
  # Redis data source
  redis:
    # Redis database index (0 by default)
    database: 0
    # Redis server address
    host: 127.0.0.1
    # Redis server connection port
    port: 6379
    # Connection timeout (MS
    timeout: 6000
    # Redis server connection password (empty by default)
    password:
    jedis:
      pool:
        max-active: 1000  # Maximum number of connections in the connection pool (use a negative value to indicate no limit)
        max-wait: -1      # Connection pool maximum block wait time (use a negative value to indicate no limit)
        max-idle: 10      # Maximum free connections in connection pool
        min-idle: 5       # Minimum free connections in connection pool

3. Redis core configuration class

Warm tip: pay attention to setting the serialization method of key and value here, otherwise the data stored in redis will be jumbled

@Configuration
public class RedisConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        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);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key is serialized by String
        template.setKeySerializer(stringRedisSerializer);
        // The hash key also uses String serialization
        template.setHashKeySerializer(stringRedisSerializer);
        // value serialization is jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // value serialization of hash adopts jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

4. Simple test

@RestController
@RequestMapping("/api")
@Api(description = "test-Interface")
public class IndexController extends BaseController {

    private final String key = "sysLog";
    
    @Autowired
    private ILogService logService;
    
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping(value = "/saveData", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "Save data", httpMethod = "POST", response = ApiResult.class)
    public ApiResult saveData(@RequestBody SysLog sysLog) {
        List<SysLog> sysLogList = logService.selectList(null);
        redisTemplate.opsForValue().set(key, sysLogList);
        return ApiResult.ok("SUCCESS");
    }

    @GetMapping(value = "/getData", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "get data", httpMethod = "GET", response = ApiResult.class)
    public ApiResult getData() {
        List<SysLog> result = (List<SysLog>) redisTemplate.opsForValue().get(key);
        return ApiResult.ok("SUCCESS", result);
    }

}

demo source code of this case

Warm tip: redis tool class, visualization tool and redis of windows are included in the demo source code

https://gitee.com/zhengqingya/java-workspace

Posted by frans-jan on Wed, 18 Dec 2019 06:32:37 -0800