Redis安装参考:Redis安装
Linux修改Redis配置文件及防火墙的更改参考:Linux防火墙更改Redis配置文件修改
Redis可视化工具下载及安装参考:Redis可视化工具
在项目中引入Redis的包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
在项目的配置文件中加入Redis连接:
#配置redis连接信息
spring.redis.host=192.168.137.129
spring.redis.port=6379
spring.redis.password=root
接着就是测试是否成功,一开始从数据库查询信息,并将其存到缓存中,以后查询就是从缓存中查:
@Autowired
private StudentMapper studentMapper;
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@Override
public List<Student> getAllStudent() {
List<Student> studentList = (List<Student>) redisTemplate.opsForValue().get("allStudents");
if (studentList == null){
studentList = studentMapper.selectAllStudent();
System.out.println("数据库查询");
redisTemplate.opsForValue().set("allStudents",studentList);
}else {
System.out.println("缓存查询");
}
return studentList;
}
结果:
数据库查询
缓存查询
缓存查询
缓存查询
缓存查询