apache-commons-configuration 프레임 워크에서 내 속성을 캐시 할 수있는 방법을 찾고 있습니다. 내 config.xml에 정의 된 다른 장소에서 속성을 가져 오는 데 오랜 시간이 걸립니다. 그래서, 캐시 된 시간 (예를 들어, 시간) Configuration
인터페이스의 구현은 무엇입니까?Apache commons configuration cache
3
A
답변
1
마지막으로, 나는 구아바를 사용하여 내 자신의 캐시를 writed. 재로드의 경우, 필자가 필요로하는 곳에서 구성을 인스턴스화하고 완료 할 때 버립니다.
public class MyConfig extends DatabaseConfiguration {
private WeakHashMap<String,Object> cache = new WeakHashMap<String,Object>();
public MyConfig(String datasourceString,String section) throws NamingException {
this((DataSource) new InitialContext().lookup(datasourceString),section);
}
protected MyConfig(DataSource datasource,String section) {
super(datasource, "COMMON_CONFIG","PROP_SECTION", "PROP_KEY", "PROP_VALUE",section);
}
@Override
public Object getProperty(String key){
Object cachedValue = cache.get(key);
if (cachedValue != null){
return cachedValue;
}
Object databaseValue = super.getProperty(key);
cache.put(key, databaseValue);
return databaseValue;
}
}
1
일부 클래스의 정적 변수에 apache 객체를 저장하고 완료되면 null로 설정할 수 있습니다. 정적 getter가 그것을 읽도록하십시오.
apache config API에 대해서는 확실하지 않지만 정적 HashMap을 사용하고 속성을 저장합니다.
개인 정적지도 데이터 = 새의 HashMap(); 당신이 할 개체를 원하는 경우 원시
public class Props{
private static Map<String, Object> data = new HashMap<String, Object>();
public static void put(String name, Object val){
data.put(name,val);
}
public static String get(String name){
return data.get(name)
}
public static void load(){//todo }
public static void save(){//todo if needed if few change and need persistence}
}
외에 모든 데이터 유형에 대해 어디
public class Props{
private static Map<String, String> data = new HashMap<String, String>();
public static void put(String name, String val){
data.put(name,val);
}
public static String get(String name){
return data.get(name)
}
public static void load(){//todo }
public static void save(){//todo if needed if few change and need persistence}
}
을 사용할 수 있도록
는 속성으로 노출 할 수 있습니다 때로는 HashMap 대신 WhirlyCache를 사용할 수 있습니다. 나는 무엇이 잘못 될지 모른다. 내 데이터베이스의 모든 시간에 충돌하지 않도록
public class Cfg {
private static Logger log = LoggerFactory.getLogger(Cfg.class);
private Configuration cfg;
private LoadingCache<String, Boolean> boolCache;
private LoadingCache<String, String> stringCache;
private LoadingCache<String, Float> floatCache;
private LoadingCache<String, Integer> integerCache;
private LoadingCache<String, List> listCache;
@PostConstruct
public void init() {
boolCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Boolean>() {
@Override
public Boolean load(String key) throws Exception {
return check(cfg.getBoolean(key), key);
}
});
stringCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return check(cfg.getString(key), key);
}
});
floatCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Float>() {
@Override
public Float load(String key) throws Exception {
return check(cfg.getFloat(key), key);
}
});
integerCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Integer>() {
@Override
public Integer load(String key) throws Exception {
return check(cfg.getInt(key), key);
}
});
listCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, List>() {
@Override
public List load(String key) throws Exception {
return check(cfg.getList(key), key);
}
});
}
public boolean _bool(String key) {
try {
return boolCache.get(key);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
public float _float(String key) {
try {
return floatCache.get(key);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
public int _int(String key) {
try {
return integerCache.get(key);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
public String _string(String key) {
try {
return stringCache.get(key);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
public List<String> _list(String key) {
try {
//noinspection unchecked
return listCache.get(key);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
public void setCfg(Configuration cfg) {
this.cfg = cfg;
}
private <T> T check(T el, String key) {
if (el != null) {
return el;
}
throw new KeyNotFound(key);
}
}
1
내가 DatabaseConfiguration를 확장 :