《Redis缓存二》Springboot+redis缓存

1.redis依赖包

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

2.设置缓存过期时间

    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @RequestMapping("/testredis")
    @ResponseBody
    public String testredis(){
        String redisKey = "testRedis";
        String value = redisTemplate.opsForValue().get(redisKey);//根据key获取缓存中的val
        if (StringUtils.isEmpty(value)) {
            //向redis里存入数据和设置缓存时间:30秒过期
            redisTemplate.opsForValue().set(redisKey, "100",30, TimeUnit.SECONDS);
            return "第一次缓存";
        } else {
           return "缓存中";
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_36029699/article/details/86507503