【问题标题】:Http inboundGateway caching jpa resultHttp inboundGateway 缓存 jpa 结果
【发布时间】:2018-10-23 04:01:17
【问题描述】:
如何缓存来自处理程序的 Jpa 结果?我尝试搜索一些类似的场景,但我找不到如何在 DSL 中执行此操作。下面是我的示例代码..
@Bean
public IntegrationFlow findProducts() {
return IntegrationFlows
.from(Http.inboundGateway("app/products")
.requestMapping(m -> m.methods(HttpMethod.GET))
.errorChannel("userApp.input"))
.handle((p, h) -> productRepository.findAll())
.get();
}
【问题讨论】:
标签:
spring-data-jpa
spring-integration
spring-cache
spring-integration-dsl
spring-integration-http
【解决方案1】:
您的productRepository.findAll() 必须标有@Cacheable:
* Annotation indicating that the result of invoking a method (or all methods
* in a class) can be cached.
*
* <p>Each time an advised method is invoked, caching behavior will be applied,
* checking whether the method has been already invoked for the given arguments.
* A sensible default simply uses the method parameters to compute the key, but
* a SpEL expression can be provided via the {@link #key} attribute, or a custom
* {@link org.springframework.cache.interceptor.KeyGenerator} implementation can
* replace the default one (see {@link #keyGenerator}).
*
* <p>If no value is found in the cache for the computed key, the target method
* will be invoked and the returned value stored in the associated cache. Note
* that Java8's {@code Optional} return types are automatically handled and its
* content is stored in the cache if present.
*
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
* <em>composed annotations</em> with attribute overrides.
*
* @author Costin Leau
* @author Phillip Webb
* @author Stephane Nicoll
* @author Sam Brannen
* @since 3.1
* @see CacheConfig
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {
在reference manual查看更多信息:
对于缓存声明,Spring的缓存抽象提供了一组Java注解: