春季缓存无法作为计算属性工作

huangapple go评论142阅读模式
英文:

Spring Cache not working as a compute property

问题

Gradle依赖:

  1. compile 'org.springframework.boot:spring-boot-starter-cache'

Spring Boot应用的主类:

  1. @SpringBootApplication
  2. @EnableCaching
  3. public class Application {
  4. public static ApplicationContext applicationContext;
  5. public static void main(String[] args) {
  6. TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
  7. // todo: 尝试保存响应文本和请求体
  8. applicationContext = SpringApplication.run(Application.class, args);
  9. }
  10. @Bean
  11. WebMvcConfigurer webMvcConfigurer(){
  12. return new WebMvcConfigurer() {
  13. @Override
  14. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  15. registry
  16. .addResourceHandler("/**")
  17. .addResourceLocations("classpath:/")
  18. .setCacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS).noTransform().mustRevalidate());
  19. }
  20. };
  21. }
  22. }

我的计算属性和测试方法:

  1. public String test(){
  2. return hello();
  3. }
  4. @Cacheable("hello")
  5. public String hello(){
  6. System.out.println("hello");
  7. return "Hello";
  8. }
英文:

I want to use a mechanism for create a one time compute function. I try to use Spring Caching. But it does not working. Please help me to solve this problem. My code like as below,

Gradle Dependency

  1. compile 'org.springframework.boot:spring-boot-starter-cache'

Main Class of Spring Boot Application

  1. @SpringBootApplication
  2. @EnableCaching
  3. public class Application {
  4. public static ApplicationContext applicationContext;
  5. public static void main(String[] args) {
  6. TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
  7. // todo: Try to save response text and request body
  8. applicationContext = SpringApplication.run(Application.class, args);
  9. }
  10. @Bean
  11. WebMvcConfigurer webMvcConfigurer(){
  12. return new WebMvcConfigurer() {
  13. @Override
  14. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  15. registry
  16. .addResourceHandler("/**")
  17. .addResourceLocations("classpath:/")
  18. .setCacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS).noTransform().mustRevalidate());
  19. }
  20. };
  21. }
  22. }

My Coputational property and Test method

  1. public String test(){
  2. return hello();
  3. }
  4. @Cacheable("hello")
  5. public String hello(){
  6. System.out.println("hello");
  7. return "Hello";
  8. }

答案1

得分: 0

@Cacheable注解在从@Bean之外的位置调用时会缓存值,因此从bean内部的另一个方法调用它将不起作用。

尝试类似于以下方式:

  1. @Bean
  2. public class CachingBean {
  3. @Cacheable("hello")
  4. public String hello(){
  5. System.out.println("hello");
  6. return "Hello";
  7. }
  8. }
  9. @Service
  10. public class CallerService {
  11. @Autowired
  12. private CachingBean cachingBean;
  13. // Setters, constructors...
  14. public String test(){
  15. return cachingBean.hello();
  16. }
  17. }

然后它应该会起作用。
这是因为@Cacheable注解在作为Bean注入时会在方法调用周围创建代理,因此直接调用(直接创建的实例)或内部调用不会被拦截,缓存机制甚至不会看到这些调用。

我有时仍会忘记它,并在刚开始时受到影响:)。
干杯!

英文:

The @Cacheable annotation caches the values when it is called from outside your @Bean so calling it from another method inside your bean will not work.

try something like

  1. @Bean
  2. public class CachingBean {
  3. @Cacheable("hello")
  4. public String hello(){
  5. System.out.println("hello");
  6. return "Hello";
  7. }
  8. }
  9. @Service
  10. public class CallerService {
  11. @Autowired
  12. private CachingBean cachingBean;
  13. // Setters, constructors...
  14. public String test(){
  15. return cachingBean.hello();
  16. }
  17. }

And then it should work.
That's because the @Cacheable annotation creates a proxy around the method call when it is injected as a Bean, so a direct call (to an instance created directly) or an internal call are not intercepted and the caching mechanism does not even see those calls.

I still sometimes forget about it and get biten by it at the beginning :).
Cheers!

huangapple
  • 本文由 发表于 2020年4月5日 16:26:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61039898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定