简体   繁体   English

Spring Boot根据请求禁用@Cacheable

[英]Spring Boot disable @Cacheable per request

I'm trying to disable a Cacheable function in Spring Boot based on a URL parameter. 我试图基于URL参数禁用Spring Boot中的Cacheable函数。 For example http://myapplication.com/some/request?cache=false 例如http://myapplication.com/some/request?cache=false

I tried the condition with SpEL but I haven't been able to get it work 我曾用SpEL尝试过这种情况,但无法使其正常工作

@Cacheable(value = "value", keyGenerator = "keygenerator", condition = "#{someComponent.isCacheEnabled()}")

Any suggestions? 有什么建议么? Thanks in advance! 提前致谢!

user4843- user4843-

I am assuming your "arbitrary component" (ie someComponent ) is a Spring managed bean in your Spring ApplicationContext ? 我假设您的“任意组件”(即someComponent )是您的Spring ApplicationContextSpring托管bean?

If so, then you have not properly referenced this Spring managed bean using the SpEL expression specified on the condition attribute inside your @Cacheable annotation declaration. 如果是这样,则您没有使用@Cacheable注释声明内的condition属性指定的SpEL表达式正确引用此Spring托管bean。 It should read... 它应该显示为...

condition="#{@someComponent.isCacheEnabled()}"

Notice the use of the @ symbol on someComponent , which is explained in more detail here . 请注意,在someComponent上使用了@符号, 此处将对其进行详细说明。 And, although the Spring documentation is less than clear on the matter, it does imply the SpEL evaluation context created and used inside Spring's Cache Abstraction when processing the caching annotations uses "the built-in parameters", or SpEL conventions , in addition to the ones listed in Table 36.1 - Cache SpEL available metadata . 并且,尽管Spring 文档尚不清楚,但这确实暗示了在处理缓存注释时,除了使用“内置参数”或SpEL约定外 ,SpEL评估上下文是在Spring的Cache Abstraction中创建和使用的。 表36.1-Cache SpEL可用元数据中列出的那些。 See examples here . 在这里查看示例。

I would also caution you on your use of some arbitrary component, or Spring managed bean to manage caching state for your entire application like this. 我还要提醒您使用某些任意组件或Spring托管Bean来像这样管理整个应用程序的缓存状态。

  1. First, if someComponent is a Singleton bean (the default in the Spring container) then this bean would need to be Thread-safe, particularly as its state is changing, possibly and seemingly with every HTTP request, which leads me to... 首先,如果someComponentSingleton Bean( Spring容器中的默认值),则此Bean需要是线程安全的,尤其是在其状态不断变化的情况下(似乎在每个HTTP请求中都在变化),这导致我...

  2. I am not sure you want a Spring managed bean managing the "cacheable state" of your application service/repository methods if the caching state is conditional on each and every individual HTTP request, particularly since most HTTP containers process each HTTP request in a separate Thread. 我不确定如果缓存状态取决于每个单独的HTTP请求,那么您不确定是否需要Spring托管的bean管理应用程序服务/存储库方法的“可缓存状态”,尤其是因为大多数HTTP容器在单独的线程中处理每个HTTP请求。 You are most certainly going to run into race conditions. 您肯定会遇到种族问题。 In this case, I'd rather use an additional cacheable service method parameter to determine the cacheable state of that particular service method invocation, stemming from the HTTP request. 在这种情况下,我宁愿使用一个额外的可缓存服务方法参数来确定特定服务方法调用的可缓存状态,该状态源自HTTP请求。

Example: 例:

@Cacheable(value = "value", keyGenerator="keyGenerator" conditional="#enableCaching")
public <return-type> someCacheableServiceMethod(..., boolean enableCaching) {
  ...
}

In fact, I'd even suggest using more intelligible business-oriented rules to determine the cacheable state of your service methods. 实际上,我什至建议使用更易理解的面向业务的规则来确定服务方法的可缓存状态。

Hope this helps! 希望这可以帮助!

-John -约翰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM