spring boot 自定义 redisTemplate

@Configuration
public class RedisTemplateConfig {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean(name = "redisCacheManager")
    public RedisCacheManager manager() {
        //设置json序列化配置
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        // 解决jackson2无法反序列化LocalDateTime的问题
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //加入写入锁
        RedisCacheWriter cacheWriter = RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory);

        //启动redis缓存的默认设置
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        //设置jdk序列化
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair
                .fromSerializer(jackson2JsonRedisSerializer));


        config.entryTtl(Duration.ofMinutes(10));
        return new RedisCacheManager(cacheWriter, config);

    }

    //配置自己的redisTemplate;
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        // 定义json 序列化工具
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(objectMapper);

        //定义 string 序列化工具
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashValueSerializer(serializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

好的,我可以回答你关于 Spring Boot 自定义 Session 的问题。 在 Spring Boot 中,Session 的默认实现是基于 Servlet 的 HttpSession 接口。如果你想要自定义 Session 实现,可以通过实现 Spring Session 的 Session 接口,并在配置文件中指定该实现来实现。 下面是一个使用 Redis 存储的自定义 Session 实现的示例代码: 首先,创建一个 RedisSession 类,实现 Session 接口,并实现相应的方法,如下所示: ```java public class RedisSession implements Session { private final String id; private Map<String, Object> attributes = new HashMap<>(); private boolean isNew; public RedisSession(String id) { this.id = id; this.isNew = true; } @Override public String getId() { return this.id; } @Override public <T> T getAttribute(String name) { return (T) this.attributes.get(name); } @Override public void setAttribute(String name, Object value) { this.attributes.put(name, value); this.isNew = false; } @Override public void removeAttribute(String name) { this.attributes.remove(name); } @Override public Instant getCreationTime() { throw new UnsupportedOperationException(); } @Override public void setLastAccessedTime(Instant lastAccessedTime) { throw new UnsupportedOperationException(); } @Override public Instant getLastAccessedTime() { throw new UnsupportedOperationException(); } @Override public void setMaxInactiveInterval(Duration interval) { throw new UnsupportedOperationException(); } @Override public Duration getMaxInactiveInterval() { throw new UnsupportedOperationException(); } @Override public boolean isExpired() { throw new UnsupportedOperationException(); } public boolean isNew() { return this.isNew; } } ``` 然后,创建一个 RedisSessionRepository 类,实现 SessionRepository 接口,并实现相应的方法,如下所示: ```java public class RedisSessionRepository implements SessionRepository<RedisSession> { private final RedisTemplate<String, RedisSession> redisTemplate; public RedisSessionRepository(RedisTemplate<String, RedisSession> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public RedisSession createSession() { String id = UUID.randomUUID().toString(); RedisSession session = new RedisSession(id); this.redisTemplate.opsForValue().set(id, session); return session; } @Override public void save(RedisSession session) { this.redisTemplate.opsForValue().set(session.getId(), session); } @Override public RedisSession findById(String id) { return this.redisTemplate.opsForValue().get(id); } @Override public void deleteById(String id) { this.redisTemplate.delete(id); } } ``` 最后,在 Spring Boot 的配置文件中指定使用 RedisSessionRepository 实现 SessionRepository,如下所示: ```properties spring.session.store-type=redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=10000 spring.session.redis.flush-mode=on_save spring.session.redis.namespace=spring:session spring.session.repository-type=redis ``` 以上就是关于 Spring Boot 自定义 Session 的步骤,希望能帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值