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
RedisCache now operates directly with RedisConnectionFactory allowing each Cache to use its own SerializationPair for cache key and value conversion.
A RedisCacheManager with default settings is created with RedisCacheManager.create(connectionFactory). Use RedisCacheManager.builder(…) to obtain a builder to customize cache settings.
RedisCacheManager cm = RedisCacheManager.builder(connectionFactory)
.cacheDefaults(defaultCacheConfig())
.initialCacheConfigurations(singletonMap("predefined", defaultCacheConfig().disableCachingNullValues()))
.transactionAware()
.build();
The behavior of a single RedisCache is defined via its RedisCacheConfiguration. Starting from defaultCacheConfig() the configuration can be altered as needed.
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(1))
.disableCachingNullValues();
RedisCacheManager defaults to a lock-free RedisCacheWriter for reading & writing binary values. Lock-free caching improves throughput. The lack of entry locking can lead to overlapping, non atomic commands, for putIfAbsent and clean methods as those require multiple commands 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.
Original pull request: #252.
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/redis.adoc
+76-16
Original file line number
Diff line number
Diff line change
@@ -465,30 +465,90 @@ As shown in the example above, the consuming code is decoupled from the actual s
465
465
[[redis:support:cache-abstraction]]
466
466
=== Support for Spring Cache Abstraction
467
467
468
+
NOTE: Changed in 2.0
469
+
468
470
Spring Redis provides an implementation for Spring http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html[cache abstraction] through the `org.springframework.data.redis.cache` package. To use Redis as a backing implementation, simply add `RedisCacheManager` to your configuration:
469
471
470
-
[source,xml]
472
+
[source,java]
473
+
----
474
+
@Bean
475
+
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
`RedisCacheManager` behavior can be configured via `RedisCacheManagerConfigurator` allowing to set the default `RedisCacheConfiguration`, transaction behaviour and predefined caches.
Behavior of `RedisCache` created via `RedisCacheManager` is defined via `RedisCacheConfiguration`. The configuration allows to set key expiration times, prefixes and ``RedisSerializer``s for converting to and from the binary storage format.
492
+
As shown above `RedisCacheManager` allows definition of configurations on a per cache base.
Using default `RedisCacheManager` uses a non locking `RedisCacheWriter` for reading & writing bits.
502
+
While this ensures a max of performance the lack of entry locking can lead to overlapping, non atomic commands, for `putIfAbsent` and `clean` as those methods combine a series of commands sent to Redis.
503
+
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.
504
+
505
+
It is possible to opt in to the locking behavior as follows:
506
+
507
+
[source,java]
485
508
----
509
+
RedisCacheManager cm = RedisCacheManager.usingCacheWriter(DefaultRedisCacheWriter.lockingRedisCacheWriter())
510
+
.withCacheDefaults(defaultCacheConfig())
511
+
...
512
+
----
513
+
514
+
.RedisCacheManager defaults
515
+
[width="80%",cols="<1,<2",options="header"]
516
+
|====
517
+
|Setting
518
+
|Value
519
+
520
+
|Cache Writer
521
+
|non locking
486
522
487
-
NOTE: By default `RedisCacheManager` will lazily initialize `RedisCache` whenever a `Cache` is requested. This can be changed by predefining a `Set` of cache names.
523
+
|Cache Configuration
524
+
|`RedisCacheConfiguraiton#defaultConfiguration`
488
525
489
-
NOTE: By default `RedisCacheManager` will not participate in any ongoing transaction. Use `setTransactionAware` to enable transaction support.
526
+
|Initial Caches
527
+
|none
490
528
491
-
NOTE: By default `RedisCacheManager` does not prefix keys for cache regions, which can lead to an unexpected growth of a `ZSET` used to maintain known keys. It's highly recommended to enable the usage of prefixes in order to avoid this unexpected growth and potential key clashes using more than one cache region.
529
+
|Trasaction Aware
530
+
|no
531
+
|====
532
+
533
+
.RedisCacheConfiguration defaults
534
+
[width="80%",cols="<1,<2",options="header"]
535
+
|====
536
+
|Key Expiration
537
+
|none
538
+
539
+
|Cache `null`
540
+
|yes
492
541
493
-
NOTE: By default `RedisCache` will not cache any `null` values as keys without a value get dropped by Redis itself. However you can explicitly enable `null` value caching via `RedisCacheManager` which will store `org.springframework.cache.support.NullValue` as a placeholder.
0 commit comments