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

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

Spring Cache not working as a compute property

问题

Gradle依赖:

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

Spring Boot应用的主类:

@SpringBootApplication
@EnableCaching
public class Application {

    public static ApplicationContext applicationContext;

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        // todo: 尝试保存响应文本和请求体
        applicationContext = SpringApplication.run(Application.class, args);
    }

    @Bean
    WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry
                        .addResourceHandler("/**")
                        .addResourceLocations("classpath:/")
                        .setCacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS).noTransform().mustRevalidate());
            }
        };
    }
}

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

public String test(){
    return hello();
}

@Cacheable("hello")
public String hello(){
    System.out.println("hello");
    return "Hello";
}
英文:

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

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

Main Class of Spring Boot Application

@SpringBootApplication
@EnableCaching
public class Application {

	public static ApplicationContext applicationContext;

	public static void main(String[] args) {
		TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
		// todo: Try to save response text and request body
		applicationContext = SpringApplication.run(Application.class, args);
	}

	@Bean
	WebMvcConfigurer webMvcConfigurer(){
		return new WebMvcConfigurer() {
			@Override
			public void addResourceHandlers(ResourceHandlerRegistry registry) {
				registry
						.addResourceHandler("/**")
						.addResourceLocations("classpath:/")
						.setCacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS).noTransform().mustRevalidate());
			}
		};
	}
}

My Coputational property and Test method

public String test(){
        return hello();
    }


    @Cacheable("hello")
    public String hello(){
        System.out.println("hello");
        return "Hello";
    }

答案1

得分: 0

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

尝试类似于以下方式:

@Bean 
public class CachingBean {
    @Cacheable("hello")
    public String hello(){
        System.out.println("hello");
        return "Hello";
    }
}

@Service
public class CallerService {

  @Autowired
  private CachingBean cachingBean;

  // Setters, constructors...

  public String test(){
        return cachingBean.hello();
  }
}

然后它应该会起作用。
这是因为@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

@Bean 
public class CachingBean {
    @Cacheable("hello")
    public String hello(){
        System.out.println("hello");
        return "Hello";
    }
}

@Service
public class CallerService {

  @Autowired
  private CachingBean cachingBean;

  // Setters, constructors...

  public String test(){
        return cachingBean.hello();
  }
}

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:

确定