gnodet commented on code in PR #1270:
URL: https://github.com/apache/maven-mvnd/pull/1270#discussion_r4792999971


##########
daemon/src/main/java/org/mvndaemon/mvnd/cache/invalidating/InvalidatingRequestCache.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.mvndaemon.mvnd.cache.invalidating;
+
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import org.apache.maven.api.Session;
+import org.apache.maven.api.SessionData;
+import org.apache.maven.api.cache.CacheMetadata;
+import org.apache.maven.api.cache.CacheRetention;
+import org.apache.maven.api.services.Request;
+import org.apache.maven.api.services.RequestTrace;
+import org.apache.maven.api.services.Result;
+import org.apache.maven.api.services.VersionParser;
+import org.apache.maven.api.services.model.ModelResolver;
+import org.apache.maven.impl.cache.AbstractRequestCache;
+import org.apache.maven.impl.cache.CachingSupplier;
+import org.apache.maven.impl.cache.WeakIdentityMap;
+import org.apache.maven.impl.model.DefaultModelBuilder;
+import org.mvndaemon.mvnd.cache.Cache;
+import org.mvndaemon.mvnd.cache.CacheFactory;
+import org.mvndaemon.mvnd.cache.CacheRecord;
+
+public class InvalidatingRequestCache extends AbstractRequestCache {
+
+    protected static final SessionData.Key<WeakIdentityMap> KEY =
+            SessionData.key(WeakIdentityMap.class, CacheMetadata.class);
+    protected static final Object ROOT = new Object();
+
+    protected final Cache<?, ?> forever;
+
+    protected final CacheFactory cacheFactory;
+    protected final VersionParser versionParser;
+
+    public InvalidatingRequestCache(CacheFactory cacheFactory, VersionParser 
versionParser, Cache<?, ?> forever) {
+        this.cacheFactory = cacheFactory;
+        this.versionParser = versionParser;
+        this.forever = forever;
+    }
+
+    @SuppressWarnings("unchecked")
+    protected <REQ extends Request<?>, REP extends Result<REQ>> 
CachingSupplier<REQ, REP> doCache(
+            REQ req, Function<REQ, REP> supplier) {
+
+        CacheRetention retention;
+        Function<REQ, Record<REQ, REP>> record;
+        if (req instanceof DefaultModelBuilder.RgavCacheKey rgavCacheKey) {
+            retention = versionParser.isSnapshot(rgavCacheKey.version())
+                    ? CacheRetention.REQUEST_SCOPED
+                    : CacheRetention.PERSISTENT;
+            record = k -> new Record<>(supplier, List.of());
+        } else if (req instanceof DefaultModelBuilder.SourceCacheKey 
sourceCacheKey) {
+            retention = CacheRetention.PERSISTENT;
+            Path path = sourceCacheKey.source().getPath();
+            record = k -> new Record<>(supplier, path != null ? List.of(path) 
: List.of());
+        } else if (req instanceof ModelResolver.ModelResolverRequest 
modelResolverRequest) {
+            retention = 
versionParser.isSnapshot(modelResolverRequest.version())
+                    ? CacheRetention.REQUEST_SCOPED
+                    : CacheRetention.PERSISTENT;
+            record = k -> new Record<>(supplier, List.of());
+        } else {
+            retention = Objects.requireNonNullElse(
+                    req instanceof CacheMetadata metadata ? 
metadata.getCacheRetention() : null,
+                    CacheRetention.PERSISTENT);
+            record = k -> new Record<>(supplier, List.of());
+        }
+        if ((retention == CacheRetention.REQUEST_SCOPED || retention == 
CacheRetention.SESSION_SCOPED)
+                && req.getSession() instanceof Session session) {
+            Object key = retention == CacheRetention.REQUEST_SCOPED ? 
doGetOuterRequest(req) : ROOT;
+            WeakIdentityMap<Object, Cache<REQ, Record<REQ, REP>>> caches =
+                    session.getData().computeIfAbsent(KEY, 
WeakIdentityMap::new);
+            Cache<REQ, Record<REQ, REP>> cache = caches.computeIfAbsent(key, k 
-> cacheFactory.newCache());
+            return cache.computeIfAbsent(req, record);
+        } else if (retention == CacheRetention.PERSISTENT) {
+            Cache<REQ, Record<REQ, REP>> cache = (Cache) forever;

Review Comment:
   The unchecked cast is already suppressed at the method level, which is good. 
However, using a weak cache for PERSISTENT retention is counterintuitive. Weak 
references mean entries can be garbage collected at any time, which may 
surprise users expecting "persistent" to mean "never evicted until 
invalidated". Consider documenting this behavior or using a different name than 
"forever" for this cache.
   
   _Claude Code on behalf of Guillaume Nodet_



##########
daemon/src/main/java/org/mvndaemon/mvnd/cache/impl/TimestampCacheFactory.java:
##########
@@ -116,7 +122,11 @@ private Set<FileState> currentFileStates() {
 
     static class TimestampCache<K, V extends CacheRecord> implements Cache<K, 
V> {
 
-        private final ConcurrentHashMap<K, Record<V>> map = new 
ConcurrentHashMap<>();
+        private final Map<K, Record<V>> map;
+
+        TimestampCache(Map<K, Record<V>> map) {

Review Comment:
   The map parameter allows injection of WeakIdentityMap. Verify that 
WeakIdentityMap provides thread-safe operations for the compute() call on line 
138, as TimestampCache appears to be used in concurrent contexts.
   
   _Claude Code on behalf of Guillaume Nodet_



##########
daemon/src/main/java/org/mvndaemon/mvnd/cache/CacheRecord.java:
##########
@@ -35,5 +35,5 @@ public interface CacheRecord {
     /**
      * Callback called by the cache when this {@link CacheRecord} is removed 
from the cache.
      */
-    void invalidate();
+    default void invalidate() {}

Review Comment:
   ✅ Good change! Making invalidate() a default method with empty 
implementation is cleaner and removes boilerplate from implementations that 
don't need custom invalidation logic.
   
   _Claude Code on behalf of Guillaume Nodet_



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