An easy way to bootstrap setting up a working environment is to create a Spring-based project via start.spring.io or create a Spring project in Spring Tools.
The GitHub spring-data-examples repository hosts several examples that you can download and play around with to get a feel for how the library works.
First, you need to set up a running Redis server. Spring Data Redis requires Redis 2.6 or above and Spring Data Redis integrates with Lettuce and Jedis, two popular open-source Java libraries for Redis.
Now you can create a simple Java application that stores and reads a value to and from Redis.
Create the main application to run, as the following example shows:
- Imperative
-
link:example$examples/RedisApplication.java[role=include]
- Reactive
-
link:example$examples/ReactiveRedisApplication.java[role=include]
Even in this simple example, there are a few notable things to point out:
-
You can create an instance of
RedisTemplate
(orReactiveRedisTemplate
for reactive usage) with aRedisConnectionFactory
. Connection factories are an abstraction on top of the supported drivers. -
There’s no single way to use Redis as it comes with support for a wide range of data structures such as plain keys ("strings"), lists, sets, sorted sets, streams, hashes and so on.