Copilot commented on code in PR #11033:
URL: https://github.com/apache/gravitino/pull/11033#discussion_r3226014766


##########
core/src/main/java/org/apache/gravitino/cache/CaffeineGravitinoCache.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.gravitino.cache;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.Ticker;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A Caffeine-backed implementation of {@link GravitinoCache}. Supports 
configurable TTL and maximum
+ * size.
+ *
+ * @param <K> the key type
+ * @param <V> the value type
+ */
+public class CaffeineGravitinoCache<K, V> implements GravitinoCache<K, V> {
+
+  private final Cache<K, V> cache;
+
+  /**
+   * Creates a new CaffeineGravitinoCache with the given TTL and maximum size.
+   *
+   * @param ttlMs the time-to-live in milliseconds for cache entries 
(safety-net TTL)
+   * @param maxSize the maximum number of entries in the cache
+   */
+  public CaffeineGravitinoCache(long ttlMs, long maxSize) {
+    this(ttlMs, maxSize, Ticker.systemTicker());
+  }
+
+  CaffeineGravitinoCache(long ttlMs, long maxSize, Ticker ticker) {
+    this.cache =
+        Caffeine.newBuilder()
+            .expireAfterWrite(ttlMs, TimeUnit.MILLISECONDS)
+            .maximumSize(maxSize)
+            .ticker(ticker)
+            .build();
+  }
+
+  @Override
+  public Optional<V> getIfPresent(K key) {
+    V value = cache.getIfPresent(key);
+    return Optional.ofNullable(value);
+  }
+
+  @Override
+  public void put(K key, V value) {
+    cache.put(key, value);
+  }
+
+  @Override
+  public void invalidate(K key) {
+    cache.invalidate(key);
+  }
+
+  @Override
+  public void invalidateAll() {
+    cache.invalidateAll();
+  }
+
+  @Override
+  public void invalidateByPrefix(String prefix) {
+    cache.asMap().keySet().removeIf(k -> k instanceof String && ((String) 
k).startsWith(prefix));
+  }

Review Comment:
   `invalidateByPrefix` performs a full scan over `cache.asMap().keySet()` and 
checks `startsWith` for every entry. This is O(n) per invalidation and can 
become a hot path if the cache grows (e.g., hierarchical metadata-id caches). 
Consider documenting the linear cost and/or restructuring the cache to avoid 
full scans (e.g., partitioning keys by scope or maintaining an index of 
prefixes→keys).



##########
core/src/main/java/org/apache/gravitino/authorization/AuthorizationRequestContext.java:
##########
@@ -20,11 +22,35 @@
 import java.security.Principal;
 import java.util.Map;
 import java.util.Objects;

Review Comment:
   `java.util.Objects` is imported but not referenced in code (only in 
Javadoc). This can trip unused-import checks (e.g., Checkstyle/Spotless). 
Consider removing the import and using a fully-qualified Javadoc link (`{@link 
java.util.Objects#hashCode}`) or referencing `Objects` from code if needed.
   



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