Skip to content

Commit 1a290c3

Browse files
mp911dechristophstrobl
authored andcommitted
Revise RedisCache documentation.
Original Pull Request: #2051
1 parent 0dc89b8 commit 1a290c3

File tree

4 files changed

+134
-119
lines changed

4 files changed

+134
-119
lines changed

Diff for: src/main/asciidoc/reference/redis-cache.adoc

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
[[redis:support:cache-abstraction]]
2+
== Redis Cache
3+
4+
NOTE: Changed in 2.0
5+
6+
Spring Redis provides an implementation for the Spring https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/integration.html#cache[cache abstraction] through the `org.springframework.data.redis.cache` package. To use Redis as a backing implementation, add `RedisCacheManager` to your configuration, as follows:
7+
8+
[source,java]
9+
----
10+
@Bean
11+
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
12+
return RedisCacheManager.create(connectionFactory);
13+
}
14+
----
15+
16+
`RedisCacheManager` behavior can be configured with `RedisCacheManagerBuilder`, letting you set the default `RedisCacheConfiguration`, transaction behavior, and predefined caches.
17+
18+
[source,java]
19+
----
20+
RedisCacheManager cm = RedisCacheManager.builder(connectionFactory)
21+
.cacheDefaults(defaultCacheConfig())
22+
.withInitialCacheConfigurations(singletonMap("predefined", defaultCacheConfig().disableCachingNullValues()))
23+
.transactionAware()
24+
.build();
25+
----
26+
27+
As shown in the preceding example, `RedisCacheManager` allows definition of configurations on a per-cache basis.
28+
29+
The behavior of `RedisCache` created with `RedisCacheManager` is defined with `RedisCacheConfiguration`. The configuration lets you set key expiration times, prefixes, and ``RedisSerializer`` implementations for converting to and from the binary storage format, as shown in the following example:
30+
31+
[source,java]
32+
----
33+
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
34+
.entryTtl(Duration.ofSeconds(1))
35+
.disableCachingNullValues();
36+
----
37+
38+
`RedisCacheManager` defaults to a lock-free `RedisCacheWriter` for reading and writing binary values. Lock-free caching improves throughput. The lack of entry locking can lead to overlapping, non-atomic commands for the `putIfAbsent` and `clean` methods, as those require multiple commands to be sent to Redis. The locking counterpart prevents command overlap by setting an explicit lock key and checking against presence of this key, which leads to additional requests and potential command wait times.
39+
40+
It is possible to opt in to the locking behavior as follows:
41+
42+
[source,java]
43+
----
44+
RedisCacheManager cm = RedisCacheManager.build(RedisCacheWriter.lockingRedisCacheWriter(connectionFactory))
45+
.cacheDefaults(defaultCacheConfig())
46+
...
47+
----
48+
49+
By default, any `key` for a cache entry gets prefixed with the actual cache name followed by two colons.
50+
This behavior can be changed to a static as well as a computed prefix.
51+
52+
The following example shows how to set a static prefix:
53+
54+
[source,java]
55+
----
56+
// static key prefix
57+
RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith("( ͡° ᴥ ͡°)");
58+
59+
The following example shows how to set a computed prefix:
60+
61+
// computed key prefix
62+
RedisCacheConfiguration.defaultCacheConfig().computePrefixWith(cacheName -> "¯\_(ツ)_/¯" + cacheName);
63+
----
64+
65+
The cache implementation defaults to use `KEYS` and `DEL` to clear the cache. `KEYS` can cause performance issues with large keyspaces. Therefore, the default `RedisCacheWriter` can be created with a `BatchStrategy` to switch to a `SCAN`-based batch strategy. The `SCAN` strategy requires a batch size to avoid excessive Redis command roundtrips:
66+
67+
[source,java]
68+
----
69+
RedisCacheManager cm = RedisCacheManager.build(RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory, BatchStrategy.scan(1000)))
70+
.cacheDefaults(defaultCacheConfig())
71+
...
72+
----
73+
74+
NOTE: The `KEYS` batch strategy is fully supported using any driver and Redis operation mode (Standalone, Clustered). `SCAN` is fully supported when using the Lettuce driver. Jedis supports `SCAN` only in non-clustered modes.
75+
76+
The following table lists the default settings for `RedisCacheManager`:
77+
78+
.`RedisCacheManager` defaults
79+
[width="80%",cols="<1,<2",options="header"]
80+
|====
81+
|Setting
82+
|Value
83+
84+
|Cache Writer
85+
|Non-locking, `KEYS` batch strategy
86+
87+
|Cache Configuration
88+
|`RedisCacheConfiguration#defaultConfiguration`
89+
90+
|Initial Caches
91+
|None
92+
93+
|Transaction Aware
94+
|No
95+
|====
96+
97+
The following table lists the default settings for `RedisCacheConfiguration`:
98+
99+
.RedisCacheConfiguration defaults
100+
[width="80%",cols="<1,<2",options="header"]
101+
|====
102+
|Key Expiration
103+
|None
104+
105+
|Cache `null`
106+
|Yes
107+
108+
|Prefix Keys
109+
|Yes
110+
111+
|Default Prefix
112+
|The actual cache name
113+
114+
|Key Serializer
115+
|`StringRedisSerializer`
116+
117+
|Value Serializer
118+
|`JdkSerializationRedisSerializer`
119+
120+
|Conversion Service
121+
|`DefaultFormattingConversionService` with default cache key converters
122+
|====
123+
124+
[NOTE]
125+
====
126+
By default `RedisCache`, statistics are disabled.
127+
Use `RedisCacheManagerBuilder.enableStatistics()` to collect local _hits_ and _misses_ through `RedisCache#getStatistics()`, returning a snapshot of the collected data.
128+
====

