如何在camel中配置咖啡因缓存

问题描述 投票:0回答:1

我正在添加一个缓存(咖啡因)以在骆驼代码中进行 REST API 调用。

请考虑以下代码:

@Component
public class TenantIdService extends ServiceBaseRouteBuilder {

    @Value("${platform.endpointsOrchestrated.tenantId}")
    private String msEndpoint;
    @Value("${spring.cache.caffeine.maximumSize}")
    private String maximumSize;
    @Value("${spring.cache.caffeine.expireAfterWriteTime}")
    private String expireAfterWriteTime;
    @Value("${spring.cache.caffeine.cacheName}")
    private String cacheName;
    private StmKafkaDTO stmKafkaDTORequest;

    @Override
    public void configure() {
        super.configure();
        

        from(direct(CALL_CACHE))
                .toF("caffeine-cache://%s?evictionType=TIME_BASED&expireAfterWriteTime=%s&maximumSize=%s",
                        cacheName,
                        expireAfterWriteTime,
                        maximumSize);

        // cached call
        from(direct(ADD_TENANT_ID))
                .unmarshal().json(StmKafkaDTO.class)
                .process(exchange -> {
                    stmKafkaDTORequest = exchange.getIn().getBody(StmKafkaDTO.class);
                    exchange.setProperty(STM_EVENT_EXCHANGE_PROPERTY, stmKafkaDTORequest.getPayload());
                })

                // Check if result is in cache
                .setHeader(CaffeineConstants.ACTION, constant("get"))
                .setHeader(CaffeineConstants.KEY, constant("TranslateEvent"))
                .to(direct(CALL_CACHE).getUri())
                //TODO: test log
                .log("Has Result ${header.CamelCaffeineActionHasResult} ActionSucceeded ${header.CamelCaffeineActionSucceeded}")
                .log("Cache body: ${body}")
                .choice()
                    .when(header(CaffeineConstants.ACTION_HAS_RESULT).isEqualTo(Boolean.FALSE))
                        .log(LoggingLevel.INFO, "Cache miss. Calling TranslateEvent...")
                        // Cache miss, call the endpoint
                        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                        .toD(msEndpoint + Util.BRIDGE_ENDPOINT + Util.THROW_EXCEPTION_OFF)
                        .unmarshal().json(CoCountryMappingDTOResponse[].class)
                        .process(TenantIdMapper::mapResponse) // Map the cached response
                        .process(PublishToPlatService::publishStmEventToPlatform) // Publish the mapped response
                        .setHeader(CaffeineConstants.ACTION, constant("put")) // Cache the result
                        .to(direct(CALL_CACHE).getUri())
                    .otherwise()
                        .log(LoggingLevel.INFO, "Cache hit. Response from cache: ${body}")
                        .process(TenantIdMapper::mapResponse) // Map the cached response
                        .process(PublishToPlatService::publishStmEventToPlatform) // Publish the mapped response
                .end()
                .stop();

    }
}

如何设置和配置缓存,而不是每次调用缓存时都传递参数? 我尝试使用 caffeineCacheBuilder 但似乎我的自定义缓存被默认缓存覆盖 谢谢

caching apache-camel caffeine-cache
1个回答
0
投票

我认为您可以使用组件级别选项 [1] 在

application.properties
文件中设置它们

字面上的意思是这样的:

camel.component.caffeine-cache.expire-after-access-time=100
camel.component.caffeine-cache.expire-after-write-time=100
...

当你使用组件时,如果URI中没有设置具体参数,那么应该从这里获取。

[1] https://camel.apache.org/components/4.0.x/caffeine-cache-component.html#_spring_boot_auto_configuration

© www.soinside.com 2019 - 2024. All rights reserved.