-
Having two different methods with the same name, the same parameter and the same E.g., the two test for this fail: import javax.enterprise.context.ApplicationScoped;
import io.quarkus.cache.CacheResult;
@ApplicationScoped
public class TestService {
public static class Test {
private String value;
public Test(String value1, String value2) {
this.value = value1 + " " + value2;
}
@Override
public String toString() {
return value;
}
}
@CacheResult(cacheName = "test-cache")
public Test hello(String value) {
var test = new Test(value, "from hello");
return test;
}
@CacheResult(cacheName = "test-cache")
public Test hi(String value) {
var test = new Test(value, "from hi");
return test;
}
} import io.quarkus.test.junit.QuarkusTest;
import javax.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@QuarkusTest
public class TestServiceTest {
@Inject
TestService service;
@Test
public void testHello() {
var result = service.hello("TEST");
Assertions.assertEquals("TEST from hello", result.toString());
}
@Test
public void testHi() {
var result = service.hi("TEST");
Assertions.assertEquals("TEST from hi", result.toString());
}
} Quarkus version is 2.9.2.Final |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
/cc @gwenneg |
Beta Was this translation helpful? Give feedback.
-
It even fails when the methods are in two different classes. If I depend on other packages using the caching API, how can I be sure that there isn't any other method in the code having the same parameters and return type? @ApplicationScoped
public class TestService {
public static class Test {
private String value;
public Test(String value1, String value2) {
this.value = value1 + " " + value2;
}
@Override
public String toString() {
return value;
}
}
@CacheResult(cacheName = "test-cache")
public Test hello(String aValue) {
var test = new Test(aValue, "from hello");
return test;
}
} @ApplicationScoped
public class TestTwoService {
@CacheResult(cacheName = "test-cache")
public Test hi(String value) {
var test = new Test(value, "from hi");
return test;
}
} @QuarkusTest
public class TestServiceTest {
@Inject
TestService test;
@Inject
TestTwoService testTwo;
@Test
public void testHello() {
var result = test.hello("TEST");
Assertions.assertEquals("TEST from hello", result.toString());
}
@Test
public void testHi() {
var result = testTwo.hi("TEST");
Assertions.assertEquals("TEST from hi", result.toString());
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi @cdhermann. The The method name or the return type are not used when a cache key is built by the extension. If the annotated method has one or more arguments, then the cache key is built only from arguments. Otherwise a default cache key can be built from the cache name. This means that the cached data can be accessed/modified/deleted from anywhere in your app indeed. There is however a way to ensure that the data cached by a method will not be accessed from another method: using CacheKeyGenerator which is also a new feature from |
Beta Was this translation helpful? Give feedback.
Hi @cdhermann.
The
quarkus-cache
doc was recently updated and now contains a detailed section about how the cache keys are generated. The doc on the Quarkus website doesn't show that update yet but2.10.0.Final
was released a few days ago so I guess the website will be updated soon.The method name or the return type are not used when a cache key is built by the extension. If the annotated method has one or more arguments, then the cache key is built only from arguments. Otherwise a default cache key can be built from the cache name. This means that the cached data can be accessed/modified/deleted from anywhere in your app indeed.
There is however a way to ensure that the data cached by a…