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
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) {
`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)
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:
`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:
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.
@@ -693,120 +695,3 @@ public class AnotherExample {
693
695
694
696
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).
695
697
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) {
`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)
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:
`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:
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.
0 commit comments