Constreto :: Core iMplementation 3.0.0 Beta4 Java Framework -Performance Optimism Guide (Constreto :: Core Implementation 3.0.0 Beta4 Java Framework -Performance Optimization)
Constreto is an open source Java framework for application configuration management.It provides a flexible and scalable way to manage the configuration information of the application and support configuration switching and management in different environments.This article will introduce performance optimization when using the Constreto framework, and provide some Java code examples.
1. Avoid reading configuration information multiple times
When using the Constreto framework, we usually use the key value in the configuration file to obtain the configuration information of the application.To improve performance, we should avoid reading configuration information multiple times.You can load the configuration information into the memory by starting the application and read directly from the memory when needed, instead of reading from the configuration file every time.
The following is an example code, demonstrate how to load the configuration information to the memory:
import org.constretto.ConstrettoBuilder;
import org.constretto.ConfigurationStore;
public class ConfigLoader {
private static ConfigurationStore configuration;
public static void loadConfig() {
ConstrettoBuilder constrettoBuilder = new ConstrettoBuilder();
configuration = constrettoBuilder.getConfigurationStore();
}
public static String getString(String key) {
return configuration.evaluateToString(key);
}
public static int getInt(String key) {
return configuration.evaluateTo(Integer.class).from(key);
}
}
Corrates the method of `Configloader.loadConfig ()` in the application class to load the configuration information into the memory.Then, where you need to obtain the configuration information, you can read directly from the memory by calling the `ConfigLoader.getString (key) or` Configloader.getInt (key) method.
2. Reasonable use of configuration cache
The Constreto framework provides configuration cache function, which can cache the configuration information to the local area, reduce the number of access to the remote configuration server, thereby improving performance.We can set different cache strategies according to the update frequency of configuration information, such as refresh the cache or manually refresh the cache when configured information update.
Here are a sample code to demonstrate how to use the configuration cache:
import org.constretto.ConstrettoBuilder;
import org.constretto.ConstrettoConfiguration;
import org.constretto.cache.CacheStrategy;
import org.constretto.cache.CacheStrategyBuilder;
import org.constretto.cache.ConfigurationCache;
public class ConfigCache {
private static ConfigurationCache cache;
public static void initCache() {
ConstrettoBuilder constrettoBuilder = new ConstrettoBuilder();
ConstrettoConfiguration configuration = constrettoBuilder.getConfiguration();
CacheStrategy Cachestrategy = CachestrategyBuilder.nocache (). Build (); // Set to not use the cache
cache = new ConfigurationCache(configuration, cacheStrategy);
cache.initialize();
}
public static String getString(String key) {
return cache.evaluateToString(key);
}
public static int getInt(String key) {
return cache.evaluateTo(Integer.class).from(key);
}
}
Corrates `Configcache.initcache ()` in the application class to initialize the configuration cache.Then, when you need to obtain the configuration information, read the method from the cache by calling the `Configcache.getString (key) or` Configcache.getIntint (key) method.
3. Avoid frequent configuration information updates
When using the Constreto framework, we can store the configuration information in the remote configuration server and support dynamic updates.However, frequent configuration information updates may lead to decline in performance.Therefore, we should reasonably set the frequency of configuration information to avoid frequent update operations.
The following is a sample code, demonstrate how to configure the update frequency of configuration information:
import org.constretto.ConstrettoBuilder;
import org.constretto.ConstrettoConfiguration;
import org.constretto.ConstrettoConfigurationMetadata;
import org.constretto.annotation.ConfigurationSource;
import org.constretto.model.ClassPathResource;
import org.constretto.model.ConfigurationValue;
import org.constretto.model.TaggedPropertySet;
@ConfigurationSource(tags = "config")
public class ConfigUpdater {
private static final String CONFIG_FILE = "config.properties";
private static long lastModified;
public static void updateConfig() {
ConstrettoBuilder constrettoBuilder = new ConstrettoBuilder();
ConstrettoConfiguration constrettoConfiguration = constrettoBuilder.createPropertiesStore()
.addResource(new ClassPathResource(CONFIG_FILE))
.done()
.getConfiguration();
ConstrettoConfigurationMetadata metadata = constrettoConfiguration.getConfigurationMetadata();
TaggedPropertySet propertySet = metadata.getPropertySetByTag("config");
ConfigurationValue configurationValue = propertySet.getValue();
long newLastModified = configurationValue.getSource().getLastModified();
if (newLastModified > lastModified) {
constrettoBuilder.replaceConfiguration(constrettoConfiguration);
lastModified = newLastModified;
}
}
public static String getString(String key) {
updateConfig();
return constrettoConfiguration.evaluateToString(key);
}
public static int getInt(String key) {
updateConfig();
return constrettoConfiguration.evaluateTo(Integer.class).from(key);
}
}
Corrates the method of calling `ConfiguPdater.UpdateConfig ()` in the application class to check the update status of the configuration information.If there is update, use the method to replace the existing configuration information with the method of `Constabletobuilder.replaceConfiguration ()`.
Through reasonable use of configuration cache, avoid reading configuration information and frequent configuration information updates, we can improve the performance of the Constretto framework and accelerate the application management process of the application.