1. 程式人生 > >(飛歌筆記)springboot 2.03版本+ redis3.2版本 + yml配置檔案

(飛歌筆記)springboot 2.03版本+ redis3.2版本 + yml配置檔案

剛剛才搭建好了springboot 2.03版本+ redis3.2版本 + yml配置檔案。發現網上相關與redis有幾個坑。

1: spring-boot-starter-data-redis與spring-boot-starter-redis兩個包的區別

看到很多網上的文章引入的是spring-boot-starter-redis。但是我在引入這個包時,卻發現沒有。這個是因為springBoot的版本為1.4.7 以上的時候,spring-boot-starter-redis這個是就不存在了。而對應的是spring-boot-starter-data-redis

2:關於CacheManager cacheManager = new RedisCacheManager(redisTemplate)找不到方法問題。

這個問題主要也是版本問題引起的,new RedisCacheManager(redisTemplate)是版本1.*的方法。new RedisCacheManager(cacheWriter, defaultCacheConfiguration);是2.0的方法.

程式碼:pom.xml

           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <version>2.0.3.RELEASE</version>
            </dependency>

application-dev.yml

spring:
    redis:
          database: 0
          host: 127.0.0.1
          port: 6379
          timeout: 5000
          password: 
          pool:
               max-active: 0
               max-wait: -1
               max-idle: 8
               min-idle: 0

注意:timeout: 別設定為0.,

當設定為0時,可能會報

io.lettuce.core.RedisCommandTimeoutException: Command timed out

controller


@RestController
public class HelloController {
    
    @Autowired
     private StringRedisTemplate redisTemplate;

    
    @RequestMapping(value = "/demoTest",method = RequestMethod.GET)
    public void demoTest(){
        redisTemplate.opsForValue().set("test44","test44");
        System.out.println(redisTemplate.opsForValue().get("test44"));
    }

}