プロパティをapache-commons-configurationフレームワークにキャッシュする方法を探しています。config.xmlで定義されているさまざまな場所からプロパティを取得するのに長い時間がかかりました。それで、インターフェースのキャッシュされた(たとえば、時間によって)実装はありConfiguration
ますか?
2507 次
3 に答える
1
apacheオブジェクトをあるクラスの静的変数に保存し、完了したらnullに設定できます。それを読むために静的ゲッターを持っている
apache config APIについてはよくわかりませんが、静的HashMapを使用してプロパティを格納しています。
すべての文字列の場合:
プライベート静的マップデータ=新しいHashMap();
プロパティとして公開できるため、どこでも使用できます
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}
}
プリミティブ以外のデータ型の場合
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}
}
しばらくしてからオブジェクトを削除したい場合は、HashMapの代わりにWhirlyCacheを使用できます。何がうまくいかないのかわかりませんか?
于 2013-03-11T10:35:52.797 に答える
1
最後に、guavaを使用して独自のキャッシュを作成しました。
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);
}
}
于 2013-03-15T13:49:54.787 に答える
1
DatabaseConfigurationを拡張して、データベースに常にヒットしないようにします。リロードに関しては、必要な場所で構成をインスタンス化し、使い終わったら破棄します。
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;
}
}
于 2013-07-15T09:26:00.547 に答える