Redis的安装:https://blog.csdn.net/weixin_45412021/article/details/97134995
Springboot集成redis session
1、pom中引入redis相关依赖
<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring session -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
2、application.properties文件中添加redis初始化配置
spring.redis.host=xxx
spring.redis.password=xxx
spring.redis.port=6379
spring.redis.timeout=0 //超时时间,0表示无限时等待。若当前连接空闲时间超过timeout则会被redis服务端回收掉
spring.redis.pool.max-idle=150 //最大空闲数
spring.redis.pool.max-active=300 //线程池内的最大连接数
3、创建Redis配置类
- maxInactiveIntervalInSeconds表示redis session的过期时间,这个时间后会自动销毁(单位是秒)
- 如果仅仅是将redis用作缓存session那么到这一步就可以结束了,这个Config的类只需添加@Configuration和@EnableRedisHttpSession注解就可以了,我们的项目有使用redis存数据的需求所以我有创建Jedis连接池
- 注意:配置名称一定要用redis默认的,否则设置的连接数在redisSession上不会生效
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600 * 24)
public class RedisSessionConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.max-active}")
private int maxTotal;
@Value("${spring.redis.password}")
private String pwd;
@Bean
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxTotal(maxTotal);
//取Jedis连接时进行校验,判断当前连接是否可用
jedisPoolConfig.setTestOnBorrow(true);
//返回Jedis连接时进行是否可用的校验
jedisPoolConfig.setTestOnReturn(true);
return new JedisPool(jedisPoolConfig, host, port, timeout, pwd);
}
}
4、创建RedisService类包装
@Component
public class RedisService {
private static final Logger logger = LoggerFactory.getLogger(RedisService.class);
@Autowired
private JedisPool jedisPool;
//取值
public String get(String key){
if(StringUtils.isBlank(key)){
logger.info("redis get, key can't be empty");
return null;
}
Jedis jedis = jedisPool.getResource();
try {
return jedis.get(key);
} catch (Exception e) {
logger.error("redis get error, ", e);
return null;
}finally {
close(jedis);
}
}
//存值
public Boolean set(String key, String value){
if(StringUtils.isBlank(key) || StringUtils.isBlank(value)){
logger.info("redis set, key and value can't be empty");
return false;
}
Jedis jedis = jedisPool.getResource();
try {
String set = jedis.set(key, value);
return "OK".equals(set);
} catch (Exception e) {
logger.error("redis set error, ", e);
return false;
}finally {
close(jedis);
}
}
//根据key删除
public Boolean del(String key){
if(StringUtils.isBlank(key)){
logger.info("redis del, key can't be empty");
return false;
}
Jedis jedis = jedisPool.getResource();
try {
Long del = jedis.del(key);
return 0 < del;
} catch (Exception e) {
logger.error("redis get error, ", e);
return false;
}finally {
close(jedis);
}
}
//回收连接
public void close(Jedis jedis){
jedis.close();
}
}