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를 확장 :
관련 문제
- 1. Apache commons logging configuration
- 2. java : Preferences API 대 Apache Commons Configuration
- 3. spring 및 commons configuration
- 4. Apache Commons Configuration - 구성 파일을로드/사용할 수 없습니다.
- 5. Hbase 예외 org/apache/commons/configuration/Configuration을 찾을 수 없습니다.
- 6. Apache Commons Configuration (java)을 사용하여 XML 파일을로드하는 방법은 무엇입니까?
- 7. apache commons configuration : 루트 요소의 xml 네임 스페이스 설정
- 8. Apache Commons Configuration : 자동 저장을 사용하는 중복 항목
- 9. Apache Tomcat Configuration Web.xml
- 10. Apache WebDav Configuration
- 11. django apache configuration confusing.
- 12. Apache Geode Configuration
- 13. Hibernate 3.5.5, Second Level Cache Configuration
- 14. Commons-configuration 및 DefaultConfigurationBuilder가 자동으로 다시로드되지 않습니다.
- 15. Apache Commons FTPClient.listFiles
- 16. Apache Commons 수학 최적화
- 17. Apache Commons JCI ReloadingClassLoader
- 18. sun.misc.BASE64 to apache commons
- 19. JSP로 로그인하는 Apache Commons
- 20. apache commons lang StrTokenizer
- 21. Java Apache Commons 사용자
- 22. Android - Apache Commons VFS 설치 -
- 23. Apache Camel이 Apache commons 파이프 라인을 대체합니까?
- 24. Apache Commons VFS with Quercus
- 25. 데비안에서 Apache Commons CLI 사용하기
- 26. Apache Commons HttpClient PostMethod를 지원합니까?
- 27. Apache Commons Logger를 설정하는 방법
- 28. Apache commons CLI - 반복되는 옵션
- 29. httpURLConnection vs apache commons http
- 30. 라이센스 Android Apache Commons 수학