This article covers basic information about Spring Data Redis which allows accessing Redis from Spring applications.
Redis is an in-memory data structure that could be used as a fast database, cache, and message broker. Redis supports several types of data structures such as strings, hashes, lists, sets, bitmaps, geospatial indexes, and streams.
Spring Data provides low-level and high-level abstractions for interacting with Redis. We could use CRUD operations and also complex queries using RedisTemplate.
2. Maven Dependencies
To start using Redis Spring Data we need to add dedicated dependencies into our project.
We can use two different approaches in order to configure Spring Data Redis:
first one is to use a common spring-data-redis library with one of Redis client for example jedis:
the second approach uses Spring Boot Starter dependency for Redis:
You could find the latest version of these libraries in our Maven repository:
The easiest way to start a Redis instance is to use an official Docker image:
This command will start secured Redis (you will need to provide a password to connect to it) on port 96379.
4. Redis Configuration
4.1. Spring Boot starter for Redis
When the spring-boot-starter-data-redis is used Spring Boot will automatically configure RedisConnectionFactory we just need to specify a few properties in our application.properties file:
Used properties:
database - database index used by the connection factory,
host - location of Redis,
port - the port on which the server is listening,
password - password when Redis is secured,
timeout - Redis connection timeout.
4.2. Using Java Configuration
In a second approach we will explicitly provide the parameters to connect with Redis:
This configuration class will be used when spring-data-redis with jedis are used in the project.
5. Spring Data Redis CRUD operations
Let's start with a simple object that will be stored in the Redis database:
The RedisHash annotations tell Redis to use a hash data structure for our entity object.
Next, we will need a repository to interact with Redis:
5.1. Save new object in Redis
Let's start with saving new UserSession in Redis:
5.2. Get a saved object from Redis using an identifier
We could use findById method to fetch specific object from Redis:
5.3. Update existing object
To update object we use the same method that save new instance save(...):
5.4. Delete existing object
We have several methods to delete objects from Redis:
deleteById(...) - delete specified object by the indetifier,
deleteAll() - delete all objects related with repository,
delete(...) - delete object used as an argument to this method.
Below test will remove specified object by its identifier:
5.5. Finding objects
We can use findAll() method to retrieve all UserSession objects.
Note that this method returns Iterable interface:
6. Conclusion
In this article, we presented basic information about Spring Data Redis.
As usual, the source code used in this tutorial could be found in a GitHub project.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}