Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify which logical database to use with spring-data-redis

I'm using spring-data-redis, spring-session and Spring Boot to connect to my Redis instance. However I would like spring-data-redis to connect not to DB 0 (which is default) but to another local database (say DB 1). This is where I'd like the sessions to be stored. Is this possible with spring-data-redis?

like image 667
Johan Avatar asked Feb 04 '16 15:02

Johan


People also ask

Why do we use different databases numbers for Redis?

Using multiple databases in a single instance may be useful in the following scenario: Different copies of the same database could be used for production, development or testing using real-time data. People may use replica to clone a redis instance to achieve the same purpose.

What is Spring Redis database?

Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.

How do I find my Redis database name?

There is no command to do it (like you would do it with MySQL for instance). The number of Redis databases is fixed, and set in the configuration file. By default, you have 16 databases. Each database is identified by a number (not a name).


1 Answers

The ConnectionFactory used by RedisTemplate offers configuration options for setting a default DB. Depending on the Redis driver in use both JedisConnectionFactory as well as LettuceConnectionFactory offer void setDatabase(int index).

Using Spring Boot RedisProperties allows to set the default DB via setDatabase or by providing spring.redis.database.

like image 162
Christoph Strobl Avatar answered Oct 28 '22 04:10

Christoph Strobl