java redis管理session_springboot 使用 redis 管理session

本文介绍了如何在SpringBoot应用中使用Redis来管理Session。首先在pom.xml中引入了相关依赖,然后配置了Redis连接信息。接着,创建了RedisConfig类,设置了KeyGenerator、CacheManager和RedisTemplate。通过@Cacheable注解实现方法级别的缓存。最后,通过@EnableRedisHttpSession配置实现了Session的共享,指定了Session的失效时间。
摘要由CSDN通过智能技术生成

1.pom.xml 引入依赖:

org.springframework.boot

spring-boot-starter-redis

org.springframework.session

spring-session-data-redis

2、添加配置文件

# REDIS (RedisProperties)

# Redis数据库索引(默认为0)

spring.redis.database=0

# Redis服务器地址

spring.redis.host=192.168.0.58

# Redis服务器连接端口

spring.redis.port=6379

# Redis服务器连接密码(默认为空)

spring.redis.password=

# 连接池最大连接数(使用负值表示没有限制)

spring.redis.pool.max-active=8

# 连接池最大阻塞等待时间(使用负值表示没有限制)

spring.redis.pool.max-wait=-1

# 连接池中的最大空闲连接

spring.redis.pool.max-idle=8

# 连接池中的最小空闲连接

spring.redis.pool.min-idle=0

# 连接超时时间(毫秒)

spring.redis.timeout=0

3、添加cache的配置类

@Configuration

@EnableCaching

public class RedisConfig extends CachingConfigurerSupport{

@Bean

//自定义key生成策略

public KeyGenerator keyGenerator() {

return new KeyGenerator() {

@Override

public Object generate(Object target, Method method, Object... params) {

StringBuilder sb = new StringBuilder();

sb.append(target.getClass().getName());

sb.append(method.getName());

for (Object obj : params) {

sb.append(obj.toString());

}

return sb.toString();

}

};

}

@Bean

//缓存管理器

public CacheManager cacheManager(RedisTemplate redisTemplate) {

RedisCacheManager rcm = new RedisCacheManager(redisTemplate);

//设置缓存过期时间

rcm.setDefaultExpiration(60);//秒

return rcm;

}

@Bean

//配置RedisTemplate

public RedisTemplate redisTemplate(RedisConnectionFactory factory) {

StringRedisTemplate template = new StringRedisTemplate(factory);

Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper om = new ObjectMapper();

om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

jackson2JsonRedisSerializer.setObjectMapper(om);

template.setValueSerializer(jackson2JsonRedisSerializer);

template.afterPropertiesSet();

return template;

}

}

4、自动根据方法生成缓存

@Cacheable(value="user-key")//value的值就是缓存到redis中的key

public User getUser() {

User user=userRepository.findByUserName("aa");

System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");

return user;

}

redis实现session共享

1、增加依赖:

org.springframework.session

spring-session-data-redis

2、Session配置:

@Configuration

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)

public class SessionConfig {

}

maxInactiveIntervalInSeconds: 设置Session失效时间,使用Redis Session之后,原Boot的server.session.timeout属性不再生效。

配置完成。此时获取session和增加session中的值,都将从redis中读取。

如果需要在两台或者多台中共享session,那么每一台服务器都要和上述一样,配置为:从redis中获取session和更新session到redis中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值