Search code examples
javaspring-bootredisspring-data-redis

How to set redis databse number to be used in spring boot


In my application and I want all my keys to go to a specific database number.

I checked Redis documentation and found that Redis has 16 databases by default 0-15.

Let's say I want to use database 3.

How can I do it in java spring-boot application?


Solution

  • You can specify that in the application.properties file like this.

    spring.redis.database=3
    

    after this while creating Jedisconnection factory set this databse to jedisconnectionfactory.

        @Autowired
        private RedisProperties redisProperties;
    
    
        @Bean
        JedisConnectionFactory jedisConnectionFactory() {
    
            JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration);
            jedisConnectionFactory.setDatabase(this.redisProperties.getDatabase());
            return jedisConnectionFactory;
        }