Spring Download @Cacheable for findOne

I would like to add caching to my Spring Boot 4 application. As a key, I would like to use the id (Long type) of my object, value is my essence.

@EnableCaching @SpringBootApplication public class SpringBootMainApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMainApplication.class, args); } } @Configuration public class AppConfiguration { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("findOne"); } } public interface SampleEntityService extends CrudRepository<SampleEntity, Long> { @Cacheable(key = "#a0", value = "findOne") SampleEntity findOne(Long id); 

but when I call the findOne method in my test, I got an exception:

 java.lang.ClassCastException: org.springframework.cache.support.SimpleValueWrapper cannot be cast to my.springboot.model.SampleEntity at my.springboot.SampleEntityCacheTest.aaa(SampleEntityCacheTest.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 

According to some tutorials, I tried to use a lot of options, but they all fail.

 @Cacheable(key = "#a0", value = "findOne") @Cacheable(key = "#p0", value = "findOne") @Cacheable(key = "a0", value = "findOne") @Cacheable(key = "p0", value = "findOne") @Cacheable(key = "#id", value = "findOne") @Cacheable(key = "id", value = "findOne") @Cacheable(key = "#root.id", value = "findOne") @Cacheable(key = "root.id", value = "findOne") 

What am I doing wrong?

======= answer ==========

Correct solution:

 @Cacheable(key = "#a0", value = "findOne") 

and inside the test I had to do something like this:

 @Test public void aaa() { SampleEntity entity = sampleEntityService.save(create("name1", uuid(1))); sampleEntityService.findOne(entity.getId()); SampleEntity entityFromCache =(SampleEntity) cacheManager.getCache("findOne").get(entity.getId()).get(); assertNotNull(entityFromCache); } 
+5
source share

All Articles