You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We ship converters for JSR-310 types (LocalDate/Time, ZonedDateTime, Period, Duration and ZoneId) to map between UTF-8-encoded byte[] and JDK 8 date/time types.
We also export Redis Repositories in a CDI environment. Repositories can be injected using @Inject. The CDI extension requires at least RedisOperations to be provided. Other beans like RedisKeyValueAdapter and RedisKeyValueTemplate can be provided by the user. If no RedisKeyValueAdapter/RedisKeyValueTemplate beans are found, the CDI extension creates own managed instances.
Original Pull Request: #156
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/redis-repositories.adoc
+91-10Lines changed: 91 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ To access domain entities stored in a Redis you can leverage repository support
14
14
====
15
15
[source,java]
16
16
----
17
-
@RedisHash("persons");
17
+
@RedisHash("persons")
18
18
public class Person {
19
19
20
20
@Id String id;
@@ -158,7 +158,7 @@ of Complex Type
158
158
addresses.[work].city = "...
159
159
|===
160
160
161
-
Mapping behavior can be customized by registering the according `Converter` in `CustomConversions`. Those converters can take care of converting from/to a single `byte[]` as well as `Map<String,byte[]>` whereas the first one is suitable for eg. converting one complex type to eg. a binary JSON representation that still uses the default mappings hash structure, whereas the second options offers full control over the resulting hash.
161
+
Mapping behavior can be customized by registering the according `Converter` in `CustomConversions`. Those converters can take care of converting from/to a single `byte[]` as well as `Map<String,byte[]>` whereas the first one is suitable for eg. converting one complex type to eg. a binary JSON representation that still uses the default mappings hash structure. The second option offers full control over the resulting hash. Writing objects to a Redis hash will delete the content from the hash and re-create the whole hash, so not mapped data will be lost.
162
162
163
163
.Sample byte[] Converters
164
164
====
@@ -308,15 +308,15 @@ public class ApplicationConfig {
308
308
309
309
[[redis.repositories.indexes]]
310
310
== Secondary Indexes
311
-
http://redis.io/topics/indexes[Secondary indexes] are used to enable lookup operations based on native redis structures. Values are written to the according indexes on every save and are removed when objects are deleted or <<redis.repositories.expirations,expire>>.
311
+
http://redis.io/topics/indexes[Secondary indexes] are used to enable lookup operations based on native Redis structures. Values are written to the according indexes on every save and are removed when objects are deleted or <<redis.repositories.expirations,expire>>.
312
312
313
313
Given the sample `Person` entity we can create an index for _firstname_ by annotating the property with `@Indexed`.
314
314
315
315
.Annotation driven indexing
316
316
====
317
317
[source,java]
318
318
----
319
-
@RedisHash("persons");
319
+
@RedisHash("persons")
320
320
public class Person {
321
321
322
322
@Id String id;
@@ -351,7 +351,7 @@ Further more the programmatic setup allows to define indexes on map keys and lis
351
351
====
352
352
[source,java]
353
353
----
354
-
@RedisHash("persons");
354
+
@RedisHash("persons")
355
355
public class Person {
356
356
357
357
// ... other properties omitted
@@ -457,16 +457,19 @@ public class TimeToLiveOnMethod {
457
457
The repository implementation ensures subscription to http://redis.io/topics/notifications[Redis keyspace notifications] via `RedisMessageListenerContainer`.
458
458
459
459
When the expiration is set to a positive value the according `EXPIRE` command is executed.
460
-
Additionally to persisting the original, a _phantom_ copy is persisted in Redis and set to expire 5 minutes after the original one. This done to enable the Repository support to publish `RedisKeyExpiredEvent` holding the expired value via Springs `ApplicationEventPublisher` whenever a key expires even though the original values have already been gone.
460
+
Additionally to persisting the original, a _phantom_ copy is persisted in Redis and set to expire 5 minutes after the original one. This is done to enable the Repository support to publish `RedisKeyExpiredEvent` holding the expired value via Springs `ApplicationEventPublisher` whenever a key expires even though the original values have already been gone. Expiry events
461
+
will be received on all connected applications using Spring Data Redis repositories.
461
462
462
463
The `RedisKeyExpiredEvent` will hold a copy of the actually expired domain object as well as the key.
463
464
464
-
NOTE: The keyspace notification message listener will alter `notify-keyspace-events` settings in Redis if those are not already set. Existing settings will not be overridden so it is left to the user to set those up correctly when not leaving them empty.
465
+
NOTE: The keyspace notification message listener will alter `notify-keyspace-events` settings in Redis if those are not already set. Existing settings will not be overridden, so it is left to the user to set those up correctly when not leaving them empty.
466
+
467
+
NOTE: Redis Pub/Sub messages are not persistent. If a key expires while the application is down the expiry event will not be processed which may lead to secondary indexes containing still references to the expired object.
465
468
466
469
[[redis.repositories.references]]
467
470
== Persisting References
468
-
Marking properties with `@Reference` allows to store a simple key reference instead of copying the all values into the hash itself.
469
-
On loading from Redis references are resolved automatically and mapped back into the object.
471
+
Marking properties with `@Reference` allows storing a simple key reference instead of copying values into the hash itself.
472
+
On loading from Redis, references are resolved automatically and mapped back into the object.
NOTE: Please make sure properties used in finder methods are set up for indexing.
503
507
504
-
Using derived finder methods might not always be sufficient to model the queries to execute. `RedisCallback` offers more control over the actual matching of index structures or even customly added ones. All it takes is providing a `RedisCallback` that returns a single or `Iterable` set of _id_ values.
508
+
NOTE: Query methods for Redis repositories support only queries for entities and collections of entities with paging.
509
+
510
+
Using derived query methods might not always be sufficient to model the queries to execute. `RedisCallback` offers more control over the actual matching of index structures or even custom added ones. All it takes is providing a `RedisCallback` that returns a single or `Iterable` set of _id_ values.
Instances of the repository interfaces are usually created by a container, which Spring is the most natural choice when working with Spring Data. There's sophisticated support to easily set up Spring to create bean instances. Spring Data Redis ships with a custom CDI extension that allows using the repository abstraction in CDI environments. The extension is part of the JAR so all you need to do to activate it is dropping the Spring Data Redis JAR into your classpath.
544
+
545
+
You can now set up the infrastructure by implementing a CDI Producer for the `RedisConnectionFactory` and `RedisOperations`:
546
+
547
+
[source, java]
548
+
----
549
+
class RedisOperationsProducer {
550
+
551
+
552
+
@Produces
553
+
RedisConnectionFactory redisConnectionFactory() {
554
+
555
+
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
The necessary setup can vary depending on the JavaEE environment you run in.
585
+
586
+
The Spring Data Redis CDI extension will pick up all Repositories available as CDI beans and create a proxy for a Spring Data repository whenever a bean of a repository type is requested by the container. Thus obtaining an instance of a Spring Data repository is a matter of declaring an `@Injected` property:
587
+
588
+
[source, java]
589
+
----
590
+
class RepositoryClient {
591
+
592
+
@Inject
593
+
PersonRepository repository;
594
+
595
+
public void businessMethod() {
596
+
List<Person> people = repository.findAll();
597
+
}
598
+
}
599
+
----
600
+
601
+
A Redis Repository requires `RedisKeyValueAdapter` and `RedisKeyValueTemplate` instances. These beans are created and managed by the Spring Data CDI extension if no provided beans are found. You can however supply your own beans to configure the specific properties of `RedisKeyValueAdapter` and `RedisKeyValueTemplate`.
0 commit comments