*豐富的特性 – Redis還支持publish/subscribe, key過時等特性。html
<!-- Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
spring: redis: host: 127.0.0.1 port: 6379 password:
package com.blog.tutorial.controller; import com.blog.tutorial.entity.Users; import com.blog.tutorial.service.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.concurrent.TimeUnit; /** * @description: * @author: youcong * @time: 2020/11/14 13:27 */@RestController @RequestMapping("/user") public class UserController { @Autowired private UsersService usersService; @Autowired private RedisTemplate redisTemplate; @GetMapping("/list") public String list() { System.out.println("list:"+redisTemplate.opsForValue().get("list")); if (StringUtils.isEmpty(redisTemplate.opsForValue().get("list"))) { redisTemplate.opsForValue().set("list", usersService.list(), 360, TimeUnit.MINUTES); } return redisTemplate.opsForValue().get("list").toString(); } }
請求接口,以下:java
控制檯,以下:web
初次請求,會打印SQL,再次請求只會輸出Redis的key,同時頁面接口響應時間很是快。redis
關於上述框架使用,我在個人博客園寫下以下幾篇文章,感興趣的能夠看看:
SpringBoot整合Redisson\(單機版\)spring
SpringBoot實戰\(七\)之與Redis進行消息傳遞數據庫
redis集羣搭建性能優化