*丰富的特性 – 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集群搭建性能优化