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


##########
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:
   Non-blocking: when no explicit `keyValueRepository` is set on a 
`.cache(...)` block, this auto-discovers a single `KeyValueRepository` bean 
from the registry. If a user registers one shared `KeyValueRepository` and uses 
it across multiple independent `.cache(...)` blocks (each relying on 
auto-discovery rather than an explicit ref), those blocks will all share the 
same key namespace — a cache key collision footgun if two unrelated blocks 
happen to produce the same key value. Might be worth calling out explicitly in 
`cache-eip.adoc` so users know to give each block its own repository (or a 
block-specific key prefix) when sharing a backing store.



-- 
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