rishabhdaim commented on code in PR #2819: URL: https://github.com/apache/jackrabbit-oak/pull/2819#discussion_r3009329127
########## 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) { Review Comment: Another reason to keep it, in case we decided to support any other Cache implementation in future, then this might come in handy. -- 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]
