Аннотация Spring Boot @Cachebale не работает должным образомJAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Аннотация Spring Boot @Cachebale не работает должным образом

Сообщение Гость »


I'm currently implementing Spring cache for some of our methods in production. It worked well with a REST controller method using @GetMapping, which makes a call to a service method annotated with @Cacheable.

However, when I tried to implement the same caching mechanism in Groovy scripts, it doesn't seem to work. ChatGPT suggested that this might be because the method call annotated with @Cacheable in the service happens from the constructor of the component.

To investigate further, I created a test Spring Boot app and attempted to call a test method with @Cacheable from another bean's method annotated with @PostConstruct, but unfortunately, it still doesn't work. After reading several manuals and documentation, I suspect that no proxy is being created or something similar.

Here is the code for my test app:

package test.spring.springtest; import jakarta.annotation.PostConstruct; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringTestApplication { private final JavaStarter javaStarter; public SpringTestApplication(JavaStarter javaStarter) { this.javaStarter = javaStarter; } public static void main(String[] args) { SpringApplication.run(SpringTestApplication.class, args); } @PostConstruct public void invokeJavaStarterMethod() { javaStarter.invokeTestMethod("test"); } } package test.spring.springtest.config; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("cache"); } } package test.spring.springtest; import org.springframework.stereotype.Component; import test.spring.springtest.service.JavaService; @Component public class JavaStarter { private final JavaService javaService; JavaStarter(JavaService javaService) { this.javaService = javaService; } public void invokeTestMethod(String input) { for (int i = 0; i < 3; i++) { System.out.println("Output: " + javaService.testMethod(input)); System.out.println(); } } } package test.spring.springtest.service; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class JavaService { @Cacheable(cacheNames = "cache") public String testMethod(String input) { System.out.println("Cache is not being used"); return input; } } Could you please explain why the caching behavior is not working as expected?

In my project, I decided to implement caching using the @Cacheable annotation in my Spring Boot application. The caching worked perfectly when I applied it to a REST controller method that called a service method annotated with @Cacheable.

However, when I attempted to use the same caching mechanism in my Groovy scripts, it didn't have the desired effect. I even tried calling a test method with @Cacheable from another bean's method using @PostConstruct, but that didn't solve the issue either.

I took the time to research various manuals and documentation to find a solution, but I couldn't figure out the underlying cause of the problem. It seems there might be a reason why the caching behavior isn't working as expected. It could be related to how I'm using the @Cacheable annotation or how it interacts with other components.


Источник: https://stackoverflow.com/questions/781 ... s-expected
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»