Skip to content

Commit a9c2b1c

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-481 - Revise RedisCache.
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.
1 parent eb3ad1e commit a9c2b1c

22 files changed

+1818
-2487
lines changed

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

+76-16
Original file line numberDiff line numberDiff line change
@@ -465,30 +465,90 @@ As shown in the example above, the consuming code is decoupled from the actual s
465465
[[redis:support:cache-abstraction]]
466466
=== Support for Spring Cache Abstraction
467467

468+
NOTE: Changed in 2.0
469+
468470
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:
469471

470-
[source,xml]
472+
[source,java]
473+
----
474+
@Bean
475+
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
476+
return RedisCacheManager.defaultCacheManager(connectionFactory);
477+
}
471478
----
472-
<beans xmlns="http://www.springframework.org/schema/beans"
473-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
474-
xmlns:cache="http://www.springframework.org/schema/cache"
475-
xmlns:c="http://www.springframework.org/schema/c"
476-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
477-
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
478479

479-
<!-- turn on declarative caching -->
480-
<cache:annotation-driven />
480+
`RedisCacheManager` behavior can be configured via `RedisCacheManagerConfigurator` allowing to set the default `RedisCacheConfiguration`, transaction behaviour and predefined caches.
481481

482-
<!-- declare Redis Cache Manager -->
483-
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/>
484-
</beans>
482+
[source,java]
483+
----
484+
RedisCacheManager cm = RedisCacheManager.usingRawConnectionFactory(connectionFactory)
485+
.withCacheDefaults(defaultCacheConfig())
486+
.withInitialCacheConfigurations(singletonMap("predefined", defaultCacheConfig().disableCachingNullValues()))
487+
.transactionAware()
488+
.createAndGet();
489+
----
490+
491+
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.
493+
494+
[source,java]
495+
----
496+
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
497+
.entryTtl(Duration.ofSeconds(1))
498+
.disableCachingNullValues();
499+
----
500+
501+
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]
485508
----
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
486522

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`
488525

489-
NOTE: By default `RedisCacheManager` will not participate in any ongoing transaction. Use `setTransactionAware` to enable transaction support.
526+
|Initial Caches
527+
|none
490528

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
492541

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.
542+
|Prefix Keys
543+
|yes
544+
545+
|Default Prefix
546+
|the actual cache name
547+
548+
|Key Serializer
549+
|`StringRedisSerializer`
550+
551+
|Value Serializer
552+
|`JdkSerializationRedisSerializer`
553+
|====
494554

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

-44
This file was deleted.

0 commit comments

Comments
 (0)