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


##########
components/camel-redis/src/main/java/org/apache/camel/component/redis/RedisKeyValueRepository.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.component.redis;
+
+import java.time.Duration;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.camel.api.management.ManagedAttribute;
+import org.apache.camel.api.management.ManagedOperation;
+import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.spi.Configurer;
+import org.apache.camel.spi.KeyValueRepository;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.jspecify.annotations.Nullable;
+import org.redisson.Redisson;
+import org.redisson.api.RBucket;
+import org.redisson.api.RKeys;
+import org.redisson.api.RedissonClient;
+import org.redisson.api.options.KeysScanOptions;
+import org.redisson.config.Config;
+
+/**
+ * A {@link KeyValueRepository} implementation backed by Redis using the 
Redisson client.
+ * <p/>
+ * Keys are namespaced under a configurable prefix ({@link #keyPrefix}) to 
avoid collisions with other data in the same
+ * Redis instance. Values are serialized using Redisson's built-in codec 
(defaults to {@code MarshallingCodec} which
+ * uses Java serialization).
+ * <p/>
+ * TTL is mapped from milliseconds to Redis native key expiry via
+ * {@code RBucket.set(value, ttl, TimeUnit.MILLISECONDS)}. Atomic {@link 
#putIfAbsent} is supported via
+ * {@code RBucket.setIfAbsent}.
+ *
+ * @since 4.23
+ */
+@Metadata(label = "bean",
+          description = "A KeyValueRepository backed by Redis (Redisson 
client).",
+          annotations = { 
"interfaceName=org.apache.camel.spi.KeyValueRepository" })
+@Configurer(metadataOnly = true)
+@ManagedResource(description = "Redis based key-value repository")
+public class RedisKeyValueRepository extends ServiceSupport implements 
KeyValueRepository {
+
+    private boolean shutdownRedisson;
+
+    @Metadata(label = "advanced", description = "To use an existing Redisson 
client to connect to Redis server")
+    private RedissonClient redisson;
+    @Metadata(description = "URL to remote Redis server (host:port)", required 
= true)
+    private String endpoint;
+    @Metadata(description = "Key prefix used to namespace entries in Redis", 
defaultValue = "camel-kvr:")
+    private String keyPrefix = "camel-kvr:";
+
+    public RedisKeyValueRepository() {
+    }
+
+    /**
+     * Creates a new Redis key-value repository connecting to the given 
endpoint.
+     *
+     * @param endpoint the Redis server address in {@code host:port} format
+     */
+    public RedisKeyValueRepository(String endpoint) {
+        this.endpoint = endpoint;
+    }
+
+    /**
+     * Creates a new Redis key-value repository connecting to the given 
endpoint with a custom key prefix.
+     *
+     * @param endpoint  the Redis server address in {@code host:port} format
+     * @param keyPrefix the prefix to prepend to all keys stored in Redis
+     */
+    public RedisKeyValueRepository(String endpoint, String keyPrefix) {
+        this.endpoint = endpoint;
+        this.keyPrefix = keyPrefix;
+    }
+
+    @Override
+    @ManagedOperation(description = "Get value by key")
+    public @Nullable Object get(String key) {
+        RBucket<Object> bucket = redisson.getBucket(toRedisKey(key));
+        return bucket.get();
+    }
+
+    @Override
+    @ManagedOperation(description = "Put a key-value pair with optional TTL")
+    public @Nullable Object put(String key, Object value, Duration ttl) {
+        RBucket<Object> bucket = redisson.getBucket(toRedisKey(key));
+        Object previous = bucket.get();
+        if (hasPositiveTtl(ttl)) {
+            bucket.set(value, ttl);
+        } else {
+            bucket.set(value);
+        }
+        return previous;
+    }
+
+    @Override
+    @ManagedOperation(description = "Delete a key")
+    public @Nullable Object delete(String key) {
+        RBucket<Object> bucket = redisson.getBucket(toRedisKey(key));
+        return bucket.getAndDelete();
+    }
+
+    @Override
+    @ManagedOperation(description = "Check if key exists")
+    public boolean contains(String key) {
+        RBucket<Object> bucket = redisson.getBucket(toRedisKey(key));
+        return bucket.isExists();
+    }
+
+    @Override
+    public Set<String> keys() {
+        RKeys rKeys = redisson.getKeys();
+        int prefixLen = keyPrefix.length();
+        return 
rKeys.getKeysStream(KeysScanOptions.defaults().pattern(toRedisKey("*")))
+                .map(k -> k.substring(prefixLen))
+                .collect(Collectors.toUnmodifiableSet());
+    }
+
+    @Override
+    @ManagedOperation(description = "Clear all entries")
+    public void clear() {
+        RKeys rKeys = redisson.getKeys();
+        String pattern = toRedisKey("*");
+        rKeys.deleteByPattern(pattern);
+    }
+
+    @Override
+    public @Nullable Object putIfAbsent(String key, Object value, Duration 
ttl) {
+        RBucket<Object> bucket = redisson.getBucket(toRedisKey(key));
+        boolean wasSet;
+        if (hasPositiveTtl(ttl)) {
+            wasSet = bucket.setIfAbsent(value, ttl);
+        } else {
+            wasSet = bucket.setIfAbsent(value);
+        }
+        if (wasSet) {
+            return null;
+        }
+        // Key already existed; return the current value
+        return bucket.get();
+    }
+
+    @Override
+    public boolean replace(String key, Object expectedOldValue, Object 
newValue, Duration ttl) {

Review Comment:
   _Claude Code on behalf of gnodet_
   
   Already addressed — the Javadoc on this method documents the two-step 
non-atomic TTL application and the crash-window tradeoff, as suggested. See 
lines 166-174 in the current code.



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