jerryshao commented on code in PR #7330: URL: https://github.com/apache/gravitino/pull/7330#discussion_r2150442316
########## 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: Please change to "caffeine", do not capitalize, which will easily lead to mis-configuraition. ########## 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: I think it is not necessary to use SPI to load the different cache interfaces, we can use class name and reflection to instantiate the class. ########## core/src/main/java/org/apache/gravitino/cache/provider/CacheProvider.java: ########## @@ -0,0 +1,45 @@ +/* + * 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 org.apache.gravitino.Config; +import org.apache.gravitino.cache.EntityCache; + +/** + * Interface CacheProvider defines the standard methods for a cache provider to obtain cache + * instances. + */ +public interface CacheProvider { + + /** + * Gets the name of the cache provider. + * + * @return The name of the cache provider. + */ + String name(); + + /** + * Gets a {@link EntityCache} instance based on the cache configuration. + * + * @param config The configuration for the cache. + * @return A cache instance initialized according to the configuration. + */ + EntityCache getCache(Config config); +} Review Comment: We don't need this interface, it is only used for SPI, but I think using SPI is not necessary. SPI will load all the implementations, which is not what we wanted. ########## 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: You can refer to the implementation like entity storage factory. -- 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: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org