Abyss-lord commented on code in PR #7330: URL: https://github.com/apache/gravitino/pull/7330#discussion_r2151256043
########## core/src/main/java/org/apache/gravitino/cache/provider/CacheFactory.java: ########## @@ -0,0 +1,60 @@ +/* + * 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.provider; + +import java.util.Map; +import java.util.ServiceLoader; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.gravitino.Config; +import org.apache.gravitino.Configs; +import org.apache.gravitino.cache.EntityCache; + +/** Factory class for creating {@link EntityCache} instances. */ +public final class CacheFactory { + // Registered cache providers, discovered via Java SPI. + private static final Map<String, CacheProvider> PROVIDERS = new ConcurrentHashMap<>(); + + static { + ServiceLoader<CacheProvider> loader = ServiceLoader.load(CacheProvider.class); + for (CacheProvider provider : loader) { + PROVIDERS.put(provider.name(), provider); + } + } + + /** + * Creates a new {@link EntityCache} using the cache type specified in the configuration. + * + * @param config The configuration. + * @return A cache instance provided by the selected {@link CacheProvider}. + */ + public static EntityCache getEntityCache(Config config) { + CacheProvider provider = getProvider(config.get(Configs.CACHE_PROVIDER)); + return provider.getCache(config); + } + + private static CacheProvider getProvider(String name) { + CacheProvider provider = PROVIDERS.get(name); + if (provider == null) { + throw new IllegalArgumentException( + "No such cache provider: " + name + ". Available providers: " + PROVIDERS.keySet()); + } + return provider; + } +} Review Comment: Fix. ########## core/src/main/java/org/apache/gravitino/Configs.java: ########## @@ -389,4 +389,12 @@ private Configs() {} .version(ConfigConstants.VERSION_1_0_0) .booleanConf() .createWithDefault(true); + + // Provider name for cache + public static final ConfigEntry<String> CACHE_PROVIDER = + new ConfigBuilder("gravitino.cache.provider") + .doc("Name of the cache provider to use.") + .version(ConfigConstants.VERSION_1_0_0) + .stringConf() + .createWithDefault("Caffeine"); Review Comment: Fix. -- 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]
