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

完整demo下載地址:https://download.csdn.net/download/ganjing222/10647902

剛剛纔搭建好了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"));
    }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章