voonhous commented on code in PR #13724: URL: https://github.com/apache/hudi/pull/13724#discussion_r2284133342
########## hudi-io/src/main/java/org/apache/hudi/io/hfile/HFileBlockCache.java: ########## @@ -0,0 +1,163 @@ +/* + * 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.hudi.io.hfile; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +/** + * Least Frequently Used (LFU) cache for HFile blocks to improve read performance by avoiding repeated block reads. + * Uses Caffeine cache with configurable size and TTL. Thread-safe for concurrent access. + */ +public class HFileBlockCache { + + private final Cache<BlockCacheKey, HFileBlock> cache; + + public HFileBlockCache(int maxCacheSize) { + this(maxCacheSize, 30, TimeUnit.MINUTES); + } + + public HFileBlockCache(int maxCacheSize, long expireAfterWrite, TimeUnit timeUnit) { + this.cache = Caffeine.newBuilder() + .maximumSize(maxCacheSize) + .expireAfterWrite(Duration.ofMillis(timeUnit.toMillis(expireAfterWrite))) + .build(); + } + + /** + * Gets a block from cache. + * + * @param key the cache key + * @return cached block or null if not found + */ + public HFileBlock getBlock(BlockCacheKey key) { + return cache.getIfPresent(key); + } + + /** + * Puts a block into cache. + * + * @param key the cache key + * @param block the block to cache + */ + public void putBlock(BlockCacheKey key, HFileBlock block) { + cache.put(key, block); + } + + /** + * Gets a block from cache, or computes and caches it if not present. + * This method is thread-safe and prevents the "cache stampede" problem + * where multiple threads try to load the same value simultaneously. + * + * @param key the cache key + * @param loader callable to load the block if not in cache + * @return cached or newly computed block + * @throws Exception if the loader throws an exception + */ + public HFileBlock getOrCompute(BlockCacheKey key, java.util.concurrent.Callable<HFileBlock> loader) throws Exception { Review Comment: Done -- 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]