Diff for: src/main/asciidoc/reference/redis.adoc

+2-117
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ include::{referenceDir}/pipelining.adoc[]
652652

653653
include::{referenceDir}/redis-scripting.adoc[]
654654

655+
include::{referenceDir}/redis-cache.adoc[]
656+
655657
:leveloffset: 1
656658
[[redis:support]]
657659
== Support Classes
@@ -693,120 +695,3 @@ public class AnotherExample {
693695

694696
As shown in the preceding example, the consuming code is decoupled from the actual storage implementation. In fact, there is no indication that Redis is used underneath. This makes moving from development to production environments transparent and highly increases testability (the Redis implementation can be replaced with an in-memory one).
695697

696-
[[redis:support:cache-abstraction]]
697-
=== Support for the Spring Cache Abstraction
698-
699-
NOTE: Changed in 2.0
700-
701-
Spring Redis provides an implementation for the Spring https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/integration.html#cache[cache abstraction] through the `org.springframework.data.redis.cache` package. To use Redis as a backing implementation, add `RedisCacheManager` to your configuration, as follows:
702-
703-
[source,java]
704-
----
705-
@Bean
706-
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
707-
return RedisCacheManager.create(connectionFactory);
708-
}
709-
----
710-
711-
`RedisCacheManager` behavior can be configured with `RedisCacheManagerBuilder`, letting you set the default `RedisCacheConfiguration`, transaction behavior, and predefined caches.
712-
713-
[source,java]
714-
----
715-
RedisCacheManager cm = RedisCacheManager.builder(connectionFactory)
716-
.cacheDefaults(defaultCacheConfig())
717-
.withInitialCacheConfigurations(singletonMap("predefined", defaultCacheConfig().disableCachingNullValues()))
718-
.transactionAware()
719-
.build();
720-
----
721-
722-
As shown in the preceding example, `RedisCacheManager` allows definition of configurations on a per-cache basis.
723-
724-
The behavior of `RedisCache` created with `RedisCacheManager` is defined with `RedisCacheConfiguration`. The configuration lets you set key expiration times, prefixes, and ``RedisSerializer`` implementations for converting to and from the binary storage format, as shown in the following example:
725-
726-
[source,java]
727-
----
728-
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
729-
.entryTtl(Duration.ofSeconds(1))
730-
.disableCachingNullValues();
731-
----
732-
733-
`RedisCacheManager` defaults to a lock-free `RedisCacheWriter` for reading and writing binary values. Lock-free caching improves throughput. The lack of entry locking can lead to overlapping, non-atomic commands for the `putIfAbsent` and `clean` methods, as those require multiple commands to be sent to Redis. The locking counterpart prevents command overlap by setting an explicit lock key and checking against presence of this key, which leads to additional requests and potential command wait times.
734-
735-
It is possible to opt in to the locking behavior as follows:
736-
737-
[source,java]
738-
----
739-
RedisCacheManager cm = RedisCacheManager.build(RedisCacheWriter.lockingRedisCacheWriter())
740-
.cacheDefaults(defaultCacheConfig())
741-
...
742-
----
743-
744-
By default, any `key` for a cache entry gets prefixed with the actual cache name followed by two colons.
745-
This behavior can be changed to a static as well as a computed prefix.
746-
747-
The following example shows how to set a static prefix:
748-
749-
[source,java]
750-
----
751-
// static key prefix
752-
RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith("( ͡° ᴥ ͡°)");
753-
754-
The following example shows how to set a computed prefix:
755-
756-
// computed key prefix
757-
RedisCacheConfiguration.defaultCacheConfig().computePrefixWith(cacheName -> "¯\_(ツ)_/¯" + cacheName);
758-
----
759-
760-
The following table lists the default settings for `RedisCacheManager`:
761-
762-
.`RedisCacheManager` defaults
763-
[width="80%",cols="<1,<2",options="header"]
764-
|====
765-
|Setting
766-
|Value
767-
768-
|Cache Writer
769-
|Non-locking
770-
771-
|Cache Configuration
772-
|`RedisCacheConfiguration#defaultConfiguration`
773-
774-
|Initial Caches
775-
|None
776-
777-
|Transaction Aware
778-
|No
779-
|====
780-
781-
The following table lists the default settings for `RedisCacheConfiguration`:
782-
783-
.RedisCacheConfiguration defaults
784-
[width="80%",cols="<1,<2",options="header"]
785-
|====
786-
|Key Expiration
787-
|None
788-
789-
|Cache `null`
790-
|Yes
791-
792-
|Prefix Keys
793-
|Yes
794-
795-
|Default Prefix
796-
|The actual cache name
797-
798-
|Key Serializer
799-
|`StringRedisSerializer`
800-
801-
|Value Serializer
802-
|`JdkSerializationRedisSerializer`
803-
804-
|Conversion Service
805-
|`DefaultFormattingConversionService` with default cache key converters
806-
|====
807-
808-
[NOTE]
809-
====
810-
By default `RedisCache`, statistics are disabled.
811-
Use `RedisCacheManagerBuilder.enableStatistics()` to collect local _hits_ and _misses_ through `RedisCache#getStatistics()`, returning a snapshot of the collected data.
812-
====

Diff for: src/main/java/org/springframework/data/redis/cache/BatchStrategy.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public abstract class BatchStrategy {
3939

4040
/**
4141
* Batching strategy using a single {@code KEYS} and {@code DEL} command to remove all matching keys. {@code KEYS}
42-
* scans the entire keyspace of the Redis database and can block the Redis worker thread for a long time when the
43-
* keyspace has a significant size.
42+
* scans the entire keyspace of the Redis database and can block the Redis worker thread for a long time on large
43+
* keyspaces.
4444
* <p/>
4545
* {@code KEYS} is supported for standalone and clustered (sharded) Redis operation modes.
4646
*

Diff for: src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java

+2
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d
169169
* <dl>
170170
* <dt>locking</dt>
171171
* <dd>disabled</dd>
172+
* <dt>batch strategy</dt>
173+
* <dd>{@link BatchStrategy#keys() KEYS}</dd>
172174
* <dt>cache configuration</dt>
173175
* <dd>{@link RedisCacheConfiguration#defaultCacheConfig()}</dd>
174176
* <dt>initial caches</dt>

0 commit comments

Comments
 (0)