sql >> Database teknologi >  >> NoSQL >> Redis

Sådan isoleres spring boot app redis og spring boot session global redis

Som standard er både spring-session og spring-cache faktisk konfigureret af spring-boot med en RedisConnectionFactory bean navngivet som connectionFactory .To måder at gøre dette på.

  1. lav spring-session at bruge en anden connectionFactory bean-instans, og lad spring-cache være for at bruge standard connectionFactory . Og nedenfor er en prøveløsning:

    @Configuration
    public class RedisHttpSessionConfig {
    
        @Bean
        StringRedisSerializer stringRedisSerializer() {
            return new StringRedisSerializer();
        }
    
        @Bean
        RedisConnectionFactory redisHttpSessionConnectionFactory() {
            RedisConnectionFactory redisHttpSessionConnectionFactory = null;
            // ... add your codes here
            return redisHttpSessionConnectionFactory;
        }
    
        @Bean
        public RedisTemplate<Object, Object> sessionRedisTemplate(
                RedisConnectionFactory redisHttpSessionConnectionFactory) {
            RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
            template.setKeySerializer(new StringRedisSerializer());
            template.setHashKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            template.setDefaultSerializer(GenericJackson2JsonRedisSerializer());
            template.setConnectionFactory(redisHttpSessionConnectionFactory);
            return template;
        }
    }
    
  2. lav spring-cache at bruge en anden connectionFactory bean-instans, og forlad spring-session for at bruge standard connectionFactory . Og nedenfor er en prøveløsning:

    @Configuration
    public class RedisCacheConfig {
    
        @Bean
        StringRedisSerializer stringRedisSerializer() {
            return new StringRedisSerializer();
        }
    
        @Bean
        RedisConnectionFactory redisCacheConnectionFactory() {
             RedisConnectionFactory redisCacheConnectionFactory = null;
             // ... add your codes here
             return redisCacheConnectionFactory;
        }
    
        @Bean
        RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisCacheConnectionFactory) {
            RedisTemplate<Object, Object> redisTemplate = new RedisTemplate();
            redisTemplate.setConnectionFactory(redisCacheConnectionFactory);
            redisTemplate.setKeySerializer(this.stringRedisSerializer());
            redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return redisTemplate;
        }
    
        @Bean(name = "stringRedisTemplate")
        public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisCacheConnectionFactory) throws UnknownHostException {
            StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
            stringRedisTemplate.setConnectionFactory(redisCacheConnectionFactory);
            stringRedisTemplate.setKeySerializer(this.stringRedisSerializer());
            stringRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return stringRedisTemplate;
        }
    
        @Bean
        CacheManager cacheManager(RedisTemplate redisTemplate) {
            RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
            cacheManager.setDefaultExpiration(600l);
            cacheManager.setUsePrefix(true);
            return cacheManager;
        }
    }
    


  1. Mongodb Explain for Aggregation framework

  2. Delte sessioner mellem node-apps?

  3. Automatisk MongoDB Backup

  4. Sådan opretter du forbindelse til ElastiCache-klyngen ved hjælp af node.js