All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.oschina.j2cache.cache.support.util.SpringJ2CacheConfigUtil Maven / Gradle / Ivy

There is a newer version: 2.8.0-release
Show newest version
package net.oschina.j2cache.cache.support.util;

import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.support.ResourcePropertySource;

import net.oschina.j2cache.J2CacheConfig;

public class SpringJ2CacheConfigUtil {

	/**
	 * 从spring环境变量中查找j2cache配置
	 */
	public final static J2CacheConfig initFromConfig(StandardEnvironment environment){
		J2CacheConfig config = new J2CacheConfig();
		config.setSerialization(environment.getProperty("j2cache.serialization"));
		config.setBroadcast(environment.getProperty("j2cache.broadcast"));
		config.setL1CacheName(environment.getProperty("j2cache.L1.provider_class"));
		config.setL2CacheName(environment.getProperty("j2cache.L2.provider_class"));
		config.setSyncTtlToRedis(!"false".equalsIgnoreCase(environment.getProperty("j2cache.sync_ttl_to_redis")));
		config.setDefaultCacheNullObject("true".equalsIgnoreCase(environment.getProperty("j2cache.default_cache_null_object")));
		String l2_config_section = environment.getProperty("j2cache.L2.config_section");
		if (l2_config_section == null || l2_config_section.trim().equals(""))
			l2_config_section = config.getL2CacheName();
		final String l2_section = l2_config_section;
		environment.getPropertySources().forEach(a -> {
			if(a instanceof ResourcePropertySource) {
				ResourcePropertySource c = (ResourcePropertySource) a;
				c.getSource().forEach((k,v) -> {
					String key = k;
					if (key.startsWith(config.getBroadcast() + ".")) {
						config.getBroadcastProperties().setProperty(key.substring((config.getBroadcast() + ".").length()),
								environment.getProperty(key));
					}	
					if (key.startsWith(config.getL1CacheName() + ".")) {
						config.getL1CacheProperties().setProperty(key.substring((config.getL1CacheName() + ".").length()),
								environment.getProperty(key));
					}
					if (key.startsWith(l2_section + ".")) {
						config.getL2CacheProperties().setProperty(key.substring((l2_section + ".").length()),
								environment.getProperty(key));
					}
				});
			}
		});
		return config;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy