Getting Started

建立工作环境的简便方法是通过 start.spring.io 创建一个基于 Spring 的项目,或在 Spring Tools 中创建一个 Spring 项目。

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.

Examples Repository

GitHub spring-data-examples repository 托管了多个示例,您可以下载和试用它们来了解库的工作原理。

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.

Hello World

首先,您需要设置一个正在运行的 Redis 服务器。Spring Data Redis 需要 Redis 2.6 或更高版本,并且 Spring Data Redis 与 LettuceJedis 集成,这两个是用于 Redis 的流行开源 Java 库。

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.

现在,你可以创建一个简单的 Java 应用程序,用于将值存储到 Redis 中并从 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

  • Reactive

Unresolved include directive in modules/ROOT/pages/redis/getting-started.adoc - include::example$examples/RedisApplication.java[]
Unresolved include directive in modules/ROOT/pages/redis/getting-started.adoc - include::example$examples/ReactiveRedisApplication.java[]

即使在这么简单的例子中,也有一些值得注意的事情:

Even in this simple example, there are a few notable things to point out:

  • You can create an instance of RedisTemplate (or ReactiveRedisTemplate for reactive usage) with a RedisConnectionFactory. 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.