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?
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.
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.
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).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With