Skip to content

Commit

Permalink
Merge pull request #11046 from gwenneg/issue-10436-cache-key-paramete…
Browse files Browse the repository at this point in the history
…rs-positions

Fix explicit composite cache key elements values and improve doc
  • Loading branch information
machi1990 authored Jul 29, 2020
2 parents f80bb06 + 133263b commit 8b69ba0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
47 changes: 45 additions & 2 deletions docs/src/main/asciidoc/cache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,49 @@ method annotated with `@CacheResult` or `@CacheInvalidate`.

This annotation is optional and should only be used when some of the method arguments are NOT part of the cache key.

=== Composite cache key building logic

When a cache key is built from several method arguments, whether they are explicitly identified with `@CacheKey` or not, the building logic depends on the order of these arguments in the method signature. On the other hand, the arguments names are not used at all and do not have any effect on the cache key.

[source,java]
----
package org.acme.cache;
import javax.enterprise.context.ApplicationScoped;
import io.quarkus.cache.CacheInvalidate;
import io.quarkus.cache.CacheResult;
@ApplicationScoped
public class CachedService {
@CacheResult(cacheName = "foo")
public Object load(String keyElement1, Integer keyElement2) {
// Call expensive service here.
}
@CacheInvalidate(cacheName = "foo")
public void invalidate1(String keyElement2, Integer keyElement1) { <1>
}
@CacheInvalidate(cacheName = "foo")
public void invalidate2(Integer keyElement2, String keyElement1) { <2>
}
@CacheInvalidate(cacheName = "foo")
public void invalidate3(Object notPartOfTheKey, @CacheKey String keyElement1, @CacheKey Integer keyElement2) { <3>
}
@CacheInvalidate(cacheName = "foo")
public void invalidate4(Object notPartOfTheKey, @CacheKey Integer keyElement2, @CacheKey String keyElement1) { <4>
}
}
----
<1> Calling this method WILL invalidate values cached by the `load` method even if the key elements names have been swapped.
<2> Calling this method WILL NOT invalidate values cached by the `load` method because the key elements order is different.
<3> Calling this method WILL invalidate values cached by the `load` method because the key elements order is the same.
<4> Calling this method WILL NOT invalidate values cached by the `load` method because the key elements order is different.

== Configuring the underlying caching provider

This extension uses https://github.com/ben-manes/caffeine[Caffeine] as its underlying caching provider.
Expand Down Expand Up @@ -504,7 +547,7 @@ A simple approach could be to catch the exception and return `null`, so that the
act accordingly:

.Sample code
[souce,java]
[source,java]
----
public void caller(int val) {
Expand Down Expand Up @@ -542,7 +585,7 @@ To prevent the cache from caching (marker) results from a remote call, we need t
the exception bubble out of the called method and catch it at the caller side:

.With Exception bubbling up
[souce,java]
[source,java]
----
public void caller(int val) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testAllCacheAnnotations() {
// Action: cache entry invalidation.
// Expected effect: STEP 2 cache entry removed.
// Verified by: STEP 7.
cachedService.invalidate(KEY_1_ELEMENT_1, KEY_1_ELEMENT_2, new Object());
cachedService.invalidate(new Object(), KEY_1_ELEMENT_1, KEY_1_ELEMENT_2);

// STEP 7
// Action: same call as STEP 2.
Expand Down Expand Up @@ -129,7 +129,7 @@ public String cachedMethod(@CacheKey Locale keyElement1, @CacheKey BigDecimal ke
}

@CacheInvalidate(cacheName = CACHE_NAME)
public void invalidate(@CacheKey Locale keyElement1, @CacheKey BigDecimal keyElement2, Object notPartOfTheKey) {
public void invalidate(Object notPartOfTheKey, @CacheKey Locale keyElement1, @CacheKey BigDecimal keyElement2) {
}

@CacheInvalidateAll(cacheName = CACHE_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected Object buildCacheKey(String cacheName, short[] cacheKeyParameterPositi
// @CacheKey-annotated parameters that were identified at build time.
if (cacheKeyParameterPositions.length > 0) {
for (int i = 0; i < cacheKeyParameterPositions.length; i++) {
keyElements.add(methodParameterValues[i]);
keyElements.add(methodParameterValues[cacheKeyParameterPositions[i]]);
}
} else {
// Otherwise, the key is composed of all of the method parameters.
Expand Down

0 comments on commit 8b69ba0

Please sign in to comment.