redis部分
国际惯例:
1.依赖包添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.添加redis相关配置文件,我这里拿集群举例
3.新增redis缓存配置类
4.编写测试类
(1)存string类型的测试
(2)存对象类型的测试
(3)redis库中数据展示
(5)自动根据方法生成缓存
@RequestMapping("/get-user")
@Cacheable(value = "user-key")
public User getUser() {
User user = new User();
user.setUserName("weixiao2");
user.setPassword("123456");
return user;
}
session部分
1.新增依赖包
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
2.新增session类
用@EnableRedisHttpSession注解启用session缓存功能
3.编写测试代码来验证
4.查看redis
以上内容部分来源:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html