springboot-整合redis:做缓存,做分布式共享session
完整代码下载链接:
https://github.com/2010yhh/springBoot-demos/tree/master/springboot-redis
环境
idea2018,jdk1.8,
springboot版本:springboot1.5.9.RELEASE,
1.redis应用场景
参见这篇
2配置
pom需要:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
redis配置类:RedisConfig
@Configuration
@EnableCaching//开启缓存注解
//maxInactiveIntervalInSeconds:session的统一过期时间,默认是1800秒过期,这里测试修改为60秒
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)//注解,开启redis集中session管理
public class RedisConfig {
// 以下redisTemplate自由根据场景选择
//默认的String-String
// @Bean
public RedisTemplate RedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringR