Skip to content

Commit 1d528f0

Browse files
DATAREDIS-619 - Polishing.
Add dedicated section on executing lua scripts via the reactive infrastructure, rephrase some other parts adapt non-Javadoc comments to code style. Original Pull Request: #280
1 parent e22d22a commit 1d528f0

File tree

2 files changed

+90
-44
lines changed

2 files changed

+90
-44
lines changed

src/main/asciidoc/reference/reactive-redis.adoc

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This section covers reactive Redis support and how to get started. You will find
77
[[redis:reactive:requirements]]
88
== Redis Requirements
99

10-
Spring Redis requires Redis 2.6 or above and Java SE 8.0 or above. In terms of language bindings (or connectors), Spring Data Redis integrates with http://github.com/lettuce-io/lettuce-core[Lettuce] as the only reactive Java connector. Spring Data Redis uses https://projectreactor.io/[Project Reactor] as reactive composition library.
10+
Spring Data Redis requires Redis 2.6 or above and Java SE 8.0 or above. In terms of language bindings (or connectors), Spring Data Redis currently integrates with http://github.com/lettuce-io/lettuce-core[Lettuce] as the only reactive Java connector. https://projectreactor.io/[Project Reactor] is used as reactive composition library.
1111

1212
[[redis:reactive:connectors]]
1313
== Connecting to Redis using a reactive driver
@@ -17,13 +17,13 @@ One of the first tasks when using Redis and Spring is to connect to the store th
1717
[[redis:reactive:connectors:operation-modes]]
1818
=== Redis Operation Modes
1919

20-
Redis can be run as Standalone server, with <<redis:sentinel,Redis Sentinel>> or in <<cluster,Redis Cluster>> mode.
20+
Redis can be run as standalone server, with <<redis:sentinel,Redis Sentinel>> or in <<cluster,Redis Cluster>> mode.
2121
http://github.com/lettuce-io/lettuce-core[Lettuce] supports all above mentioned connection types.
2222

2323
[[redis:reactive:connectors:connection]]
2424
=== ReactiveRedisConnection and ReactiveRedisConnectionFactory
2525

26-
`ReactiveRedisConnection` provides the building block for Redis communication as it handles the communication with the Redis back-end. It also automatically translates the underlying connecting library exceptions to Spring's consistent DAO exception http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/data-access.html#dao-exceptions[hierarchy] so one can switch the connectors without any code changes as the operation semantics remain the same.
26+
`ReactiveRedisConnection` provides the building block for Redis communication as it handles the communication with the Redis back-end. It also automatically translates the underlying driver exceptions to Spring's consistent DAO exception http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/data-access.html#dao-exceptions[hierarchy] so one can switch the connectors without any code changes as the operation semantics remain the same.
2727

2828
Active ``ReactiveRedisConnection``s are created through `ReactiveRedisConnectionFactory`. In addition, the factories act as ``PersistenceExceptionTranslator``s, meaning once declared, they allow one to do transparent exception translation. For example, exception translation through the use of the `@Repository` annotation and AOP. For more information see the dedicated http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/data-access.html#orm-exception-translation[section] in Spring Framework documentation.
2929

@@ -34,45 +34,43 @@ The easiest way to work with a `ReactiveRedisConnectionFactory` is to configure
3434
[[redis:reactive:connectors:lettuce]]
3535
=== Configuring Lettuce connector
3636

37-
https://github.com/lettuce-io/lettuce-core[Lettuce] is a http://netty.io/[netty]-based open-source connector supported by Spring Data Redis through the `org.springframework.data.redis.connection.lettuce` package.
37+
https://github.com/lettuce-io/lettuce-core[Lettuce] is supported by Spring Data Redis through the `org.springframework.data.redis.connection.lettuce` package.
3838

39-
Its configuration is probably easy to guess:
39+
Setting up `ReactiveRedisConnectionFactory` for Lettuce can be done as follows:
4040

