gnodet commented on code in PR #25731:
URL: https://github.com/apache/camel/pull/25731#discussion_r3864358629


##########
core/camel-core-reifier/src/main/java/org/apache/camel/reifier/CacheReifier.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.reifier;
+
+import org.apache.camel.Expression;
+import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.model.CacheDefinition;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.processor.CacheProcessor;
+import org.apache.camel.spi.KeyValueRepository;
+import org.apache.camel.support.MemoryKeyValueRepository;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CacheReifier extends ExpressionReifier<CacheDefinition> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CacheReifier.class);
+
+    public CacheReifier(Route route, ProcessorDefinition<?> definition) {
+        super(route, CacheDefinition.class.cast(definition));
+    }
+
+    @Override
+    public Processor createProcessor() throws Exception {
+        Processor childProcessor = this.createChildProcessor(true);
+
+        KeyValueRepository kvRepository = resolveKeyValueRepository();
+        ObjectHelper.notNull(kvRepository, "keyValueRepository", definition);
+
+        Expression expression = createExpression(definition.getExpression());
+
+        long ttl = parseDuration(definition.getTtl(), -1);
+        boolean cacheNull = parseBoolean(definition.getCacheNull(), false);
+
+        CacheProcessor answer = new CacheProcessor(expression, kvRepository, 
ttl, cacheNull, childProcessor);
+        answer.setDisabled(isDisabled(camelContext, definition));
+        return answer;
+    }
+
+    /**
+     * Strategy method to resolve the {@link KeyValueRepository} to use.
+     * <p/>
+     * Resolution order:
+     * <ol>
+     * <li>Explicit bean set programmatically</li>
+     * <li>Registry lookup by reference name</li>
+     * <li>Auto-discover a single KeyValueRepository from the registry</li>
+     * <li>Auto-create a MemoryKeyValueRepository</li>
+     * </ol>
+     */
+    protected KeyValueRepository resolveKeyValueRepository() {
+        KeyValueRepository repo = definition.getKeyValueRepositoryBean();
+        String ref = parseString(definition.getKeyValueRepository());
+        if (repo == null && ref != null) {
+            repo = mandatoryLookup(ref, KeyValueRepository.class);
+        }
+        if (repo == null) {
+            // Auto-discover a KeyValueRepository from the registry

Review Comment:
   Good catch — added a `NOTE` callout in the docs (commit 7a8df1b) right after 
the auto-discovery sentence. It warns about the shared key namespace and 
suggests two mitigation strategies:
   
   1. Give each `.cache()` block its own named `KeyValueRepository`
   2. Use inherently unique key expressions (e.g. 
`simple("product:${header.productId}")` vs `simple("order:${header.orderId}")`)
   
   _Claude Code on behalf of nicola_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to