rishabhdaim commented on code in PR #2819: URL: https://github.com/apache/jackrabbit-oak/pull/2819#discussion_r3009368417
########## oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/OakCacheBuilder.java: ########## @@ -0,0 +1,464 @@ +/* + * 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; + +import java.time.Duration; +import java.util.Locale; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; + +import org.apache.jackrabbit.guava.common.cache.CacheLoader; +import org.jetbrains.annotations.NotNull; + +/** + * Builder for {@link OakCache} and {@link OakLoadingCache} 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 OakCacheBuilder<K, V> { + + // Common fields + private String module; + private CacheImplementation implementation; + private long maximumWeight = -1; + private long maximumSize = -1; + private OakWeigher<K, V> weigher; + private OakRemovalListener<K, 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 OakCacheBuilder() { + } + + /** + * 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> OakCacheBuilder<K, V> newBuilder() { + return new OakCacheBuilder<>(); + } + + /** + * Sets a module label used in logging and diagnostics. + * + * @param module the module name (must not be null) + * @return this builder + */ + @NotNull + public OakCacheBuilder<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 OakCacheBuilder<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(OakWeigher)} and may not be + * combined with {@link #maximumSize(long)}. + * + * @param maximumWeight the maximum weight (must be non-negative) + * @return this builder + */ + @NotNull + public OakCacheBuilder<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 OakCacheBuilder<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 OakCacheBuilder<K, V> weigher(@NotNull OakWeigher<K, 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 OakCacheBuilder<K, V> removalListener(@NotNull OakRemovalListener<K, V> removalListener) { + if (removalListener == null) { + throw new IllegalArgumentException("removalListener must not be null"); + } + this.removalListener = removalListener; + return this; + } + + /** + * Enables collection of cache statistics accessible via {@link OakCache#stats()}. + * + * @return this builder + */ + @NotNull + public OakCacheBuilder<K, V> recordStats() { + this.recordStats = true; + return this; + } + + /** + * Sets how long entries may remain in the cache after their last access. + * Applies to the Caffeine backend only; silently ignored for LIRS. + * + * @param duration the maximum idle duration (must not be null) + * @return this builder + */ + @NotNull + public OakCacheBuilder<K, V> expireAfterAccess(@NotNull Duration duration) { + if (duration == null) { + throw new IllegalArgumentException("duration must not be null"); + } + this.expireAfterAccess = duration; + return this; + } + + /** + * Sets how long entries may remain in the cache after they were written. + * Applies to the Caffeine backend only; silently ignored for LIRS. + * + * @param duration the maximum age after write (must not be null) + * @return this builder + */ + @NotNull + public OakCacheBuilder<K, V> expireAfterWrite(@NotNull Duration duration) { + if (duration == null) { + throw new IllegalArgumentException("duration must not be null"); + } + this.expireAfterWrite = duration; + return this; + } + + /** + * Sets how soon a loading cache should automatically refresh entries after write. + * Applies to the Caffeine backend only; requires {@link #build(OakCacheLoader)} + * and is ignored for LIRS. + * + * @param duration the refresh interval (must not be null) + * @return this builder + */ + @NotNull + public OakCacheBuilder<K, V> refreshAfterWrite(@NotNull Duration duration) { + if (duration == null) { + throw new IllegalArgumentException("duration must not be null"); + } + this.refreshAfterWrite = duration; + return this; + } + + /** + * Sets the number of LIRS segments. Applies to the LIRS backend only. + * + * @param segmentCount the number of segments (must be positive) + * @return this builder + */ + @NotNull + public OakCacheBuilder<K, V> segmentCount(int segmentCount) { + if (segmentCount <= 0) { + throw new IllegalArgumentException("segmentCount must be positive, got: " + segmentCount); + } + this.segmentCount = segmentCount; + return this; + } + + /** + * Sets the LIRS stack move distance. Applies to the LIRS backend only. + * + * @param stackMoveDistance the stack move distance (must be non-negative) + * @return this builder + */ + @NotNull + public OakCacheBuilder<K, V> stackMoveDistance(int stackMoveDistance) { + if (stackMoveDistance < 0) { + throw new IllegalArgumentException("stackMoveDistance must be non-negative, got: " + stackMoveDistance); + } + this.stackMoveDistance = stackMoveDistance; + return this; + } + + /** + * Sets the average expected weight per entry for LIRS sizing. + * Applies to the LIRS backend only and requires {@link #maximumWeight(long)}. + * + * @param averageWeight the average entry weight (must be positive and + * less than or equal to {@link Integer#MAX_VALUE}) + * @return this builder + */ + @NotNull + public OakCacheBuilder<K, V> averageWeight(long averageWeight) { + if (averageWeight <= 0) { + throw new IllegalArgumentException("averageWeight must be positive, got: " + averageWeight); + } + this.averageWeight = averageWeight; + return this; + } Review Comment: I would prefer to keep them for atleast of couple of releases and then would remove them once we don't see any regression in Caffiene. Moreover, the goal of these tasks is to remove Guava, not CacheLIRS. We could always remove CacheLIRS afterwards. cc @thomasmueller @reschke -- 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]
