jsedding commented on code in PR #2819: URL: https://github.com/apache/jackrabbit-oak/pull/2819#discussion_r3022379069
########## oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/impl/CacheBuilder.java: ########## @@ -0,0 +1,471 @@ +/* + * 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.jackrabbit.oak.cache.api.impl; + +import java.time.Duration; +import java.util.Locale; + +import com.github.benmanes.caffeine.cache.Caffeine; + +import org.apache.jackrabbit.oak.cache.CacheLIRS; +import org.apache.jackrabbit.oak.cache.api.Cache; +import org.apache.jackrabbit.oak.cache.api.CacheLoader; +import org.apache.jackrabbit.oak.cache.api.LoadingCache; +import org.apache.jackrabbit.oak.cache.api.EvictionListener; +import org.apache.jackrabbit.oak.cache.api.Weigher; +import org.apache.jackrabbit.oak.cache.api.impl.caffeine.CacheComputationException; +import org.apache.jackrabbit.oak.cache.api.impl.caffeine.CaffeineCacheAdapter; +import org.apache.jackrabbit.oak.cache.api.impl.caffeine.CaffeineLoadingCacheAdapter; +import org.apache.jackrabbit.oak.cache.api.impl.lirs.LirsCacheAdapter; +import org.apache.jackrabbit.oak.cache.api.impl.lirs.LirsLoadingCacheAdapter; +import org.jetbrains.annotations.NotNull; + +/** + * Builder for {@link Cache} and {@link LoadingCache} instances. + * + * <p>The backing implementation is chosen by a two-level resolution:</p> + * <ol> + * <li><strong>Per-instance override</strong> — {@link #implementation(CacheImplementation)} + * pins this cache to one backend, regardless of any global setting.</li> + * <li><strong>Global default</strong> — the system property {@code oak.cache.type} + * ({@code lirs} or {@code caffeine}, case-insensitive); defaults to {@code lirs}.</li> + * </ol> + * + * <p>Example:</p> + * <pre>{@code + * OakCache<String, NodeState> cache = OakCacheBuilder.<String, NodeState>newBuilder() + * .module("DocumentNodeStore") + * .maximumWeight(64 * 1024 * 1024) + * .weigher((k, v) -> v.estimateMemory()) + * .recordStats() + * .build(); + * }</pre> + * + * @param <K> the type of cache keys + * @param <V> the type of cache values + */ +public final class CacheBuilder<K, V> { + + // Common fields + private String module; + private CacheImplementation implementation; + private long maximumWeight = -1; + private long maximumSize = -1; + private Weigher<? super K, ? super V> weigher; + private EvictionListener<? super K, ? super V> removalListener; + private boolean recordStats; + // Caffeine-only time-based expiry + private Duration expireAfterAccess; + private Duration expireAfterWrite; + private Duration refreshAfterWrite; + // LIRS-specific tuning + private int segmentCount = -1; + private int stackMoveDistance = -1; + private long averageWeight = -1; + + private CacheBuilder() { + } + + /** + * Creates a new builder with no pre-configured settings. + * + * @param <K> the type of cache keys + * @param <V> the type of cache values + * @return a new builder instance + */ + @NotNull + public static <K, V> CacheBuilder<K, V> newBuilder() { + return new CacheBuilder<>(); + } + + /** + * Sets a module label used in logging and diagnostics. + * + * @param module the module name (must not be null) + * @return this builder + */ + @NotNull + public CacheBuilder<K, V> module(@NotNull String module) { + if (module == null || module.isEmpty()) { + throw new IllegalArgumentException("module must not be null or empty"); + } + this.module = module; + return this; + } + + /** + * Pins this cache to the given implementation, overriding the global + * {@code oak.cache.type} system property. + * + * @param implementation the implementation to use (must not be null) + * @return this builder + */ + @NotNull + public CacheBuilder<K, V> implementation(@NotNull CacheImplementation implementation) { + if (implementation == null) { + throw new IllegalArgumentException("implementation must not be null"); + } + this.implementation = implementation; + return this; + } + + /** + * Sets the maximum total weight of entries the cache may hold. + * Must be used together with {@link #weigher(Weigher)} and may not be + * combined with {@link #maximumSize(long)}. + * + * @param maximumWeight the maximum weight (must be non-negative) + * @return this builder + */ + @NotNull + public CacheBuilder<K, V> maximumWeight(long maximumWeight) { + if (maximumWeight < 0) { + throw new IllegalArgumentException("maximumWeight must be non-negative, got: " + maximumWeight); + } + this.maximumWeight = maximumWeight; + return this; + } + + /** + * Sets the maximum number of entries the cache may hold. + * May not be combined with {@link #maximumWeight(long)}. + * + * @param maximumSize the maximum entry count (must be non-negative) + * @return this builder + */ + @NotNull + public CacheBuilder<K, V> maximumSize(long maximumSize) { + if (maximumSize < 0) { + throw new IllegalArgumentException("maximumSize must be non-negative, got: " + maximumSize); + } + this.maximumSize = maximumSize; + return this; + } + + /** + * Sets the weigher used to determine the weight of each cache entry. + * Requires {@link #maximumWeight(long)}. + * + * @param weigher the weigher (must not be null) + * @return this builder + */ + @NotNull + public CacheBuilder<K, V> weigher(@NotNull Weigher<? super K, ? super V> weigher) { + if (weigher == null) { + throw new IllegalArgumentException("weigher must not be null"); + } + this.weigher = weigher; + return this; + } + + /** + * Registers a listener to be notified when entries are removed from the cache. + * + * @param removalListener the listener (must not be null) + * @return this builder + */ + @NotNull + public CacheBuilder<K, V> removalListener(@NotNull EvictionListener<? super K, ? super V> removalListener) { + if (removalListener == null) { + throw new IllegalArgumentException("removalListener must not be null"); + } + this.removalListener = removalListener; Review Comment: ```suggestion this.evictionListener = evictionListener; ``` -- 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]
