diff --git a/docs/src/main/asciidoc/cache.adoc b/docs/src/main/asciidoc/cache.adoc index 3e0a34619b97c..f167d8c30fb6a 100644 --- a/docs/src/main/asciidoc/cache.adoc +++ b/docs/src/main/asciidoc/cache.adoc @@ -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. @@ -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) { @@ -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 { diff --git a/extensions/cache/deployment/src/test/java/io/quarkus/cache/test/runtime/ExplicitCompositeKeyCacheTest.java b/extensions/cache/deployment/src/test/java/io/quarkus/cache/test/runtime/ExplicitCompositeKeyCacheTest.java index 189336b5238a3..fe6ecb0796294 100644 --- a/extensions/cache/deployment/src/test/java/io/quarkus/cache/test/runtime/ExplicitCompositeKeyCacheTest.java +++ b/extensions/cache/deployment/src/test/java/io/quarkus/cache/test/runtime/ExplicitCompositeKeyCacheTest.java @@ -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. @@ -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) diff --git a/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java b/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java index 6dbe8f668401e..a46ed5b2ba6fc 100644 --- a/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java +++ b/extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java @@ -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.