4141
[source,java]
4242
----
4343
@Bean
44-
public ReactiveRedisConnectionFactory lettuceConnectionFactory() {
44+
public ReactiveRedisConnectionFactory connectionFactory() {
4545
return new LettuceConnectionFactory("localhost", 6379);
4646
}
4747
----
4848

49-
A more sophisticated configuration could look like:
49+
A more sophisticated configuration, including SSL and timeouts, using `LettuceClientConfigurationBuilder` might look like below:
5050

5151
[source,java]
5252
----
5353
@Bean
5454
public ReactiveRedisConnectionFactory lettuceConnectionFactory() {
5555
56-
RedisStandaloneConfiguration standalone = new RedisStandaloneConfiguration("localhost", 6379);
57-
5856
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
5957
.useSsl().and()
6058
.commandTimeout(Duration.ofSeconds(2))
6159
.shutdownTimeout(Duration.ZERO)
6260
.build();
6361
64-
return new LettuceConnectionFactory(standalone, clientConfig);
62+
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379), clientConfig);
6563
}
6664
----
6765

68-
There are also a few Lettuce-specific connection parameters that can be tweaked. See `LettuceClientConfiguration` for more details.
66+
For more detailed client configuration tweaks have a look at `LettuceClientConfiguration`.
6967

7068
[[redis:reactive:template]]
7169
== Working with Objects through ReactiveRedisTemplate
7270

7371
Most users are likely to use `ReactiveRedisTemplate` and its corresponding package `org.springframework.data.redis.core` - the template is in fact the central class of the Redis module due to its rich feature set. The template offers a high-level abstraction for Redis interactions. While `ReactiveRedisConnection` offers low level methods that accept and return binary values (`ByteBuffer`), the template takes care of serialization and connection management, freeing the user from dealing with such details.
7472

75-
Moreover, the template provides operations views (following the grouping from Redis command http://redis.io/commands[reference]) that offer rich, generified interfaces for working against a certain type as described below:
73+
Moreover, the template provides operation views (following the grouping from Redis command http://redis.io/commands[reference]) that offer rich, generified interfaces for working against a certain type as described below:
7674

7775
.Operational views
7876
[width="80%",cols="<1,<2",options="header"]
@@ -106,7 +104,7 @@ Moreover, the template provides operations views (following the grouping from Re
106104

107105
Once configured, the template is thread-safe and can be reused across multiple instances.
108106

109-
Out of the box, `ReactiveRedisTemplate` uses a Java-based serializer for most of its operations. This means that any object written or read by the template will be serialized/deserialized through Java throug `RedisElementWriter` respective `RedisElementReader`. The serialization context is passed to the template upon construction, and the Redis module offers several implementations available in the `org.springframework.data.redis.serializer` package - see <<redis:serializer>> for more information.
107+
Out of the box, `ReactiveRedisTemplate` uses a Java-based serializer for most of its operations. This means that any object written or read by the template will be serialized/deserialized through `RedisElementWriter` respective `RedisElementReader`. The serialization context is passed to the template upon construction, and the Redis module offers several implementations available in the `org.springframework.data.redis.serializer` package - see <<redis:serializer>> for more information.
110108

111109
[source,java]
112110
----
@@ -124,15 +122,36 @@ class RedisConfiguration {
124122
----
125123
public class Example {
126124
127-
// inject the actual template
128125
@Autowired
129126
private ReactiveRedisTemplate<String, String> template;
130127
131-
132128
public Mono<Long> addLink(String userId, URL url) {
133129
return template.opsForList().leftPush(userId, url.toExternalForm());
134130
}
135131
}
136132
----
137133

134+
== Reactive Scripting
135+
136+
Executing Redis scripts via the reactive infrastructure can be done using the `ReactiveScriptExecutor` accessed best via `ReactiveRedisTemplate`.
137+
138+
[source,java]
139+
----
140+
public class Example {
141+
142+
@Autowired
143+
private ReactiveRedisTemplate<String, String> template;
144+
145+
public Flux<Long> theAnswerToLife() {
146+
147+
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
148+
script.setLocation(new ClassPathResource("META-INF/scripts/42.lua"));
149+
script.setResultType(Long.class);
150+
151+
return reactiveTemplate.execute(script);
152+
}
153+
}
154+
----
155+
156+
Please refer to the <<scripting,scripting section>> for more details on scripting commands.
138157

0 commit comments

Comments
 (0)